1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-10-31 08:20:37 +01:00

convert auth service profile methods to promises

This commit is contained in:
Kyle Spearrin 2017-03-25 10:43:19 -04:00
parent 2154607d11
commit 19203e976b
8 changed files with 162 additions and 145 deletions

View File

@ -6,34 +6,31 @@ angular
require: 'ngModel', require: 'ngModel',
restrict: 'A', restrict: 'A',
link: function (scope, elem, attr, ngModel) { link: function (scope, elem, attr, ngModel) {
var profile = authService.getUserProfile(); authService.getUserProfile().then(function (profile) {
if (!profile) { // For DOM -> model validation
return; ngModel.$parsers.unshift(function (value) {
} if (!value) {
return undefined;
}
// For DOM -> model validation var key = cryptoService.makeKey(value, profile.email, true);
ngModel.$parsers.unshift(function (value) { var valid = key === cryptoService.getKey(true);
if (!value) { ngModel.$setValidity('masterPassword', valid);
return undefined; return valid ? value : undefined;
} });
var key = cryptoService.makeKey(value, profile.email, true); // For model -> DOM validation
var valid = key === cryptoService.getKey(true); ngModel.$formatters.unshift(function (value) {
ngModel.$setValidity('masterPassword', valid); if (!value) {
return valid ? value : undefined; return undefined;
}); }
// For model -> DOM validation var key = cryptoService.makeKey(value, profile.email, true);
ngModel.$formatters.unshift(function (value) { var valid = key === cryptoService.getKey(true);
if (!value) {
return undefined;
}
var key = cryptoService.makeKey(value, profile.email, true); ngModel.$setValidity('masterPassword', valid);
var valid = key === cryptoService.getKey(true); return value;
});
ngModel.$setValidity('masterPassword', valid);
return value;
}); });
} }
}; };

View File

