1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-11-24 12:06:15 +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') .module('bit.accounts')
.controller('accountsLoginTwoFactorController', function ($scope, $state, authService, toastr, utilsService, SweetAlert, .controller('accountsLoginTwoFactorController', function ($scope, $state, authService, toastr, utilsService, SweetAlert,
@ -85,7 +85,7 @@
} }
var key = cryptoService.makeKey(masterPassword, email); 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); var request = new TwoFactorEmailRequest(email, hash);
apiService.postTwoFactorEmail(request, function () { apiService.postTwoFactorEmail(request, function () {
if (doToast) { if (doToast) {

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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