From 0402ec648dcee91306daadc6cb559592d91d126d Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Tue, 6 Sep 2016 00:22:47 -0400 Subject: [PATCH] Cipher service setup. Bind data for site view. --- src/popup/app/services/cipherService.js | 77 +++++++++++++++++++ src/popup/app/vault/vaultAddSiteController.js | 24 +----- src/popup/app/vault/vaultController.js | 32 +++----- .../app/vault/vaultViewSiteController.js | 13 ++-- src/popup/app/vault/views/vaultViewSite.html | 31 +++++++- src/popup/index.html | 1 + 6 files changed, 127 insertions(+), 51 deletions(-) create mode 100644 src/popup/app/services/cipherService.js diff --git a/src/popup/app/services/cipherService.js b/src/popup/app/services/cipherService.js new file mode 100644 index 00000000..4d2cb5d1 --- /dev/null +++ b/src/popup/app/services/cipherService.js @@ -0,0 +1,77 @@ +angular + .module('bit.services') + + .factory('cipherService', function (cryptoService, $q) { + var _service = {}; + + _service.encryptSite = function (site, callback) { + var model = {}; + + cryptoService.encrypt(site.name, function (nameCipherString) { + model.name = nameCipherString; + cryptoService.encrypt(site.uri, function (uriCipherString) { + model.uri = uriCipherString; + cryptoService.encrypt(site.username, function (usernameCipherString) { + model.username = usernameCipherString; + cryptoService.encrypt(site.password, function (passwordCipherString) { + model.password = passwordCipherString; + cryptoService.encrypt(site.notes, function (notesCipherString) { + model.notes = notesCipherString; + callback(model); + }); + }); + }); + }); + }); + } + + _service.decryptSite = function (site) { + var model = { + id: site.id, + folderId: site.folderId, + favorite: site.favorite + }; + + return $q(function (resolve, reject) { + decrypt(site.name).then(function (obj) { + model.name = obj.val; + return decrypt(site.uri); + }).then(function (obj) { + model.uri = obj.val; + return decrypt(site.username); + }).then(function (obj) { + model.username = obj.val; + return decrypt(site.password); + }).then(function (obj) { + model.password = obj.val; + return decrypt(site.notes); + }).then(function (obj) { + model.notes = obj.val; + resolve(model); + }); + }); + } + + _service.decrypt = decrypt; + + function decrypt(cipherString, index) { + return $q(function (resolve, reject) { + if (!cipherString) { + resolve({ + val: null, + index: index + }); + } + else { + cipherString.decrypt(function (decVal) { + resolve({ + val: decVal, + index: index + }); + }); + } + }); + } + + return _service; + }); diff --git a/src/popup/app/vault/vaultAddSiteController.js b/src/popup/app/vault/vaultAddSiteController.js index a9aaf3a8..2d2b9e75 100644 --- a/src/popup/app/vault/vaultAddSiteController.js +++ b/src/popup/app/vault/vaultAddSiteController.js @@ -1,13 +1,13 @@ angular .module('bit.vault') - .controller('vaultAddSiteController', function ($scope, siteService, cryptoService) { + .controller('vaultAddSiteController', function ($scope, siteService, cipherService) { $scope.site = { folderId: null }; $scope.createSite = function (model) { - encryptSite(model, function (siteModel) { + cipherService.encryptSite(model, function (siteModel) { var site = new Site(siteModel, true); siteService.save(site, function () { $scope.close(); @@ -18,24 +18,4 @@ $scope.close = function () { $scope.parentScope.closeAddSite(); }; - - function encryptSite(model, callback) { - var siteModel = {}; - cryptoService.encrypt(model.name, function (nameCipherString) { - siteModel.name = nameCipherString; - cryptoService.encrypt(model.uri, function (uriCipherString) { - siteModel.uri = uriCipherString; - cryptoService.encrypt(model.username, function (usernameCipherString) { - siteModel.username = usernameCipherString; - cryptoService.encrypt(model.password, function (passwordCipherString) { - siteModel.password = passwordCipherString; - cryptoService.encrypt(model.notes, function (notesCipherString) { - siteModel.notes = notesCipherString; - callback(siteModel); - }); - }); - }); - }); - }); - } }); diff --git a/src/popup/app/vault/vaultController.js b/src/popup/app/vault/vaultController.js index 00b63839..9d2bd624 100644 --- a/src/popup/app/vault/vaultController.js +++ b/src/popup/app/vault/vaultController.js @@ -1,10 +1,11 @@ angular .module('bit.vault') - .controller('vaultController', function ($scope, $ionicModal, siteService, folderService, $q) { + .controller('vaultController', function ($scope, $ionicModal, siteService, folderService, $q, cipherService) { $scope.parentScope = $scope; $scope.sites = []; $scope.folders = []; + $scope.focusedSiteId = null; $scope.$on("$ionicView.enter", function (event, data) { loadVault(); @@ -29,7 +30,7 @@ id: folders[i].id }); - var folderNamePromise = decrypt(sites[i].name, i); + var folderNamePromise = cipherService.decrypt(sites[i].name, i); promises.push(folderNamePromise); folderNamePromise.then(function (obj) { decFolders[obj.index].name = obj.val; @@ -43,13 +44,13 @@ favorite: sites[j].favorite }); - var namePromise = decrypt(sites[j].name, j); + var namePromise = cipherService.decrypt(sites[j].name, j); promises.push(namePromise); namePromise.then(function (obj) { decSites[obj.index].name = obj.val; }); - var usernamePromise = decrypt(sites[j].username, j); + var usernamePromise = cipherService.decrypt(sites[j].username, j); promises.push(usernamePromise); usernamePromise.then(function (obj) { decSites[obj.index].username = obj.val; @@ -64,26 +65,8 @@ }); } - function decrypt(cipherString, index) { - return $q(function (resolve, reject) { - if (!cipherString) { - resolve({ - val: null, - index: index - }); - } - else { - cipherString.decrypt(function (decString) { - resolve({ - val: decString, - index: index - }); - }); - } - }); - } - $scope.viewSite = function (site) { + $scope.focusedSiteId = site.id; $ionicModal.fromTemplateUrl('app/vault/views/vaultViewSite.html', { scope: $scope, animation: 'slide-in-up' @@ -94,6 +77,7 @@ }; $scope.editSite = function (site) { + $scope.focusedSiteId = site.id; $ionicModal.fromTemplateUrl('app/vault/views/vaultEditSite.html', { scope: $scope, animation: 'slide-in-up' @@ -119,10 +103,12 @@ $scope.closeViewSite = function () { $scope.viewSiteModal.hide(); + $scope.focusedSiteId = null; }; $scope.closeEditSite = function () { $scope.editSiteModal.hide(); + $scope.focusedSiteId = null; }; $scope.$on('modal.hidden', function () { diff --git a/src/popup/app/vault/vaultViewSiteController.js b/src/popup/app/vault/vaultViewSiteController.js index 4115e94f..1144f977 100644 --- a/src/popup/app/vault/vaultViewSiteController.js +++ b/src/popup/app/vault/vaultViewSiteController.js @@ -1,13 +1,16 @@ angular .module('bit.vault') - .controller('vaultViewSiteController', function ($scope, siteService, cryptoService) { - $scope.site = { - folderId: null - }; + .controller('vaultViewSiteController', function ($scope, siteService, cipherService) { + $scope.site = null; + siteService.get($scope.parentScope.focusedSiteId, function (site) { + cipherService.decryptSite(site).then(function (model) { + $scope.site = model; + }); + }); $scope.editSite = function () { - + // TODO }; $scope.close = function () { diff --git a/src/popup/app/vault/views/vaultViewSite.html b/src/popup/app/vault/views/vaultViewSite.html index 21f9a4ab..8c1ba4c0 100644 --- a/src/popup/app/vault/views/vaultViewSite.html +++ b/src/popup/app/vault/views/vaultViewSite.html @@ -5,6 +5,35 @@ - View site +
+
+ Site Information +
+
+
Name
+ {{site.name}} +
+
+
Website
+ {{site.uri}} +
+
+
Username
+ {{site.username}} +
+
+
Password
+ {{site.password}} +
+
+ Notes +
+
+ {{site.notes}} +
+
+   +
+
diff --git a/src/popup/index.html b/src/popup/index.html index 2cb721b2..1457247c 100644 --- a/src/popup/index.html +++ b/src/popup/index.html @@ -22,6 +22,7 @@ +