@ -10,7 +10,9 @@ angular
$scope.currentYear = new Date().getFullYear(); $scope.currentYear = new Date().getFullYear();
$scope.$on('$viewContentLoaded', function () { $scope.$on('$viewContentLoaded', function () {
vm.userProfile = authService.getUserProfile(); authService.getUserProfile().then(function (profile) {
vm.userProfile = profile;
});
if ($.AdminLTE) { if ($.AdminLTE) {
if ($.AdminLTE.layout) { if ($.AdminLTE.layout) {

View File

@ -6,16 +6,17 @@ angular
$scope.params = $state.params; $scope.params = $state.params;
if ($state.includes('backend.org')) { if ($state.includes('backend.org')) {
var userProfile = authService.getUserProfile(); authService.getUserProfile().then(function (userProfile) {
if (!userProfile.organizations || !userProfile.organizations.length) { if (!userProfile.organizations || !userProfile.organizations.length) {
return; return;
}
for (var i = 0; i < userProfile.organizations.length; i++) {
if (userProfile.organizations[i].id === $state.params.orgId) {
$scope.orgProfile = userProfile.organizations[i];
break;
} }
}
for (var i = 0; i < userProfile.organizations.length; i++) {
if (userProfile.organizations[i].id === $state.params.orgId) {
$scope.orgProfile = userProfile.organizations[i];
break;
}
}
});
} }
}); });

View File

@ -58,16 +58,21 @@ angular
_service.getUserProfile = function () { _service.getUserProfile = function () {
if (!_userProfile) { if (!_userProfile) {
_service.setUserProfile(); return _service.setUserProfile();
} }
return _userProfile; var deferred = $q.defer();
deferred.resolve(_userProfile);
return deferred.promise;
}; };
_service.setUserProfile = function () { _service.setUserProfile = function () {
var deferred = $q.defer();
var token = tokenService.getToken(); var token = tokenService.getToken();
if (!token) { if (!token) {
return; deferred.reject();
return deferred.promise;
} }
var decodedToken = jwtHelper.decodeToken(token); var decodedToken = jwtHelper.decodeToken(token);
@ -77,52 +82,56 @@ angular
email: decodedToken.email email: decodedToken.email
}; };
apiService.accounts.getProfile({}, loadProfile); apiService.accounts.getProfile({}, function (profile) {
_userProfile.extended = {
name: profile.Name,
twoFactorEnabled: profile.TwoFactorEnabled,
culture: profile.Culture
};
if (profile.Organizations) {
var orgs = [];
for (var i = 0; i < profile.Organizations.length; i++) {
orgs.push({
id: profile.Organizations[i].Id,
name: profile.Organizations[i].Name,
key: profile.Organizations[i].Key,
status: profile.Organizations[i].Status
});
}
_userProfile.organizations = orgs;
cryptoService.setOrgKeys(orgs);
deferred.resolve(_userProfile);
}
}, function () {
deferred.reject();
});
return deferred.promise;
}; };
_service.addProfileOrganization = function (org) { _service.addProfileOrganization = function (org) {
var profile = _service.getUserProfile(); return _service.getUserProfile().then(function (profile) {
if (profile) { if (profile) {
if (!profile.Organizations) { if (!profile.Organizations) {
profile.Organizations = []; profile.Organizations = [];
}
var o = {
id: org.Id,
name: org.Name,
key: org.Key,
status: org.Status
};
profile.organizations.push(o);
_userProfile = profile;
cryptoService.addOrgKey(o);
} }
});
var o = {
id: org.Id,
name: org.Name,
key: org.Key,
status: org.Status
};
profile.organizations.push(o);
_userProfile = profile;
cryptoService.addOrgKey(o);
}
}; };
function loadProfile(profile) {
_userProfile.extended = {
name: profile.Name,
twoFactorEnabled: profile.TwoFactorEnabled,
culture: profile.Culture
};
if (profile.Organizations) {
var orgs = [];
for (var i = 0; i < profile.Organizations.length; i++) {
orgs.push({
id: profile.Organizations[i].Id,
name: profile.Organizations[i].Name,
key: profile.Organizations[i].Key,
status: profile.Organizations[i].Status
});
}
_userProfile.organizations = orgs;
cryptoService.setOrgKeys(orgs);
}
}
_service.isAuthenticated = function () { _service.isAuthenticated = function () {
return tokenService.getToken() !== null; return tokenService.getToken() !== null;
}; };

View File

@ -24,40 +24,42 @@
$scope.processing = true; $scope.processing = true;
var profile = authService.getUserProfile(); authService.getUserProfile().then(function (profile) {
var newKey = cryptoService.makeKey(model.newMasterPassword, profile.email.toLowerCase()); return cryptoService.makeKey(model.newMasterPassword, profile.email.toLowerCase());
}).then(function (newKey) {
var reencryptedLogins = []; var reencryptedLogins = [];
var loginsPromise = apiService.logins.list({ dirty: false }, function (encryptedLogins) { var loginsPromise = apiService.logins.list({ dirty: false }, function (encryptedLogins) {
var unencryptedLogins = cipherService.decryptLogins(encryptedLogins.Data); var unencryptedLogins = cipherService.decryptLogins(encryptedLogins.Data);
reencryptedLogins = cipherService.encryptLogins(unencryptedLogins, newKey); reencryptedLogins = cipherService.encryptLogins(unencryptedLogins, newKey);
}).$promise;
var reencryptedFolders = [];
var foldersPromise = apiService.folders.list({ dirty: false }, function (encryptedFolders) {
var unencryptedFolders = cipherService.decryptFolders(encryptedFolders.Data);
reencryptedFolders = cipherService.encryptFolders(unencryptedFolders, newKey);
}).$promise;
$q.all([loginsPromise, foldersPromise]).then(function () {
var request = {
masterPasswordHash: cryptoService.hashPassword(model.masterPassword),
newMasterPasswordHash: cryptoService.hashPassword(model.newMasterPassword, newKey),
ciphers: reencryptedLogins.concat(reencryptedFolders)
};
$scope.savePromise = apiService.accounts.putPassword(request, 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');
});
}, function () {
// TODO: recovery mode
$uibModalInstance.dismiss('cancel');
toastr.error('Something went wrong.', 'Oh No!');
}).$promise; }).$promise;
var reencryptedFolders = [];
var foldersPromise = apiService.folders.list({ dirty: false }, function (encryptedFolders) {
var unencryptedFolders = cipherService.decryptFolders(encryptedFolders.Data);
reencryptedFolders = cipherService.encryptFolders(unencryptedFolders, newKey);
}).$promise;
$q.all([loginsPromise, foldersPromise]).then(function () {
var request = {
masterPasswordHash: cryptoService.hashPassword(model.masterPassword),
newMasterPasswordHash: cryptoService.hashPassword(model.newMasterPassword, newKey),
ciphers: reencryptedLogins.concat(reencryptedFolders)
};
$scope.savePromise = apiService.accounts.putPassword(request, 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');
});
}, function () {
// TODO: recovery mode
$uibModalInstance.dismiss('cancel');
toastr.error('Something went wrong.', 'Oh No!');
}).$promise;
});
}); });
}; };

