From 2106e48e0eed36877ecafdf53aad590b5487e44b Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Wed, 31 May 2017 14:49:18 -0400 Subject: [PATCH] move updateKey to cipher service for re-use --- src/app/services/cipherService.js | 53 +++++++++++++++- .../settingsChangePasswordController.js | 63 +++---------------- 2 files changed, 59 insertions(+), 57 deletions(-) diff --git a/src/app/services/cipherService.js b/src/app/services/cipherService.js index 34b8fa793e..df46ab9b4f 100644 --- a/src/app/services/cipherService.js +++ b/src/app/services/cipherService.js @@ -1,7 +1,7 @@ angular .module('bit.services') - .factory('cipherService', function (cryptoService, apiService) { + .factory('cipherService', function (cryptoService, apiService, $q) { var _service = {}; _service.decryptLogins = function (encryptedLogins) { @@ -204,5 +204,56 @@ angular }; }; + _service.updateKey = function (masterPassword, success, error) { + var madeEncKey = cryptoService.makeEncKey(null); + encKey = madeEncKey.encKey; + var encKeyEnc = madeEncKey.encKeyEnc; + + var reencryptedLogins = []; + var loginsPromise = apiService.logins.list({}, function (encryptedLogins) { + var filteredEncryptedLogins = []; + for (var i = 0; i < encryptedLogins.Data.length; i++) { + if (encryptedLogins.Data[i].OrganizationId) { + continue; + } + + filteredEncryptedLogins.push(encryptedLogins.Data[i]); + } + + var unencryptedLogins = _service.decryptLogins(filteredEncryptedLogins); + reencryptedLogins = _service.encryptLogins(unencryptedLogins, encKey); + }).$promise; + + var reencryptedFolders = []; + var foldersPromise = apiService.folders.list({}, function (encryptedFolders) { + var unencryptedFolders = _service.decryptFolders(encryptedFolders.Data); + reencryptedFolders = _service.encryptFolders(unencryptedFolders, encKey); + }).$promise; + + var privateKey = cryptoService.getPrivateKey('raw'), + reencryptedPrivateKey = null; + if (privateKey) { + reencryptedPrivateKey = cryptoService.encrypt(privateKey, encKey, 'raw'); + } + + return $q.all([loginsPromise, foldersPromise]).then(function () { + var request = { + masterPasswordHash: cryptoService.hashPassword(masterPassword), + ciphers: reencryptedLogins, + folders: reencryptedFolders, + privateKey: reencryptedPrivateKey, + key: encKeyEnc + }; + + return apiService.accounts.putKey(request).$promise; + }, error).then(function () { + cryptoService.setEncKey(encKey, null, true); + return success(); + }, function () { + cryptoService.clearEncKey(); + error(); + }); + }; + return _service; }); diff --git a/src/app/settings/settingsChangePasswordController.js b/src/app/settings/settingsChangePasswordController.js index 19e2694651..06d10653bf 100644 --- a/src/app/settings/settingsChangePasswordController.js +++ b/src/app/settings/settingsChangePasswordController.js @@ -2,7 +2,7 @@ .module('bit.settings') .controller('settingsChangePasswordController', function ($scope, $state, apiService, $uibModalInstance, - cryptoService, authService, cipherService, validationService, $q, toastr, $analytics) { + cryptoService, authService, cipherService, validationService, toastr, $analytics) { $analytics.eventTrack('settingsChangePasswordController', { category: 'Modal' }); $scope.save = function (model, form) { @@ -31,61 +31,12 @@ } else { // User is not using an enc key, let's make them one - $scope.savePromise = updateKey(model); + $scope.savePromise = cipherService.updateKey(model.masterPassword, function () { + return changePassword(model); + }, processError); } }; - function updateKey(model) { - var madeEncKey = cryptoService.makeEncKey(null); - encKey = madeEncKey.encKey; - var encKeyEnc = madeEncKey.encKeyEnc; - - var reencryptedLogins = []; - var loginsPromise = apiService.logins.list({}, function (encryptedLogins) { - var filteredEncryptedLogins = []; - for (var i = 0; i < encryptedLogins.Data.length; i++) { - if (encryptedLogins.Data[i].OrganizationId) { - continue; - } - - filteredEncryptedLogins.push(encryptedLogins.Data[i]); - } - - var unencryptedLogins = cipherService.decryptLogins(filteredEncryptedLogins); - reencryptedLogins = cipherService.encryptLogins(unencryptedLogins, encKey); - }).$promise; - - var reencryptedFolders = []; - var foldersPromise = apiService.folders.list({}, function (encryptedFolders) { - var unencryptedFolders = cipherService.decryptFolders(encryptedFolders.Data); - reencryptedFolders = cipherService.encryptFolders(unencryptedFolders, encKey); - }).$promise; - - var privateKey = cryptoService.getPrivateKey('raw'), - reencryptedPrivateKey = null; - if (privateKey) { - reencryptedPrivateKey = cryptoService.encrypt(privateKey, encKey, 'raw'); - } - - return $q.all([loginsPromise, foldersPromise]).then(function () { - var request = { - masterPasswordHash: cryptoService.hashPassword(model.masterPassword), - ciphers: reencryptedLogins, - folders: reencryptedFolders, - privateKey: reencryptedPrivateKey, - key: encKeyEnc - }; - - return apiService.accounts.putKey(request).$promise; - }, error).then(function () { - cryptoService.setEncKey(encKey, null, true); - return changePassword(model); - }, function () { - cryptoService.clearEncKey(); - error(); - }); - } - function changePassword(model) { return authService.getUserProfile().then(function (profile) { var newKey = cryptoService.makeKey(model.newMasterPassword, profile.email.toLowerCase()); @@ -99,17 +50,17 @@ }; return apiService.accounts.putPassword(request).$promise; - }, error).then(function () { + }, processError).then(function () { $uibModalInstance.dismiss('cancel'); authService.logOut(); $analytics.eventTrack('Changed Password'); $state.go('frontend.login.info').then(function () { toastr.success('Please log back in.', 'Master Password Changed'); }); - }, error); + }, processError); } - function error() { + function processError() { $uibModalInstance.dismiss('cancel'); toastr.error('Something went wrong.', 'Oh No!'); }