1
0
mirror of https://github.com/bitwarden/browser synced 2024-12-23 00:25:04 +01:00

added q promise library. moving cipher service functions out into application services and domain models.

This commit is contained in:
Kyle Spearrin 2016-09-15 00:09:48 -04:00
parent 2c44bd5cc8
commit 16a59f8d09
8 changed files with 121 additions and 77 deletions

View File

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

View File

@ -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"
}
}

View File

@ -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",

View File

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

View File

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

View File

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

View File

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

View File

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