View File

@ -36,15 +36,17 @@
$scope.generalSave = function () { $scope.generalSave = function () {
$scope.generalPromise = apiService.accounts.putProfile({}, $scope.model.profile, function (profile) { $scope.generalPromise = apiService.accounts.putProfile({}, $scope.model.profile, function (profile) {
authService.setUserProfile(profile); authService.setUserProfile(profile).then(function (updatedProfile) {
toastr.success('Account has been updated.', 'Success!'); toastr.success('Account has been updated.', 'Success!');
});
}).$promise; }).$promise;
}; };
$scope.passwordHintSave = function () { $scope.passwordHintSave = function () {
$scope.passwordHintPromise = apiService.accounts.putProfile({}, $scope.model.profile, function (profile) { $scope.passwordHintPromise = apiService.accounts.putProfile({}, $scope.model.profile, function (profile) {
authService.setUserProfile(profile); authService.setUserProfile(profile).then(function (updatedProfile) {
toastr.success('Account has been updated.', 'Success!'); toastr.success('Account has been updated.', 'Success!');
});
}).$promise; }).$promise;
}; };

View File

@ -4,13 +4,16 @@
.controller('settingsTwoFactorController', function ($scope, apiService, $uibModalInstance, cryptoService, authService, $q, toastr, $analytics) { .controller('settingsTwoFactorController', function ($scope, apiService, $uibModalInstance, cryptoService, authService, $q, toastr, $analytics) {
$analytics.eventTrack('settingsTwoFactorController', { category: 'Modal' }); $analytics.eventTrack('settingsTwoFactorController', { category: 'Modal' });
var _issuer = 'bitwarden', var _issuer = 'bitwarden',
_profile = authService.getUserProfile(), _profile = null,
_masterPasswordHash; _masterPasswordHash;
$scope.account = _profile.email; authService.getUserProfile().then(function (profile) {
$scope.enabled = function () { _profile = profile;
return _profile.extended && _profile.extended.twoFactorEnabled; $scope.account = _profile.email;
}; $scope.enabled = function () {
return _profile.extended && _profile.extended.twoFactorEnabled;
};
});
$scope.auth = function (model) { $scope.auth = function (model) {
_masterPasswordHash = cryptoService.hashPassword(model.masterPassword); _masterPasswordHash = cryptoService.hashPassword(model.masterPassword);

View File

@ -12,33 +12,34 @@
$scope.login = cipherService.decryptLogin(login); $scope.login = cipherService.decryptLogin(login);
}); });
var profile = authService.getUserProfile(); authService.getUserProfile().then(function (profile) {
if (profile && profile.organizations) { if (profile && profile.organizations) {
var orgs = []; var orgs = [];
for (var i = 0; i < profile.organizations.length; i++) { for (var i = 0; i < profile.organizations.length; i++) {
orgs.push({ orgs.push({
id: profile.organizations[i].id, id: profile.organizations[i].id,
name: profile.organizations[i].name name: profile.organizations[i].name
});
if (i === 0) {
$scope.model.organizationId = profile.organizations[i].id;
}
}
$scope.organizations = orgs;
apiService.subvaults.listMe(function (response) {
var subvaults = [];
for (var i = 0; i < response.Data.length; i++) {
var decSubvault = cipherService.decryptSubvault(response.Data[i]);
decSubvault.organizationId = response.Data[i].OrganizationId;
subvaults.push(decSubvault);
}
$scope.subvaults = subvaults;
}); });
if (i === 0) {
$scope.model.organizationId = profile.organizations[i].id;
}
} }
});
$scope.organizations = orgs;
apiService.subvaults.listMe(function (response) {
var subvaults = [];
for (var i = 0; i < response.Data.length; i++) {
var decSubvault = cipherService.decryptSubvault(response.Data[i]);
decSubvault.organizationId = response.Data[i].OrganizationId;
subvaults.push(decSubvault);
}
$scope.subvaults = subvaults;
});
}
$scope.submitPromise = null; $scope.submitPromise = null;
$scope.submit = function (model) { $scope.submit = function (model) {