org keys and optimized org profile load for sidenav

This commit is contained in:
Kyle Spearrin 2017-03-06 23:54:06 -05:00
parent b3c8337f83
commit 0ea4b4400f
6 changed files with 84 additions and 14 deletions

View File

@ -6,6 +6,7 @@ angular
vm.bodyClass = '';
vm.searchVaultText = null;
vm.version = appSettings.version;
vm.userProfile = authService.getUserProfile();
$scope.currentYear = new Date().getFullYear();

View File

@ -1,17 +1,21 @@
angular
.module('bit.global')
.controller('sideNavController', function ($scope, $state, authService, apiService) {
.controller('sideNavController', function ($scope, $state, authService) {
$scope.$state = $state;
$scope.params = $state.params;
if ($state.includes('backend.user')) {
$scope.userProfile = authService.getUserProfile();
}
else if ($state.includes('backend.org')) {
$scope.orgProfile = {};
apiService.organizations.get({ id: $state.params.orgId }, function (response) {
$scope.orgProfile.name = response.Name;
});
if ($state.includes('backend.org')) {
var userProfile = authService.getUserProfile();
if (!userProfile.organizations.length) {
return;
}
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

@ -7,7 +7,13 @@
$scope.confirm = function (user) {
apiService.users.getPublicKey({ id: user.userId }, function (userKey) {
var key = cryptoService.rsaEncrypt('org key', userKey.PublicKey);
var orgKey = cryptoService.getOrgKey($state.params.orgId);
if (!orgKey) {
toastr.error('Unable to confirm user.', 'Error');
return;
}
var key = cryptoService.rsaEncrypt(orgKey, userKey.PublicKey);
apiService.organizationUsers.confirm({ orgId: $state.params.orgId, id: user.id }, { key: key }, function () {
user.status = 2;
toastr.success(user.email + ' has been confirmed.', 'User Confirmed');

View File

@ -92,11 +92,14 @@ angular
for (var i = 0; i < profile.Organizations.length; i++) {
orgs.push({
id: profile.Organizations[i].Id,
name: profile.Organizations[i].Name
name: profile.Organizations[i].Name,
key: profile.Organizations[i].Key,
status: profile.Organizations[i].Status
});
}
_userProfile.organizations = orgs;
cryptoService.setOrgKeys(orgs);
}
}

View File

@ -6,7 +6,8 @@ angular
_key,
_b64Key,
_privateKey,
_publicKey;
_publicKey,
_orgKeys;
_service.setKey = function (key) {
_key = key;
@ -24,6 +25,28 @@ angular
}
};
_service.setOrgKeys = function (orgKeysCt, privateKey) {
if (!orgKeysCt.length) {
return;
}
var orgKeysb64 = {},
_orgKeys = {};
for (var i = 0; i < orgKeysCt.length; i++) {
try {
var orgKey = _service.rsaDecrypt(orgKeysCt[i].key, privateKey);
_orgKeys[orgKeysCt[i].id] = orgKey;
orgKeysb64[orgKeysCt[i].id] = forge.util.encode64(orgKey);
}
catch (e) {
console.log('Cannot set org key ' + i + '. Decryption failed.');
}
}
$sessionStorage.orgKeys = orgKeysb64;
};
_service.getKey = function (b64) {
if (b64 && b64 === true && _b64Key) {
return _b64Key;
@ -86,6 +109,33 @@ angular
return _publicKey;
};
_service.getOrgKeys = function () {
if (_orgKeys) {
return _orgKeys;
}
if ($sessionStorage.orgKeys) {
_orgKeys = {};
for (var orgId in $sessionStorage.orgKeys) {
if ($sessionStorage.orgKeys.hasOwnProperty(orgId)) {
var orgKeyBytes = forge.util.decode64($sessionStorage.orgKeys[orgId]);
_orgKeys[orgId] = orgKeyBytes;
}
}
}
return _orgKeys;
};
_service.getOrgKey = function (orgId) {
var orgKeys = _service.getOrgKeys();
if (!orgKeys || !(orgId in orgKeys)) {
return null;
}
return orgKeys[orgId];
}
_service.clearKey = function () {
_key = _b64Key = null;
delete $sessionStorage.key;
@ -97,9 +147,15 @@ angular
delete $sessionStorage.privateKey;
};
_service.clearOrgKeys = function () {
_orgKeys = {};
delete $sessionStorage.orgKeys;
};
_service.clearKeys = function () {
_service.clearKey();
_service.clearKeyPair();
_service.clearOrgKeys();
};
_service.makeKey = function (password, salt, b64) {

View File

@ -22,11 +22,11 @@
<section class="sidebar">
<div class="user-panel">
<div class="pull-left image">
<img src="//www.gravatar.com/avatar/{{ userProfile.email | gravatar }}.jpg?s=45&d=mm"
<img src="//www.gravatar.com/avatar/{{ main.userProfile.email | gravatar }}.jpg?s=45&d=mm"
class="img-circle" alt="User Image">
</div>
<div class="pull-left info">
<p>{{userProfile.extended && userProfile.extended.name ? userProfile.extended.name : userProfile.email}}</p>
<p>{{main.userProfile.extended && main.userProfile.extended.name ? main.userProfile.extended.name : main.userProfile.email}}</p>
<a ui-sref="frontend.logout">Log Out</a>
</div>
</div>