From 0ea4b4400f22aeaa1f411d74e3e5851e668355a8 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Mon, 6 Mar 2017 23:54:06 -0500 Subject: [PATCH] org keys and optimized org profile load for sidenav --- src/app/global/mainController.js | 1 + src/app/global/sideNavController.js | 22 ++++--- .../organizationPeopleController.js | 8 ++- src/app/services/authService.js | 5 +- src/app/services/cryptoService.js | 58 ++++++++++++++++++- src/app/views/userLayout.html | 4 +- 6 files changed, 84 insertions(+), 14 deletions(-) diff --git a/src/app/global/mainController.js b/src/app/global/mainController.js index 3c41090291..d8db0a517c 100644 --- a/src/app/global/mainController.js +++ b/src/app/global/mainController.js @@ -6,6 +6,7 @@ angular vm.bodyClass = ''; vm.searchVaultText = null; vm.version = appSettings.version; + vm.userProfile = authService.getUserProfile(); $scope.currentYear = new Date().getFullYear(); diff --git a/src/app/global/sideNavController.js b/src/app/global/sideNavController.js index 575c5759ff..9243f6eb87 100644 --- a/src/app/global/sideNavController.js +++ b/src/app/global/sideNavController.js @@ -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; + } + } } }); diff --git a/src/app/organization/organizationPeopleController.js b/src/app/organization/organizationPeopleController.js index fd51d9af9b..e69fab78b9 100644 --- a/src/app/organization/organizationPeopleController.js +++ b/src/app/organization/organizationPeopleController.js @@ -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'); diff --git a/src/app/services/authService.js b/src/app/services/authService.js index ddb06ff206..5af23b5c61 100644 --- a/src/app/services/authService.js +++ b/src/app/services/authService.js @@ -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); } } diff --git a/src/app/services/cryptoService.js b/src/app/services/cryptoService.js index e7095e368b..4e4aa035a4 100644 --- a/src/app/services/cryptoService.js +++ b/src/app/services/cryptoService.js @@ -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) { diff --git a/src/app/views/userLayout.html b/src/app/views/userLayout.html index 44d823cfd6..49ad5e6226 100644 --- a/src/app/views/userLayout.html +++ b/src/app/views/userLayout.html @@ -22,11 +22,11 @@