1
0
mirror of https://github.com/bitwarden/web synced 2025-12-30 15:13:21 +00:00
Files
web/src/app/settings/settingsChangePasswordController.js
2017-07-31 22:53:27 -04:00

64 lines
2.5 KiB
JavaScript

angular
.module('bit.settings')
.controller('settingsChangePasswordController', function ($scope, $state, apiService, $uibModalInstance,
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);
error = true;
}
if ($scope.model.newMasterPassword !== $scope.model.confirmNewMasterPassword) {
validationService.addError(form, 'ConfirmNewMasterPasswordHash',
'New master password confirmation does not match.', true);
error = true;
}
if (error) {
return;
}
var makeResult;
$scope.savePromise = authService.getUserProfile().then(function (profile) {
return cryptoService.makeKeyAndHash(profile.email, model.newMasterPassword);
}).then(function (result) {
makeResult = result;
return cryptoService.hashPassword(model.masterPassword);
}).then(function (hash) {
var encKey = cryptoService.getEncKey();
var newEncKey = cryptoService.encrypt(encKey.key, makeResult.key, 'raw');
var request = {
masterPasswordHash: hash,
newMasterPasswordHash: makeResult.hash,
key: newEncKey
};
return apiService.accounts.putPassword(request).$promise;
}).then(function () {
$uibModalInstance.dismiss('cancel');
authService.logOut();
$analytics.eventTrack('Changed Password');
return $state.go('frontend.login.info');
}).then(function () {
toastr.success('Please log back in.', 'Master Password Changed');
});
};
$scope.close = function () {
$uibModalInstance.dismiss('cancel');
};
});