1
0
mirror of https://github.com/bitwarden/desktop.git synced 2024-11-14 10:16:02 +01:00

Moved vault to cached folders/sites

This commit is contained in:
Kyle Spearrin 2016-09-15 21:09:56 -04:00
parent 16a59f8d09
commit 6f142c3682
4 changed files with 118 additions and 51 deletions

View File

@ -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;
};
}();

View File

@ -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();
}
});
}

View File

@ -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';

View File

@ -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);