1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-12-25 16:59:17 +01:00

convert methods to promises

This commit is contained in:
Kyle Spearrin 2017-11-02 08:54:40 -04:00
parent 54a909ab24
commit c3704c3dfd
7 changed files with 86 additions and 89 deletions

View File

@ -1,4 +1,4 @@
angular
angular
.module('bit.accounts')
.controller('accountsLoginTwoFactorController', function ($scope, $state, authService, toastr, utilsService, SweetAlert,
@ -85,7 +85,7 @@
}
var key = cryptoService.makeKey(masterPassword, email);
cryptoService.hashPassword(masterPassword, key, function (hash) {
cryptoService.hashPassword(masterPassword, key).then(function (hash) {
var request = new TwoFactorEmailRequest(email, hash);
apiService.postTwoFactorEmail(request, function () {
if (doToast) {

View File

@ -1,4 +1,4 @@
angular
angular
.module('bit.accounts')
.controller(
@ -46,16 +46,16 @@
function registerPromise(key, masterPassword, email, hint) {
var deferred = $q.defer();
cryptoService.makeEncKey(key).then(function (encKey) {
cryptoService.hashPassword(masterPassword, key, function (hashedPassword) {
var request = new RegisterRequest(email, hashedPassword, hint, encKey.encryptedString);
apiService.postRegister(request,
function () {
deferred.resolve();
},
function (error) {
deferred.reject(error);
});
});
return cryptoService.hashPassword(masterPassword, key);
}).then(function (hashedPassword) {
var request = new RegisterRequest(email, hashedPassword, hint, encKey.encryptedString);
apiService.postRegister(request,
function () {
deferred.resolve();
},
function (error) {
deferred.reject(error);
});
});
return deferred.promise;
}

View File

@ -1,4 +1,4 @@
angular
angular
.module('bit.lock')
.controller('lockController', function ($scope, $state, $analytics, i18nService, cryptoService, toastr,
@ -26,18 +26,18 @@
$scope.submit = function () {
userService.getEmail(function (email) {
var key = cryptoService.makeKey($scope.masterPassword, email);
cryptoService.hashPassword($scope.masterPassword, key, function (keyHash) {
cryptoService.getKeyHash(function (storedKeyHash) {
if (storedKeyHash && keyHash && storedKeyHash === keyHash) {
cryptoService.setKey(key).then(function () {
chrome.runtime.sendMessage({ command: 'unlocked' });
$state.go('tabs.current');
});
}
else {
toastr.error(i18nService.invalidMasterPassword, i18nService.errorsOccurred);
}
});
cryptoService.hashPassword($scope.masterPassword, key).then(function (keyHash) {
return cryptoService.getKeyHash();
}).then(function (storedKeyHash) {
if (storedKeyHash && keyHash && storedKeyHash === keyHash) {
cryptoService.setKey(key).then(function () {
chrome.runtime.sendMessage({ command: 'unlocked' });
$state.go('tabs.current');
});
}
else {
toastr.error(i18nService.invalidMasterPassword, i18nService.errorsOccurred);
}
});
});
};

View File

@ -1,4 +1,4 @@
angular
angular
.module('bit.services')
.factory('authService', function (cryptoService, apiService, userService, tokenService, $q, $rootScope,
@ -15,59 +15,59 @@
deviceRequest = new DeviceRequest(appId, utilsService);
return tokenService.getTwoFactorToken(email);
}).then(function (twoFactorRememberedToken) {
cryptoService.hashPassword(masterPassword, key, function (hashedPassword) {
var request;
return cryptoService.hashPassword(masterPassword, key);
}).then(function (hashedPassword) {
var request;
if (twoFactorToken && typeof (twoFactorProvider) !== 'undefined' && twoFactorProvider !== null) {
request = new TokenRequest(email, hashedPassword, twoFactorProvider, twoFactorToken, remember,
deviceRequest);
}
else if (twoFactorRememberedToken) {
request = new TokenRequest(email, hashedPassword, constantsService.twoFactorProvider.remember,
twoFactorRememberedToken, false, deviceRequest);
}
else {
request = new TokenRequest(email, hashedPassword, null, null, false, deviceRequest);
if (twoFactorToken && typeof (twoFactorProvider) !== 'undefined' && twoFactorProvider !== null) {
request = new TokenRequest(email, hashedPassword, twoFactorProvider, twoFactorToken, remember,
deviceRequest);
}
else if (twoFactorRememberedToken) {
request = new TokenRequest(email, hashedPassword, constantsService.twoFactorProvider.remember,
twoFactorRememberedToken, false, deviceRequest);
}
else {
request = new TokenRequest(email, hashedPassword, null, null, false, deviceRequest);
}
apiService.postIdentityToken(request, function (response) {
// success
if (!response || !response.accessToken) {
return;
}
apiService.postIdentityToken(request, function (response) {
// success
if (!response || !response.accessToken) {
return;
}
if (response.twoFactorToken) {
tokenService.setTwoFactorToken(response.twoFactorToken, email, function () { });
}
if (response.twoFactorToken) {
tokenService.setTwoFactorToken(response.twoFactorToken, email, function () { });
}
tokenService.setTokens(response.accessToken, response.refreshToken, function () {
cryptoService.setKey(key).then(function () {
cryptoService.setKeyHash(hashedPassword, function () {
userService.setUserIdAndEmail(tokenService.getUserId(), tokenService.getEmail(),
function () {
cryptoService.setEncKey(response.key).then(function () {
return cryptoService.setEncPrivateKey(response.privateKey);
}).then(function () {
chrome.runtime.sendMessage({ command: 'loggedIn' });
deferred.resolve({
twoFactor: false,
twoFactorProviders: null
});
});
tokenService.setTokens(response.accessToken, response.refreshToken, function () {
cryptoService.setKey(key).then(function () {
return cryptoService.setKeyHash(hashedPassword);
}).then(function () {
userService.setUserIdAndEmail(tokenService.getUserId(), tokenService.getEmail(),
function () {
cryptoService.setEncKey(response.key).then(function () {
return cryptoService.setEncPrivateKey(response.privateKey);
}).then(function () {
chrome.runtime.sendMessage({ command: 'loggedIn' });
deferred.resolve({
twoFactor: false,
twoFactorProviders: null
});
});
});
});
});
}, function (providers) {
// two factor required
deferred.resolve({
twoFactor: true,
twoFactorProviders: providers
});
}, function (error) {
// error
deferred.reject(error);
});
}, function (providers) {
// two factor required
deferred.resolve({
twoFactor: true,
twoFactorProviders: providers
});
}, function (error) {
// error
deferred.reject(error);
});
});
return deferred.promise;

View File

@ -1,4 +1,4 @@
angular
angular
.module('bit.settings')
.controller('settingsController', function ($scope, $state, SweetAlert, utilsService, $analytics,
@ -31,7 +31,7 @@
}
chrome.storage.local.set(obj, function () {
cryptoService.getKeyHash(function (keyHash) {
cryptoService.getKeyHash().then(function (keyHash) {
if (keyHash) {
cryptoService.toggleKey();
}

View File

@ -1,4 +1,4 @@
angular
angular
.module('bit.tools')
.controller('toolsExportController', function ($scope, $state, toastr, $q, $analytics,
@ -24,15 +24,15 @@
userService.getEmail(function (email) {
var key = cryptoService.makeKey($scope.masterPassword, email);
cryptoService.hashPassword($scope.masterPassword, key, function (keyHash) {
cryptoService.getKeyHash(function (storedKeyHash) {
if (storedKeyHash && keyHash && storedKeyHash === keyHash) {
deferred.resolve();
}
else {
deferred.reject();
}
});
cryptoService.hashPassword($scope.masterPassword, key).then(function (keyHash) {
return cryptoService.getKeyHash();
}).then(function (storedKeyHash) {
if (storedKeyHash && keyHash && storedKeyHash === keyHash) {
deferred.resolve();
}
else {
deferred.reject();
}
});
});

View File

@ -42,8 +42,7 @@ export default class CryptoService {
return self.utilsService.saveObjToStorage(Keys.key, key.keyB64);
}
// TODO: convert uses to promises
setKeyHash(keyHash: string) {
setKeyHash(keyHash: string): Promise<{}> {
this.keyHash = keyHash;
return this.utilsService.saveObjToStorage(Keys.keyHash, keyHash);
}
@ -66,7 +65,7 @@ export default class CryptoService {
}
// TODO: proper response model type for orgs
setOrgKeys(orgs: any) {
setOrgKeys(orgs: any): Promise<{}> {
const orgKeys: any = {};
for (const org of orgs) {
orgKeys[org.id] = org.key;
@ -93,7 +92,6 @@ export default class CryptoService {
return key == null ? null : this.key;
}
// TODO: convert uses to promises
getKeyHash(): Promise<string> {
if (this.keyHash != null) {
return Promise.resolve(this.keyHash);
@ -259,7 +257,6 @@ export default class CryptoService {
return new SymmetricCryptoKey(keyBytes);
}
// TODO: convert uses to promises
async hashPassword(password: string, key: SymmetricCryptoKey): Promise<string> {
const storedKey = await this.getKey();
key = key || storedKey;