From 27667a3086f9918989f0ca22db246d91ca025dcd Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Mon, 5 Sep 2016 22:27:32 -0400 Subject: [PATCH] decrypt and list sites --- src/models/dataModels.js | 26 +++++++------- src/models/domainModels.js | 35 +++++++++---------- src/popup/app/vault/vaultController.js | 47 ++++++++++++++++---------- src/popup/app/vault/views/vault.html | 14 +++++--- src/services/cryptoService.js | 9 +++-- 5 files changed, 77 insertions(+), 54 deletions(-) diff --git a/src/models/dataModels.js b/src/models/dataModels.js index d5fdb6efe9..d8ac1365ac 100644 --- a/src/models/dataModels.js +++ b/src/models/dataModels.js @@ -1,4 +1,4 @@ -var FolderData = function (response, userId) { +var FolderData = function (response, userId) { var data = null; if (response instanceof FolderResponse) { data = response; @@ -10,10 +10,10 @@ throw 'unsupported instance'; } - this.id = response.Id; + this.id = response.id; this.userId = userId; - this.name = data.Name; - this.revisionDate = response.RevisionDate; + this.name = data.name; + this.revisionDate = response.revisionDate; }; var SiteData = function (response, userId) { @@ -28,14 +28,14 @@ var SiteData = function (response, userId) { throw 'unsupported instance'; } - this.id = response.Id; - this.folderId = response.FolderId; + this.id = response.id; + this.folderId = response.folderId; this.userId = userId; - this.name = data.Name; - this.uri = data.Uri; - this.username = data.Username; - this.password = data.Password; - this.notes = data.Notes; - this.favorite = response.Favorite; - this.revisionDate = response.RevisionDate; + this.name = data.name; + this.uri = data.uri; + this.username = data.username; + this.password = data.password; + this.notes = data.notes; + this.favorite = response.favorite; + this.revisionDate = response.revisionDate; }; diff --git a/src/models/domainModels.js b/src/models/domainModels.js index 9759a750c8..b4bae908bd 100644 --- a/src/models/domainModels.js +++ b/src/models/domainModels.js @@ -18,40 +18,41 @@ var CipherString = function (encryptedString) { callback(_decryptedValue); }); } - - callback(_decryptedValue); + else { + callback(_decryptedValue); + } }; }(); var Site = function (obj, alreadyEncrypted) { - this.id = obj.id; - this.folderId = obj.folderId; + this.id = obj.id ? obj.id : null; + this.folderId = obj.folderId ? obj.folderId : null; if (alreadyEncrypted === true) { - this.name = obj.name; - this.uri = obj.uri; - this.username = obj.username; - this.password = obj.password; - this.notes = obj.notes; + this.name = obj.name ? obj.name : null; + this.uri = obj.uri ? obj.uri : null; + this.username = obj.username ? obj.username : null; + this.password = obj.password ? obj.password : null; + this.notes = obj.notes ? obj.notes : null; } else { - this.name = new CipherString(obj.name); - this.uri = new CipherString(obj.uri); - this.username = new CipherString(obj.username); - this.password = new CipherString(obj.password); - this.notes = new CipherString(obj.notes); + this.name = obj.name ? new CipherString(obj.name) : null; + this.uri = obj.uri ? new CipherString(obj.uri) : null; + this.username = obj.username ? new CipherString(obj.username) : null; + this.password = obj.password ? new CipherString(obj.password) : null; + this.notes = obj.notes ? new CipherString(obj.notes) : null; } this.favorite = obj.favorite ? true : false; }; var Folder = function (obj, alreadyEncrypted) { - this.id = obj.id; + this.id = obj.id ? obj.id : null; if (alreadyEncrypted === true) { - this.name = obj.name; + this.name = obj.name ? obj.name : null; } else { - this.name = new CipherString(obj.name); + this.name = obj.name ? new CipherString(obj.name) : null; } }; diff --git a/src/popup/app/vault/vaultController.js b/src/popup/app/vault/vaultController.js index 6db4aed574..d20d4c6f04 100644 --- a/src/popup/app/vault/vaultController.js +++ b/src/popup/app/vault/vaultController.js @@ -1,7 +1,7 @@ angular .module('bit.vault') - .controller('vaultController', function ($scope, $ionicModal, siteService, folderService) { + .controller('vaultController', function ($scope, $ionicModal, siteService, folderService, $q) { $scope.parentScope = $scope; $scope.sites = []; $scope.folders = []; @@ -14,14 +14,16 @@ folderService.getAll(function (folders) { siteService.getAll(function (sites) { + var promises = []; + for (var i = 0; i < folders.length; i++) { decFolders.push({ id: folders[i].id }); - folders[i].name.decrypt(function (name) { - decFolders.name = name; - }); + promises.push(decrypt(folders[j].name, i).then(function (obj) { + decFolders[obj.index].name = obj.val; + })); } for (var j = 0; j < sites.length; j++) { @@ -31,24 +33,35 @@ favorite: sites[j].favorite }); - if (sites[j].name && sites[j].name.encryptedString) { - sites[j].name.decrypt(function (name) { - decSites.name = name; - }); - } - - if (sites[j].username && sites[j].username.encryptedString) { - sites[j].username.decrypt(function (username) { - decSites.username = username; - }); - } + promises.push(decrypt(sites[j].name, j).then(function (obj) { + decSites[obj.index].name = obj.val; + })); + + promises.push(decrypt(sites[j].username, j).then(function (obj) { + decSites[obj.index].username = obj.val; + })); } - $scope.sites = decSites; - $scope.folders = decFolders; + $q.all(promises).then(function () { + $scope.sites = decSites; + $scope.folders = decFolders; + }); }); }); + 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.addSite = function () { $ionicModal.fromTemplateUrl('app/vault/views/vaultAddSite.html', { scope: $scope, diff --git a/src/popup/app/vault/views/vault.html b/src/popup/app/vault/views/vault.html index 285039ca97..8f998db6c3 100644 --- a/src/popup/app/vault/views/vault.html +++ b/src/popup/app/vault/views/vault.html @@ -4,10 +4,16 @@
-
- Site 1 - -
+ +
+ {{folder.name}} +
+
+ {{site.name}}
+ {{site.username}} + +
+
diff --git a/src/services/cryptoService.js b/src/services/cryptoService.js index 01bc010fc9..5cfeb8a022 100644 --- a/src/services/cryptoService.js +++ b/src/services/cryptoService.js @@ -1,4 +1,4 @@ -function CryptoService() { +function CryptoService() { initCryptoService(); }; @@ -105,6 +105,7 @@ function initCryptoService() { if (plaintextValue === null || plaintextValue === undefined) { callback(null); + return; } this.getKey(false, function (key) { @@ -123,7 +124,8 @@ function initCryptoService() { var ct = ctJson.match(/"ct":"([^"]*)"/)[1]; var iv = sjcl.codec.base64.fromBits(response.iv); - callback(new CipherString(iv + "|" + ct)); + var cs = new CipherString(iv + "|" + ct); + callback(cs); }); }; @@ -145,7 +147,8 @@ function initCryptoService() { var ctBits = sjcl.codec.base64.toBits(cipherString.cipherText); var decBits = sjcl.mode.cbc.decrypt(aes, ctBits, ivBits, null); - callback(sjcl.codec.utf8String.fromBits(decBits)); + var decValue = sjcl.codec.utf8String.fromBits(decBits); + callback(decValue); }); }; };