mirror of
https://github.com/bitwarden/desktop.git
synced 2024-11-18 10:55:48 +01:00
more promise/utils helper conversions
This commit is contained in:
parent
0fdee0f13b
commit
999689cf7e
@ -31,7 +31,7 @@ var bg_isBackground = true,
|
|||||||
bg_utilsService = new UtilsService();
|
bg_utilsService = new UtilsService();
|
||||||
bg_i18nService = new i18nService(bg_utilsService);
|
bg_i18nService = new i18nService(bg_utilsService);
|
||||||
bg_constantsService = new ConstantsService(bg_i18nService);
|
bg_constantsService = new ConstantsService(bg_i18nService);
|
||||||
bg_cryptoService = new CryptoService(bg_constantsService);
|
bg_cryptoService = new CryptoService(bg_constantsService, bg_utilsService);
|
||||||
bg_tokenService = new TokenService(bg_utilsService);
|
bg_tokenService = new TokenService(bg_utilsService);
|
||||||
bg_appIdService = new AppIdService(bg_utilsService);
|
bg_appIdService = new AppIdService(bg_utilsService);
|
||||||
bg_apiService = new ApiService(bg_tokenService, bg_appIdService, bg_utilsService, bg_constantsService, logout);
|
bg_apiService = new ApiService(bg_tokenService, bg_appIdService, bg_utilsService, bg_constantsService, logout);
|
||||||
|
@ -29,7 +29,7 @@
|
|||||||
cryptoService.hashPassword($scope.masterPassword, key, function (keyHash) {
|
cryptoService.hashPassword($scope.masterPassword, key, function (keyHash) {
|
||||||
cryptoService.getKeyHash(function (storedKeyHash) {
|
cryptoService.getKeyHash(function (storedKeyHash) {
|
||||||
if (storedKeyHash && keyHash && storedKeyHash === keyHash) {
|
if (storedKeyHash && keyHash && storedKeyHash === keyHash) {
|
||||||
cryptoService.setKey(key, function () {
|
cryptoService.setKey(key).then(function () {
|
||||||
chrome.runtime.sendMessage({ command: 'unlocked' });
|
chrome.runtime.sendMessage({ command: 'unlocked' });
|
||||||
$state.go('tabs.current');
|
$state.go('tabs.current');
|
||||||
});
|
});
|
||||||
|
@ -41,7 +41,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
tokenService.setTokens(response.accessToken, response.refreshToken, function () {
|
tokenService.setTokens(response.accessToken, response.refreshToken, function () {
|
||||||
cryptoService.setKey(key, function () {
|
cryptoService.setKey(key).then(function () {
|
||||||
cryptoService.setKeyHash(hashedPassword, function () {
|
cryptoService.setKeyHash(hashedPassword, function () {
|
||||||
userService.setUserIdAndEmail(tokenService.getUserId(), tokenService.getEmail(),
|
userService.setUserIdAndEmail(tokenService.getUserId(), tokenService.getEmail(),
|
||||||
function () {
|
function () {
|
||||||
|
@ -33,7 +33,7 @@
|
|||||||
chrome.storage.local.set(obj, function () {
|
chrome.storage.local.set(obj, function () {
|
||||||
cryptoService.getKeyHash(function (keyHash) {
|
cryptoService.getKeyHash(function (keyHash) {
|
||||||
if (keyHash) {
|
if (keyHash) {
|
||||||
cryptoService.toggleKey(function () { });
|
cryptoService.toggleKey();
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
SweetAlert.swal({
|
SweetAlert.swal({
|
||||||
@ -44,7 +44,7 @@
|
|||||||
cancelButtonText: i18nService.cancel
|
cancelButtonText: i18nService.cancel
|
||||||
}, function (confirmed) {
|
}, function (confirmed) {
|
||||||
if (confirmed) {
|
if (confirmed) {
|
||||||
cryptoService.toggleKey(function () { });
|
cryptoService.toggleKey();
|
||||||
chrome.runtime.sendMessage({ command: 'logout' });
|
chrome.runtime.sendMessage({ command: 'logout' });
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
function CryptoService(constantsService) {
|
function CryptoService(constantsService, utilsService) {
|
||||||
this.constantsService = constantsService;
|
this.constantsService = constantsService;
|
||||||
|
this.utilsService = utilsService;
|
||||||
|
|
||||||
initCryptoService(constantsService);
|
initCryptoService(constantsService);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -11,29 +13,25 @@ function initCryptoService(constantsService) {
|
|||||||
_privateKey,
|
_privateKey,
|
||||||
_orgKeys,
|
_orgKeys,
|
||||||
_crypto = window.crypto,
|
_crypto = window.crypto,
|
||||||
_subtle = window.crypto.subtle;
|
_subtle = window.crypto.subtle,
|
||||||
|
keyKey = 'key',
|
||||||
CryptoService.prototype.setKey = function (key, callback) {
|
encOrgKeysKey = 'encOrgKeys',
|
||||||
if (!callback || typeof callback !== 'function') {
|
encPrivateKeyKey = 'encPrivateKey',
|
||||||
throw 'callback function required';
|
encKeyKey = 'encKey',
|
||||||
}
|
keyHashKey = 'keyHash';
|
||||||
|
|
||||||
|
CryptoService.prototype.setKey = function (key) {
|
||||||
var self = this;
|
var self = this;
|
||||||
_key = key;
|
_key = key;
|
||||||
|
|
||||||
chrome.storage.local.get(self.constantsService.lockOptionKey, function (obj) {
|
return self.utilsService.getObjFromStorage(self.constantsService.lockOptionKey).then(function (option) {
|
||||||
if (obj && (obj[self.constantsService.lockOptionKey] || obj[self.constantsService.lockOptionKey] === 0)) {
|
if (option || option === 0) {
|
||||||
// if we have a lock option set, we do not store the key
|
// if we have a lock option set, we do not store the key
|
||||||
callback();
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
chrome.storage.local.set({
|
return self.utilsService.saveObjToStorage(keyKey, key.keyB64);
|
||||||
'key': key.keyB64
|
})
|
||||||
}, function () {
|
|
||||||
callback();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
CryptoService.prototype.setKeyHash = function (keyHash, callback) {
|
CryptoService.prototype.setKeyHash = function (keyHash, callback) {
|
||||||
@ -51,84 +49,52 @@ function initCryptoService(constantsService) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
CryptoService.prototype.setEncKey = function (encKey) {
|
CryptoService.prototype.setEncKey = function (encKey) {
|
||||||
var deferred = Q.defer();
|
|
||||||
|
|
||||||
if (encKey === undefined) {
|
if (encKey === undefined) {
|
||||||
deferred.resolve();
|
return Q();
|
||||||
return deferred.promise;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
chrome.storage.local.set({
|
return this.utilsService.saveObjToStorage(encKeyKey, encKey).then(function () {
|
||||||
'encKey': encKey
|
|
||||||
}, function () {
|
|
||||||
_encKey = null;
|
_encKey = null;
|
||||||
deferred.resolve();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
return deferred.promise;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
CryptoService.prototype.setEncPrivateKey = function (encPrivateKey) {
|
CryptoService.prototype.setEncPrivateKey = function (encPrivateKey) {
|
||||||
var deferred = Q.defer();
|
|
||||||
|
|
||||||
if (encPrivateKey === undefined) {
|
if (encPrivateKey === undefined) {
|
||||||
deferred.resolve();
|
return Q();
|
||||||
return deferred.promise;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
chrome.storage.local.set({
|
return this.utilsService.saveObjToStorage(encPrivateKeyKey, encPrivateKey).then(function () {
|
||||||
'encPrivateKey': encPrivateKey
|
|
||||||
}, function () {
|
|
||||||
_privateKey = null;
|
_privateKey = null;
|
||||||
deferred.resolve();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
return deferred.promise;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
CryptoService.prototype.setOrgKeys = function (orgs) {
|
CryptoService.prototype.setOrgKeys = function (orgs) {
|
||||||
var deferred = Q.defer();
|
|
||||||
|
|
||||||
var orgKeys = {};
|
var orgKeys = {};
|
||||||
for (var i = 0; i < orgs.length; i++) {
|
for (var i = 0; i < orgs.length; i++) {
|
||||||
orgKeys[orgs[i].id] = orgs[i].key;
|
orgKeys[orgs[i].id] = orgs[i].key;
|
||||||
}
|
}
|
||||||
|
|
||||||
chrome.storage.local.set({
|
return this.utilsService.saveObjToStorage(encOrgKeysKey, orgKeys);
|
||||||
'encOrgKeys': orgKeys
|
|
||||||
}, function () {
|
|
||||||
deferred.resolve();
|
|
||||||
});
|
|
||||||
|
|
||||||
return deferred.promise;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
CryptoService.prototype.getKey = function () {
|
CryptoService.prototype.getKey = function () {
|
||||||
var deferred = Q.defer();
|
|
||||||
|
|
||||||
if (_key) {
|
if (_key) {
|
||||||
deferred.resolve(_key);
|
return Q(_key);
|
||||||
return deferred.promise;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var self = this;
|
var self = this;
|
||||||
chrome.storage.local.get(self.constantsService.lockOptionKey, function (obj) {
|
return self.utilsService.getObjFromStorage(self.constantsService.lockOptionKey).then(function (option) {
|
||||||
if (obj && (obj[self.constantsService.lockOptionKey] || obj[self.constantsService.lockOptionKey] === 0)) {
|
if (option || option === 0) {
|
||||||
// if we have a lock option set, we do not try to fetch the storage key since it should not even be there
|
return false;
|
||||||
deferred.resolve(null);
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
chrome.storage.local.get('key', function (obj) {
|
return self.utilsService.getObjFromStorage(keyKey);
|
||||||
if (obj && obj.key) {
|
}).then(function (key) {
|
||||||
_key = new SymmetricCryptoKey(obj.key, true);
|
if (key) {
|
||||||
|
_key = new SymmetricCryptoKey(key, true);
|
||||||
}
|
}
|
||||||
|
return key === false ? null : _key;
|
||||||
deferred.resolve(_key);
|
|
||||||
});
|
});
|
||||||
});
|
|
||||||
|
|
||||||
return deferred.promise;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
CryptoService.prototype.getKeyHash = function (callback) {
|
CryptoService.prototype.getKeyHash = function (callback) {
|
||||||
@ -141,7 +107,7 @@ function initCryptoService(constantsService) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
chrome.storage.local.get('keyHash', function (obj) {
|
chrome.storage.local.get(keyHashKey, function (obj) {
|
||||||
if (obj && obj.keyHash) {
|
if (obj && obj.keyHash) {
|
||||||
_keyHash = obj.keyHash;
|
_keyHash = obj.keyHash;
|
||||||
}
|
}
|
||||||
@ -151,56 +117,59 @@ function initCryptoService(constantsService) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
CryptoService.prototype.getEncKey = function () {
|
CryptoService.prototype.getEncKey = function () {
|
||||||
var deferred = Q.defer();
|
|
||||||
if (_encKey) {
|
if (_encKey) {
|
||||||
deferred.resolve(_encKey);
|
return Q(_encKey);
|
||||||
return deferred.promise;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var self = this;
|
var self = this,
|
||||||
chrome.storage.local.get('encKey', function (obj) {
|
encKey = null;
|
||||||
if (!obj || !obj.encKey) {
|
return self.utilsService.getObjFromStorage(encKeyKey).then(function (theEncKey) {
|
||||||
deferred.resolve(null);
|
if (!theEncKey) {
|
||||||
return;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
self.getKey().then(function (key) {
|
encKey = theEncKey;
|
||||||
return self.decrypt(new CipherString(obj.encKey), key, 'raw');
|
return self.getKey();
|
||||||
}).then(function (encKey) {
|
}).then(function (key) {
|
||||||
_encKey = new SymmetricCryptoKey(encKey);
|
if (!key) {
|
||||||
deferred.resolve(_encKey);
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return self.decrypt(new CipherString(encKey), key, 'raw');
|
||||||
|
}).then(function (decEncKey) {
|
||||||
|
if (decEncKey) {
|
||||||
|
_encKey = new SymmetricCryptoKey(decEncKey);
|
||||||
|
return _encKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
}, function () {
|
}, function () {
|
||||||
deferred.reject('Cannot get enc key. Decryption failed.');
|
throw 'Cannot get enc key. Decryption failed.';
|
||||||
});
|
});
|
||||||
});
|
|
||||||
|
|
||||||
return deferred.promise;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
CryptoService.prototype.getPrivateKey = function () {
|
CryptoService.prototype.getPrivateKey = function () {
|
||||||
var deferred = Q.defer();
|
|
||||||
if (_privateKey) {
|
if (_privateKey) {
|
||||||
deferred.resolve(_privateKey);
|
return Q(_privateKey);
|
||||||
return deferred.promise;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var self = this;
|
var self = this;
|
||||||
chrome.storage.local.get('encPrivateKey', function (obj) {
|
return self.utilsService.getObjFromStorage(encPrivateKeyKey).then(function (encPrivateKey) {
|
||||||
if (!obj || !obj.encPrivateKey) {
|
if (!encPrivateKey) {
|
||||||
deferred.resolve(null);
|
return null;
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
return self.decrypt(new CipherString(encPrivateKey), null, 'raw');
|
||||||
self.decrypt(new CipherString(obj.encPrivateKey), null, 'raw').then(function (privateKey) {
|
}).then(function (privateKey) {
|
||||||
|
if (privateKey) {
|
||||||
var privateKeyB64 = forge.util.encode64(privateKey);
|
var privateKeyB64 = forge.util.encode64(privateKey);
|
||||||
_privateKey = fromB64ToArray(privateKeyB64).buffer;
|
_privateKey = fromB64ToArray(privateKeyB64).buffer;
|
||||||
deferred.resolve(_privateKey);
|
return _privateKey;
|
||||||
}, function () {
|
}
|
||||||
deferred.reject('Cannot get private key. Decryption failed.');
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
return deferred.promise;
|
return null;
|
||||||
|
}, function () {
|
||||||
|
throw 'Cannot get private key. Decryption failed.';
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
CryptoService.prototype.getOrgKeys = function () {
|
CryptoService.prototype.getOrgKeys = function () {
|
||||||
@ -212,7 +181,7 @@ function initCryptoService(constantsService) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var self = this;
|
var self = this;
|
||||||
chrome.storage.local.get('encOrgKeys', function (obj) {
|
chrome.storage.local.get(encOrgKeysKey, function (obj) {
|
||||||
if (obj && obj.encOrgKeys) {
|
if (obj && obj.encOrgKeys) {
|
||||||
var orgKeys = {},
|
var orgKeys = {},
|
||||||
setKey = false;
|
setKey = false;
|
||||||
@ -252,9 +221,7 @@ function initCryptoService(constantsService) {
|
|||||||
|
|
||||||
CryptoService.prototype.getOrgKey = function (orgId) {
|
CryptoService.prototype.getOrgKey = function (orgId) {
|
||||||
if (!orgId) {
|
if (!orgId) {
|
||||||
var deferred = Q.defer();
|
return Q(null);
|
||||||
deferred.resolve(null);
|
|
||||||
return deferred.promise;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.getOrgKeys().then(function (orgKeys) {
|
return this.getOrgKeys().then(function (orgKeys) {
|
||||||
@ -267,73 +234,37 @@ function initCryptoService(constantsService) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
CryptoService.prototype.clearKey = function (callback) {
|
CryptoService.prototype.clearKey = function (callback) {
|
||||||
var deferred = Q.defer();
|
|
||||||
|
|
||||||
_key = _legacyEtmKey = null;
|
_key = _legacyEtmKey = null;
|
||||||
chrome.storage.local.remove('key', function () {
|
return this.utilsService.removeFromStorage(keyKey);
|
||||||
deferred.resolve();
|
|
||||||
});
|
|
||||||
|
|
||||||
return deferred.promise;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
CryptoService.prototype.clearKeyHash = function (callback) {
|
CryptoService.prototype.clearKeyHash = function (callback) {
|
||||||
var deferred = Q.defer();
|
|
||||||
|
|
||||||
_keyHash = null;
|
_keyHash = null;
|
||||||
chrome.storage.local.remove('keyHash', function () {
|
return this.utilsService.removeFromStorage(keyHashKey);
|
||||||
deferred.resolve();
|
|
||||||
});
|
|
||||||
|
|
||||||
return deferred.promise;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
CryptoService.prototype.clearEncKey = function (memoryOnly) {
|
CryptoService.prototype.clearEncKey = function (memoryOnly) {
|
||||||
var deferred = Q.defer();
|
|
||||||
|
|
||||||
_encKey = null;
|
_encKey = null;
|
||||||
if (memoryOnly) {
|
if (memoryOnly) {
|
||||||
deferred.resolve();
|
return Q();
|
||||||
}
|
}
|
||||||
else {
|
return this.utilsService.removeFromStorage(encKeyKey);
|
||||||
chrome.storage.local.remove('encKey', function () {
|
|
||||||
deferred.resolve();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
return deferred.promise;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
CryptoService.prototype.clearPrivateKey = function (memoryOnly) {
|
CryptoService.prototype.clearPrivateKey = function (memoryOnly) {
|
||||||
var deferred = Q.defer();
|
|
||||||
|
|
||||||
_privateKey = null;
|
_privateKey = null;
|
||||||
if (memoryOnly) {
|
if (memoryOnly) {
|
||||||
deferred.resolve();
|
return Q();
|
||||||
}
|
}
|
||||||
else {
|
return this.utilsService.removeFromStorage(encPrivateKeyKey);
|
||||||
chrome.storage.local.remove('encPrivateKey', function () {
|
|
||||||
deferred.resolve();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
return deferred.promise;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
CryptoService.prototype.clearOrgKeys = function (memoryOnly) {
|
CryptoService.prototype.clearOrgKeys = function (memoryOnly) {
|
||||||
var deferred = Q.defer();
|
|
||||||
|
|
||||||
_orgKeys = null;
|
_orgKeys = null;
|
||||||
if (memoryOnly) {
|
if (memoryOnly) {
|
||||||
deferred.resolve();
|
return Q();
|
||||||
}
|
}
|
||||||
else {
|
return this.utilsService.removeFromStorage(encOrgKeysKey);
|
||||||
chrome.storage.local.remove('encOrgKeys', function () {
|
|
||||||
deferred.resolve();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
return deferred.promise;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
CryptoService.prototype.clearKeys = function () {
|
CryptoService.prototype.clearKeys = function () {
|
||||||
@ -347,31 +278,25 @@ function initCryptoService(constantsService) {
|
|||||||
]);
|
]);
|
||||||
};
|
};
|
||||||
|
|
||||||
CryptoService.prototype.toggleKey = function (callback) {
|
CryptoService.prototype.toggleKey = function () {
|
||||||
if (!callback || typeof callback !== 'function') {
|
var self = this,
|
||||||
throw 'callback function required';
|
key = null;
|
||||||
}
|
|
||||||
|
|
||||||
var self = this;
|
return self.getKey().then(function (theKey) {
|
||||||
self.getKey().then(function (key) {
|
key = theKey;
|
||||||
chrome.storage.local.get(self.constantsService.lockOptionKey, function (obj) {
|
return self.utilsService.getObjFromStorage(self.constantsService.lockOptionKey);
|
||||||
if (obj && (obj[self.constantsService.lockOptionKey] || obj[self.constantsService.lockOptionKey] === 0)) {
|
}).then(function (option) {
|
||||||
|
if (option || option === 0) {
|
||||||
// if we have a lock option set, clear the key
|
// if we have a lock option set, clear the key
|
||||||
self.clearKey().then(function () {
|
return self.clearKey().then(function () {
|
||||||
_key = key;
|
_key = key;
|
||||||
callback();
|
|
||||||
return;
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// no lock option, so store the current key
|
// no lock option, so store the current key
|
||||||
self.setKey(key, function () {
|
return self.setKey(key);
|
||||||
callback();
|
|
||||||
return;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
CryptoService.prototype.makeKey = function (password, salt) {
|
CryptoService.prototype.makeKey = function (password, salt) {
|
||||||
|
Loading…
Reference in New Issue
Block a user