From 6f142c36823a1ff8416f6f39615a5007c69a4955 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Thu, 15 Sep 2016 21:09:56 -0400 Subject: [PATCH] Moved vault to cached folders/sites --- src/models/domainModels.js | 16 ++++++ src/popup/app/vault/vaultController.js | 72 ++++++++------------------ src/services/folderService.js | 53 +++++++++++++++++++ src/services/siteService.js | 28 ++++++++++ 4 files changed, 118 insertions(+), 51 deletions(-) diff --git a/src/models/domainModels.js b/src/models/domainModels.js index 3791e98e..f98ee991 100644 --- a/src/models/domainModels.js +++ b/src/models/domainModels.js @@ -110,4 +110,20 @@ var Folder = function (obj, alreadyEncrypted) { return deferred.promise; }; + + Folder.prototype.decrypt = function () { + var self = this; + var model = { + id: self.id + }; + + var deferred = Q.defer(); + + self.name.decryptWithPromise().then(function (val) { + model.name = val; + deferred.resolve(model); + }); + + return deferred.promise; + }; }(); diff --git a/src/popup/app/vault/vaultController.js b/src/popup/app/vault/vaultController.js index d56d2eaa..3352bfec 100644 --- a/src/popup/app/vault/vaultController.js +++ b/src/popup/app/vault/vaultController.js @@ -23,61 +23,31 @@ } function loadVault() { - var promises = []; + var decFolders = []; var decSites = []; - var decFolders = [{ - id: null, - name: '(none)' - }]; + var promises = []; - folderService.getAll(function (folders) { - siteService.getAll(function (sites) { - for (var i = 1; i < folders.length; i++) { - decFolders.push({ - id: folders[i].id - }); + var folderPromise = $q.when(folderService.getAllDecrypted()); + folderPromise.then(function (folders) { + decFolders = folders; + }); + promises.push(folderPromise); - var folderNamePromise = cipherService.decrypt(folders[i].name, i); - promises.push(folderNamePromise); - folderNamePromise.then(function (obj) { - decFolders[obj.index].name = obj.val; - }); - } + var sitePromise = $q.when(siteService.getAllDecrypted()); + sitePromise.then(function (sites) { + decSites = sites; + }); + promises.push(sitePromise); - for (var j = 0; j < sites.length; j++) { - decSites.push({ - id: sites[j].id, - folderId: sites[j].folderId, - favorite: sites[j].favorite - }); - - var namePromise = cipherService.decrypt(sites[j].name, j); - promises.push(namePromise); - namePromise.then(function (obj) { - decSites[obj.index].name = obj.val; - }); - - var usernamePromise = cipherService.decrypt(sites[j].username, j); - promises.push(usernamePromise); - usernamePromise.then(function (obj) { - decSites[obj.index].username = obj.val; - }); - - var passwordPromise = cipherService.decrypt(sites[j].password, j); - promises.push(passwordPromise); - passwordPromise.then(function (obj) { - decSites[obj.index].password = obj.val; - }); - } - - $q.all(promises).then(function () { - $rootScope.vaultSites = decSites; - $rootScope.vaultFolders = decFolders; - if (!delayLoad) { - setScrollY(); - } - }); - }); + $q.all(promises).then(function () { + $rootScope.vaultFolders = decFolders.concat([{ + id: null, + name: '(none)' + }]); + $rootScope.vaultSites = decSites; + if (!delayLoad) { + setScrollY(); + } }); } diff --git a/src/services/folderService.js b/src/services/folderService.js index 1773ac47..5d7a1144 100644 --- a/src/services/folderService.js +++ b/src/services/folderService.js @@ -2,11 +2,37 @@ this.cryptoService = cryptoService; this.userService = userService; this.apiService = apiService; + this.decryptedFolderCache = null; initFolderService(); }; function initFolderService() { + FolderService.prototype.encrypt = function (folder) { + var model = { + id: folder.id + }; + + var deferred = Q.defer(); + + encryptWithPromise(folder.name).then(function (cs) { + model.name = cs; + deferred.resolve(model); + }); + + return deferred.promise; + }; + + function encryptWithPromise(plaintextString) { + var deferred = Q.defer(); + + cryptoService.encrypt(plaintextString, function (cipherString) { + deferred.resolve(cipherString); + }); + + return deferred.promise; + } + FolderService.prototype.get = function (id, callback) { if (!callback || typeof callback !== 'function') { throw 'callback function required'; @@ -48,6 +74,33 @@ function initFolderService() { }); }; + FolderService.prototype.getAllDecrypted = function () { + var deferred = Q.defer(); + + var self = this; + if (self.decryptedFolderCache) { + deferred.resolve(self.decryptedFolderCache); + return deferred.promise; + } + + var promises = []; + var decFolders = []; + self.getAll(function (folders) { + for (var i = 0; i < folders.length; i++) { + promises.push(folders[i].decrypt().then(function (folder) { + decFolders.push(folder); + })); + } + + Q.all(promises).then(function () { + self.decryptedFolderCache = decFolders; + deferred.resolve(self.decryptedFolderCache); + }); + }); + + return deferred.promise; + }; + FolderService.prototype.saveWithServer = function (folder, callback) { if (!callback || typeof callback !== 'function') { throw 'callback function required'; diff --git a/src/services/siteService.js b/src/services/siteService.js index b0034809..78105177 100644 --- a/src/services/siteService.js +++ b/src/services/siteService.js @@ -2,6 +2,7 @@ this.cryptoService = cryptoService; this.userService = userService; this.apiService = apiService; + this.decryptedSiteCache = null; initSiteService(); }; @@ -90,6 +91,33 @@ function initSiteService() { }); }; + SiteService.prototype.getAllDecrypted = function () { + var deferred = Q.defer(); + + var self = this; + if (self.decryptedSiteCache) { + deferred.resolve(self.decryptedSiteCache); + return deferred.promise; + } + + var promises = []; + var decSites = []; + self.getAll(function (sites) { + for (var i = 0; i < sites.length; i++) { + promises.push(sites[i].decrypt().then(function (site) { + decSites.push(site); + })); + } + + Q.all(promises).then(function () { + self.decryptedSiteCache = decSites; + deferred.resolve(self.decryptedSiteCache); + }); + }); + + return deferred.promise; + }; + SiteService.prototype.saveWithServer = function (site, successCallback, errorCallback) { var self = this, request = new SiteRequest(site);