From c3704c3dfd1718785da5835b70e6919a579c3123 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Thu, 2 Nov 2017 08:54:40 -0400 Subject: [PATCH] convert methods to promises --- .../accountsLoginTwoFactorController.js | 4 +- .../accounts/accountsRegisterController.js | 22 ++--- src/popup/app/lock/lockController.js | 26 +++--- src/popup/app/services/authService.js | 92 +++++++++---------- src/popup/app/settings/settingsController.js | 4 +- src/popup/app/tools/toolsExportController.js | 20 ++-- src/services/crypto.service.ts | 7 +- 7 files changed, 86 insertions(+), 89 deletions(-) diff --git a/src/popup/app/accounts/accountsLoginTwoFactorController.js b/src/popup/app/accounts/accountsLoginTwoFactorController.js index ca497aab72..df3ff9dc46 100644 --- a/src/popup/app/accounts/accountsLoginTwoFactorController.js +++ b/src/popup/app/accounts/accountsLoginTwoFactorController.js @@ -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) { diff --git a/src/popup/app/accounts/accountsRegisterController.js b/src/popup/app/accounts/accountsRegisterController.js index 71669fdbb7..adfe07ea3e 100644 --- a/src/popup/app/accounts/accountsRegisterController.js +++ b/src/popup/app/accounts/accountsRegisterController.js @@ -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; } diff --git a/src/popup/app/lock/lockController.js b/src/popup/app/lock/lockController.js index c8ce93b577..fa9eae492c 100644 --- a/src/popup/app/lock/lockController.js +++ b/src/popup/app/lock/lockController.js @@ -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); + } }); }); }; diff --git a/src/popup/app/services/authService.js b/src/popup/app/services/authService.js index c2d94efd21..6bd72be89f 100644 --- a/src/popup/app/services/authService.js +++ b/src/popup/app/services/authService.js @@ -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; diff --git a/src/popup/app/settings/settingsController.js b/src/popup/app/settings/settingsController.js index 87fc9ab458..7a08a0d3fa 100644 --- a/src/popup/app/settings/settingsController.js +++ b/src/popup/app/settings/settingsController.js @@ -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(); } diff --git a/src/popup/app/tools/toolsExportController.js b/src/popup/app/tools/toolsExportController.js index 310bf00894..2962166457 100644 --- a/src/popup/app/tools/toolsExportController.js +++ b/src/popup/app/tools/toolsExportController.js @@ -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(); + } }); }); diff --git a/src/services/crypto.service.ts b/src/services/crypto.service.ts index bd43e17222..3b955e2cdc 100644 --- a/src/services/crypto.service.ts +++ b/src/services/crypto.service.ts @@ -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 { 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 { const storedKey = await this.getKey(); key = key || storedKey;