From 16a59f8d09490a6f45459818e73a21153cf418a3 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Thu, 15 Sep 2016 00:09:48 -0400 Subject: [PATCH] added q promise library. moving cipher service functions out into application services and domain models. --- gulpfile.js | 4 + package.json | 3 +- src/manifest.json | 1 + src/models/domainModels.js | 87 +++++++++++++++---- src/popup/app/services/cipherService.js | 60 +------------ src/popup/app/vault/vaultAddSiteController.js | 2 - .../app/vault/vaultEditSiteController.js | 2 - src/services/siteService.js | 39 +++++++++ 8 files changed, 121 insertions(+), 77 deletions(-) diff --git a/gulpfile.js b/gulpfile.js index f1c844a953..567bd39a0c 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -96,6 +96,10 @@ gulp.task('lib', ['clean:lib'], function () { { src: paths.npmDir + 'clipboard/dist/clipboard.js', dest: paths.libDir + 'clipboard' + }, + { + src: paths.npmDir + 'q/q.js', + dest: paths.libDir + 'q' } ]; diff --git a/package.json b/package.json index 49a57c22cb..49676fc611 100644 --- a/package.json +++ b/package.json @@ -23,6 +23,7 @@ "ngclipboard": "1.1.1", "clipboard": "1.5.12", "merge-stream": "1.0.0", - "angular-toastr": "2.1.1" + "angular-toastr": "2.1.1", + "q": "1.4.1" } } diff --git a/src/manifest.json b/src/manifest.json index 376667f414..167974ebc9 100644 --- a/src/manifest.json +++ b/src/manifest.json @@ -30,6 +30,7 @@ "lib/sjcl/sjcl.js", "lib/sjcl/cbc.js", "lib/sjcl/bitArray.js", + "lib/q/q.js", "scripts/tld.js", "models/api/requestModels.js", "models/api/responseModels.js", diff --git a/src/models/domainModels.js b/src/models/domainModels.js index ba6f5ce7be..3791e98e1e 100644 --- a/src/models/domainModels.js +++ b/src/models/domainModels.js @@ -8,21 +8,6 @@ var CipherString = function (encryptedString) { } }; -!function () { - CipherString.prototype.decrypt = function (callback) { - if (!this.decryptedValue) { - var cryptoService = chrome.extension.getBackgroundPage().cryptoService; - cryptoService.decrypt(this, function (decValue) { - this.decryptedValue = decValue; - callback(this.decryptedValue); - }); - } - else { - callback(this.decryptedValue); - } - }; -}(); - var Site = function (obj, alreadyEncrypted) { this.id = obj.id ? obj.id : null; this.folderId = obj.folderId ? obj.folderId : null; @@ -54,3 +39,75 @@ var Folder = function (obj, alreadyEncrypted) { this.name = obj.name ? new CipherString(obj.name) : null; } }; + +!function () { + CipherString.prototype.decrypt = function (callback) { + if (!this.decryptedValue) { + var cryptoService = chrome.extension.getBackgroundPage().cryptoService; + cryptoService.decrypt(this, function (decValue) { + this.decryptedValue = decValue; + callback(this.decryptedValue); + }); + } + else { + callback(this.decryptedValue); + } + }; + + CipherString.prototype.decryptWithPromise = function () { + var deferred = Q.defer(); + + if (!this) { + deferred.resolve(null); + } + else { + this.decrypt(function (decVal) { + deferred.resolve(decVal); + }); + } + + return deferred.promise; + } + + Site.prototype.decrypt = function () { + var self = this; + var model = { + id: self.id, + folderId: self.folderId, + favorite: self.favorite + }; + + var deferred = Q.defer(); + + self.name.decryptWithPromise().then(function (val) { + model.name = val; + if (self.uri) { + return self.uri.decryptWithPromise(); + } + return null; + }).then(function (val) { + model.uri = val; + if (self.username) { + return self.username.decryptWithPromise(); + } + return null; + }).then(function (val) { + model.username = val; + if (self.password) { + return self.password.decryptWithPromise(); + } + return null; + }).then(function (val) { + model.password = val; + if (self.notes) { + return self.notes.decryptWithPromise(); + } + return null; + }).then(function (val) { + model.notes = val; + deferred.resolve(model); + }); + + return deferred.promise; + }; +}(); diff --git a/src/popup/app/services/cipherService.js b/src/popup/app/services/cipherService.js index c4cfd865fa..9184731930 100644 --- a/src/popup/app/services/cipherService.js +++ b/src/popup/app/services/cipherService.js @@ -1,61 +1,15 @@ angular .module('bit.services') - .factory('cipherService', function (cryptoService, $q) { + .factory('cipherService', function ($q, siteService) { var _service = {}; _service.encryptSite = function (site) { - var model = { - id: site.id, - folderId: site.folderId, - favorite: site.favorite - }; - - return $q(function (resolve, reject) { - encrypt(site.name).then(function (cs) { - model.name = cs; - return encrypt(site.uri); - }).then(function (cs) { - model.uri = cs; - return encrypt(site.username); - }).then(function (cs) { - model.username = cs; - return encrypt(site.password); - }).then(function (cs) { - model.password = cs; - return encrypt(site.notes); - }).then(function (cs) { - model.notes = cs; - resolve(model); - }); - }); + return $q.when(siteService.encrypt(site)); }; _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); - }); - }); + return $q.when(site.decrypt()); }; _service.decrypt = decrypt; @@ -79,13 +33,5 @@ }); } - function encrypt(plaintextString) { - return $q(function (resolve, reject) { - cryptoService.encrypt(plaintextString, function (cipherString) { - resolve(cipherString); - }); - }); - } - return _service; }); diff --git a/src/popup/app/vault/vaultAddSiteController.js b/src/popup/app/vault/vaultAddSiteController.js index 7547b66984..49d0c0e54b 100644 --- a/src/popup/app/vault/vaultAddSiteController.js +++ b/src/popup/app/vault/vaultAddSiteController.js @@ -48,8 +48,6 @@ $scope.save = function (model) { $scope.savePromise = cipherService.encryptSite(model).then(function (siteModel) { var site = new Site(siteModel, true); - return site; - }).then(function (site) { return saveSite(site).then(function (site) { toastr.success('Added site'); $scope.close(); diff --git a/src/popup/app/vault/vaultEditSiteController.js b/src/popup/app/vault/vaultEditSiteController.js index 0ffb422ae4..77f7221a36 100644 --- a/src/popup/app/vault/vaultEditSiteController.js +++ b/src/popup/app/vault/vaultEditSiteController.js @@ -45,8 +45,6 @@ angular $scope.save = function (model) { $scope.savePromise = cipherService.encryptSite(model).then(function (siteModel) { var site = new Site(siteModel, true); - return site; - }).then(function (site) { return saveSite(site).then(function (site) { toastr.success('Edited site'); $scope.close(); diff --git a/src/services/siteService.js b/src/services/siteService.js index 643e60eb52..b003480928 100644 --- a/src/services/siteService.js +++ b/src/services/siteService.js @@ -7,6 +7,45 @@ }; function initSiteService() { + SiteService.prototype.encrypt = function (site) { + var model = { + id: site.id, + folderId: site.folderId, + favorite: site.favorite + }; + + var deferred = Q.defer(); + + encryptWithPromise(site.name).then(function (cs) { + model.name = cs; + return encryptWithPromise(site.uri); + }).then(function (cs) { + model.uri = cs; + return encryptWithPromise(site.username); + }).then(function (cs) { + model.username = cs; + return encryptWithPromise(site.password); + }).then(function (cs) { + model.password = cs; + return encryptWithPromise(site.notes); + }).then(function (cs) { + model.notes = 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; + } + SiteService.prototype.get = function (id, callback) { if (!callback || typeof callback !== 'function') { throw 'callback function required';