diff --git a/src/app/services/cipherService.js b/src/app/services/cipherService.js index 9e0aff0186..7ee03bdcf8 100644 --- a/src/app/services/cipherService.js +++ b/src/app/services/cipherService.js @@ -312,56 +312,5 @@ angular }; }; - _service.updateKey = function (masterPasswordHash, 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: masterPasswordHash, - 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/settingsChangeEmailController.js b/src/app/settings/settingsChangeEmailController.js index cc740fd424..04cc59087c 100644 --- a/src/app/settings/settingsChangeEmailController.js +++ b/src/app/settings/settingsChangeEmailController.js @@ -2,49 +2,39 @@ .module('bit.settings') .controller('settingsChangeEmailController', function ($scope, $state, apiService, $uibModalInstance, cryptoService, - cipherService, authService, $q, toastr, $analytics) { + authService, toastr, $analytics, validationService) { $analytics.eventTrack('settingsChangeEmailController', { category: 'Modal' }); var _masterPasswordHash, _masterPassword, _newEmail; - $scope.token = function (model) { + $scope.token = function (model, form) { + var encKey = cryptoService.getEncKey(); + if (!encKey) { + validationService.addError(form, null, + 'You cannot change your email until you update your encryption key.', true); + return; + } + _masterPassword = model.masterPassword; _newEmail = model.newEmail.toLowerCase(); - cryptoService.hashPassword(_masterPassword).then(function (hash) { + $scope.tokenPromise = cryptoService.hashPassword(_masterPassword).then(function (hash) { _masterPasswordHash = hash; - var encKey = cryptoService.getEncKey(); - if (encKey) { - $scope.tokenPromise = requestToken(); - } - else { - // User is not using an enc key, let's make them one - $scope.tokenPromise = cipherService.updateKey(_masterPasswordHash, function () { - return requestToken(); - }, function (err) { - toastr.error('Something went wrong.', 'Oh No!'); - }); - } + var request = { + newEmail: _newEmail, + masterPasswordHash: _masterPasswordHash + }; + + return apiService.accounts.emailToken(request, function () { + $scope.tokenSent = true; + }).$promise; }); }; - function requestToken() { - var request = { - newEmail: _newEmail, - masterPasswordHash: _masterPasswordHash - }; - - return apiService.accounts.emailToken(request, function () { - $scope.tokenSent = true; - }).$promise; - } - $scope.confirm = function (model) { - $scope.processing = true; - $scope.confirmPromise = cryptoService.makeKeyAndHash(_newEmail, _masterPassword).then(function (result) { var encKey = cryptoService.getEncKey(); var newEncKey = cryptoService.encrypt(encKey.key, result.key, 'raw'); @@ -64,9 +54,6 @@ return $state.go('frontend.login.info'); }).then(function () { toastr.success('Please log back in.', 'Email Changed'); - }, function () { - $uibModalInstance.dismiss('cancel'); - toastr.error('Something went wrong. Try again.', 'Oh No!'); }); }; diff --git a/src/app/settings/settingsChangePasswordController.js b/src/app/settings/settingsChangePasswordController.js index d073c913ac..247a99fa85 100644 --- a/src/app/settings/settingsChangePasswordController.js +++ b/src/app/settings/settingsChangePasswordController.js @@ -2,12 +2,19 @@ .module('bit.settings') .controller('settingsChangePasswordController', function ($scope, $state, apiService, $uibModalInstance, - cryptoService, authService, cipherService, validationService, toastr, $analytics) { + cryptoService, authService, validationService, toastr, $analytics) { $analytics.eventTrack('settingsChangePasswordController', { category: 'Modal' }); $scope.save = function (model, form) { var error = false; + var encKey = cryptoService.getEncKey(); + if (!encKey) { + validationService.addError(form, null, + 'You cannot change your master password until you update your encryption key.', true); + error = true; + } + if ($scope.model.newMasterPassword.length < 8) { validationService.addError(form, 'NewMasterPasswordHash', 'Master password must be at least 8 characters long.', true); @@ -23,27 +30,8 @@ return; } - $scope.processing = true; - - var encKey = cryptoService.getEncKey(); - if (encKey) { - $scope.savePromise = changePassword(model); - } - else { - // User is not using an enc key, let's make them one - $scope.savePromise = cryptoService.hashPassword(model.masterPassword).then(function (hash) { - return cipherService.updateKey(hash); - }).then(function () { - return changePassword(model); - }, function (err) { - toastr.error('Something went wrong.', 'Oh No!'); - }); - } - }; - - function changePassword(model) { var makeResult; - return authService.getUserProfile().then(function (profile) { + $scope.savePromise = authService.getUserProfile().then(function (profile) { return cryptoService.makeKeyAndHash(profile.email, model.newMasterPassword); }).then(function (result) { makeResult = result; @@ -66,11 +54,8 @@ return $state.go('frontend.login.info'); }).then(function () { toastr.success('Please log back in.', 'Master Password Changed'); - }, function () { - $uibModalInstance.dismiss('cancel'); - toastr.error('Something went wrong.', 'Oh No!'); }); - } + }; $scope.close = function () { $uibModalInstance.dismiss('cancel'); diff --git a/src/app/settings/settingsUpdateKeyController.js b/src/app/settings/settingsUpdateKeyController.js index 1672e51b81..b05bf85264 100644 --- a/src/app/settings/settingsUpdateKeyController.js +++ b/src/app/settings/settingsUpdateKeyController.js @@ -1,8 +1,8 @@ angular .module('bit.settings') - .controller('settingsUpdateKeyController', function ($scope, $state, apiService, $uibModalInstance, - cryptoService, authService, cipherService, validationService, toastr, $analytics) { + .controller('settingsUpdateKeyController', function ($scope, $state, apiService, $uibModalInstance, cipherService, + cryptoService, authService, validationService, toastr, $analytics, $q) { $analytics.eventTrack('settingsUpdateKeyController', { category: 'Modal' }); $scope.save = function (form) { @@ -13,27 +13,68 @@ return; } - $scope.processing = true; $scope.savePromise = cryptoService.hashPassword($scope.masterPassword).then(function (hash) { - return cipherService.updateKey(hash, function () { - return null; - }, function () { - return null; - }); + return updateKey(hash); }).then(function () { $uibModalInstance.dismiss('cancel'); authService.logOut(); $analytics.eventTrack('Key Updated'); return $state.go('frontend.login.info'); + }, function (e) { + throw e ? e : 'Error occurred.'; }).then(function () { toastr.success('Please log back in. If you are using other bitwarden applications, ' + 'log out and back in to those as well.', 'Key Updated', { timeOut: 10000 }); - }, function () { - $uibModalInstance.dismiss('cancel'); - toastr.error('Something went wrong.', 'Oh No!'); }); }; + function updateKey(masterPasswordHash) { + var madeEncKey = cryptoService.makeEncKey(null); + + 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, madeEncKey.encKey); + }).$promise; + + var reencryptedFolders = []; + var foldersPromise = apiService.folders.list({}, function (encryptedFolders) { + var unencryptedFolders = cipherService.decryptFolders(encryptedFolders.Data); + reencryptedFolders = cipherService.encryptFolders(unencryptedFolders, madeEncKey.encKey); + }).$promise; + + var privateKey = cryptoService.getPrivateKey('raw'), + reencryptedPrivateKey = null; + if (privateKey) { + reencryptedPrivateKey = cryptoService.encrypt(privateKey, madeEncKey.encKey, 'raw'); + } + + return $q.all([loginsPromise, foldersPromise]).then(function () { + var request = { + masterPasswordHash: masterPasswordHash, + ciphers: reencryptedLogins, + folders: reencryptedFolders, + privateKey: reencryptedPrivateKey, + key: madeEncKey.encKeyEnc + }; + + return apiService.accounts.putKey(request).$promise; + }, function () { + throw 'Error while encrypting data.'; + }).then(function () { + cryptoService.setEncKey(madeEncKey.encKey, null, true); + }); + } + $scope.close = function () { $uibModalInstance.dismiss('cancel'); }; diff --git a/src/app/settings/views/settingsChangeEmail.html b/src/app/settings/views/settingsChangeEmail.html index 8d6745f01e..3f80f13168 100644 --- a/src/app/settings/views/settingsChangeEmail.html +++ b/src/app/settings/views/settingsChangeEmail.html @@ -2,7 +2,8 @@ -
+
-
+
- diff --git a/src/app/settings/views/settingsUpdateKey.html b/src/app/settings/views/settingsUpdateKey.html index e2f9e72485..fb78e10c88 100644 --- a/src/app/settings/views/settingsUpdateKey.html +++ b/src/app/settings/views/settingsUpdateKey.html @@ -2,7 +2,7 @@ -
+
-