Site Information
diff --git a/src/services/apiService.js b/src/services/apiService.js
index 6301f2ada7..c6e6b2f337 100644
--- a/src/services/apiService.js
+++ b/src/services/apiService.js
@@ -1,9 +1,11 @@
function ApiService(tokenService) {
this.baseUrl = 'https://api.bitwarden.com';
this.tokenService = tokenService;
+
+ initApiService();
};
-!function () {
+function initApiService() {
// Auth APIs
ApiService.prototype.postToken = function (tokenRequest, success, error) {
@@ -235,4 +237,4 @@
function handleError(errorCallback, jqXHR, textStatus, errorThrown) {
errorCallback(new ErrorResponse(jqXHR));
}
-}();
+};
diff --git a/src/services/cryptoService.js b/src/services/cryptoService.js
index 4696c3c610..4649f17a16 100644
--- a/src/services/cryptoService.js
+++ b/src/services/cryptoService.js
@@ -1,8 +1,8 @@
function CryptoService() {
-
+ initCryptoService();
};
-!function () {
+function initCryptoService() {
var _key,
_b64Key,
_aes;
@@ -32,9 +32,9 @@
return callback(_key);
}
- chrome.storage.local.get('key', function (key) {
- if (key) {
- _key = sjcl.codec.base64.toBits(key);
+ chrome.storage.local.get('key', function (obj) {
+ if (obj && obj.key) {
+ _key = sjcl.codec.base64.toBits(obj.key);
}
if (b64 && b64 === true) {
@@ -80,46 +80,60 @@
return sjcl.codec.base64.fromBits(hashBits);
};
- CryptoService.prototype.getAes = function () {
- if (!_aes && this.getKey()) {
- _aes = new sjcl.cipher.aes(this.getKey());
+ CryptoService.prototype.getAes = function (callback) {
+ if (!callback || typeof callback !== 'function') {
+ throw 'callback function required';
}
- return _aes;
+ this.getKey(false, function (key) {
+ if (!_aes && key) {
+ _aes = new sjcl.cipher.aes(key);
+ }
+
+ callback(_aes);
+ });
};
- CryptoService.prototype.encrypt = function (plaintextValue, key) {
- if (!this.getKey() && !key) {
- throw 'Encryption key unavailable.';
+ CryptoService.prototype.encrypt = function (plaintextValue, callback) {
+ if (!callback || typeof callback !== 'function') {
+ throw 'callback function required';
}
- if (!key) {
- key = this.getKey();
- }
+ this.getKey(false, function (key) {
+ if (!key) {
+ throw 'Encryption key unavailable.';
+ }
- var response = {};
- var params = {
- mode: "cbc",
- iv: sjcl.random.randomWords(4, 0)
- };
+ var response = {};
+ var params = {
+ mode: "cbc",
+ iv: sjcl.random.randomWords(4, 0)
+ };
- var ctJson = sjcl.encrypt(key, plaintextValue, params, response);
+ var ctJson = sjcl.encrypt(key, plaintextValue, params, response);
- var ct = ctJson.match(/"ct":"([^"]*)"/)[1];
- var iv = sjcl.codec.base64.fromBits(response.iv);
+ var ct = ctJson.match(/"ct":"([^"]*)"/)[1];
+ var iv = sjcl.codec.base64.fromBits(response.iv);
- return new CipherString(iv + "|" + ct);
+ callback(new CipherString(iv + "|" + ct));
+ });
};
- CryptoService.prototype.decrypt = function (cipherString) {
- if (!this.getAes()) {
- throw 'AES encryption unavailable.';
+ CryptoService.prototype.decrypt = function (cipherStrin, callback) {
+ if (!callback || typeof callback !== 'function') {
+ throw 'callback function required';
}
- var ivBits = sjcl.codec.base64.toBits(cipherString.initializationVector);
- var ctBits = sjcl.codec.base64.toBits(cipherString.cipherText);
+ this.getAes(function (aes) {
+ if (!aes) {
+ throw 'AES encryption unavailable.';
+ }
- var decBits = sjcl.mode.cbc.decrypt(this.getAes(), ctBits, ivBits, null);
- return sjcl.codec.utf8String.fromBits(decBits);
+ var ivBits = sjcl.codec.base64.toBits(cipherString.initializationVector);
+ var ctBits = sjcl.codec.base64.toBits(cipherString.cipherText);
+
+ var decBits = sjcl.mode.cbc.decrypt(aes, ctBits, ivBits, null);
+ callback(sjcl.codec.utf8String.fromBits(decBits));
+ });
};
-}();
+};
diff --git a/src/services/folderService.js b/src/services/folderService.js
new file mode 100644
index 0000000000..70141d9d9a
--- /dev/null
+++ b/src/services/folderService.js
@@ -0,0 +1,50 @@
+function FolderService(cryptoService, userService, apiService) {
+ this.cryptoService = cryptoService;
+ this.userService = userService;
+ this.apiService = apiService;
+
+ initFolderService();
+};
+
+function initFolderService() {
+ this.userService.getUserId(function (userId) {
+ var foldersKey = 'folders_' + userId;
+
+ FolderService.prototype.get = function (id, callback) {
+ if (!callback || typeof callback !== 'function') {
+ throw 'callback function required';
+ }
+
+ chrome.storage.local.get(foldersKey, function (obj) {
+ var folders = obj[foldersKey];
+ if (id in folders) {
+ callback(new Folder(folders[id]));
+ return;
+ }
+
+ callback(null);
+ });
+ };
+
+ FolderService.prototype.getAll = function (callback) {
+ if (!callback || typeof callback !== 'function') {
+ throw 'callback function required';
+ }
+
+ chrome.storage.local.get(foldersKey, function (obj) {
+ var folders = obj[foldersKey];
+ var response = [];
+ for (var id in folders) {
+ var folder = folders[id];
+ response.push(new Folder(folder));
+ }
+
+ callback(response);
+ });
+ };
+
+ function handleError() {
+ // TODO: check for unauth or forbidden and logout
+ }
+ });
+};
diff --git a/src/services/siteService.js b/src/services/siteService.js
index dbf7fcb8e3..3cff741fc5 100644
--- a/src/services/siteService.js
+++ b/src/services/siteService.js
@@ -2,130 +2,115 @@
this.cryptoService = cryptoService;
this.userService = userService;
this.apiService = apiService;
+
+ initSiteService();
};
-!function () {
- var ciphersKey = 'ciphers_' + this.userService.userId;
+function initSiteService() {
+ this.userService.getUserId(function (userId) {
+ var sitesKey = 'sites_' + userId;
- SiteService.prototype.get = function (id, callback) {
- if (!callback || typeof callback !== 'function') {
- throw 'callback function required';
- }
+ SiteService.prototype.get = function (id, callback) {
+ if (!callback || typeof callback !== 'function') {
+ throw 'callback function required';
+ }
+
+ chrome.storage.local.get(sitesKey, function (obj) {
+ var sites = obj[sitesKey];
+ if (id in sites) {
+ callback(new Site(sites[id]));
+ return;
+ }
- chrome.storage.local.get(ciphersKey, function (obj) {
- if (!obj) {
callback(null);
+ });
+ };
+
+ SiteService.prototype.getAll = function (callback) {
+ if (!callback || typeof callback !== 'function') {
+ throw 'callback function required';
}
- var sites = obj[ciphersKey];
- if (id in sites) {
- callback(new Site(sites[id]));
- return;
+ chrome.storage.local.get(sitesKey, function (obj) {
+ var sites = obj[sitesKey];
+ var response = [];
+ for (var id in sites) {
+ response.push(new Site(sites[id]));
+ }
+
+ callback(response);
+ });
+ };
+
+ SiteService.prototype.save = function (site, callback) {
+ if (!callback || typeof callback !== 'function') {
+ throw 'callback function required';
}
- callback(null);
- });
- };
+ var newRecord = site.id === null,
+ self = this;
- SiteService.prototype.getAll = function (callback) {
- if (!callback || typeof callback !== 'function') {
- throw 'callback function required';
- }
-
- chrome.storage.local.get(ciphersKey, function (obj) {
- if (!obj) {
- callback([]);
+ var request = new SiteRequest(site);
+ if (newRecord) {
+ self.apiService.postSite(request, apiSuccess, handleError);
+ }
+ else {
+ self.apiService.putSite(site.id, request, apiSuccess, handleError);
}
- var sites = obj[ciphersKey];
- var response = [];
- for (var id in sites) {
- response.push(new Site(sites[id]));
- }
+ function apiSuccess(response) {
+ userService.getUserId(function (userId) {
+ var data = new SiteData(response, userId);
- callback(response);
- });
- };
+ chrome.storage.local.get(sitesKey, function (obj) {
+ var sites = obj[sitesKey];
+ if (!newRecord && site.id in sites) {
+ sites[site.id] = data;
+ }
+ else {
+ sites.push(data);
+ site.id = data.id;
+ }
- SiteService.prototype.save = function (site, callback) {
- if (!callback || typeof callback !== 'function') {
- throw 'callback function required';
- }
-
- var newRecord = site.id === null,
- self = this;
-
- var request = new SiteRequest(site);
- if (newRecord) {
- self.apiService.postSite(request, apiSuccess, handleError);
- }
- else {
- self.apiService.putSite(site.id, request, apiSuccess, handleError);
- }
-
- function apiSuccess(response) {
- userService.getUserId(function (userId) {
- var data = new SiteData(response, userId);
-
- chrome.storage.local.get(ciphersKey, function (obj) {
- if (!obj) {
- obj = {};
- obj[ciphersKey] = [];
- }
-
- var sites = obj[ciphersKey];
- if (!newRecord && site.id in sites) {
- sites[site.id] = data;
- }
- else {
- sites.push(data);
- site.id = data.id;
- }
-
- obj[ciphersKey] = sites;
- chrome.storage.local.set(obj, function () {
- callback(site);
+ obj[sitesKey] = sites;
+ chrome.storage.local.set(obj, function () {
+ callback(site);
+ });
});
});
- });
- }
- };
+ }
+ };
- SiteService.prototype.delete = function (id, callback) {
- if (!callback || typeof callback !== 'function') {
- throw 'callback function required';
- }
+ SiteService.prototype.delete = function (id, callback) {
+ if (!callback || typeof callback !== 'function') {
+ throw 'callback function required';
+ }
- self.apiService.deleteCipher(id, apiSuccess, handleError);
+ self.apiService.deleteCipher(id, apiSuccess, handleError);
- function apiSuccess(response) {
- userService.getUserId(function (userId) {
- chrome.storage.local.get(ciphersKey, function (obj) {
- if (!obj) {
- obj = {};
- obj[ciphersKey] = [];
- }
+ function apiSuccess(response) {
+ userService.getUserId(function (userId) {
+ chrome.storage.local.get(sitesKey, function (obj) {
+ var sites = obj[sitesKey];
+ if (id in sites) {
+ var index = sites.indexOf(sites[id]);
+ sites.splice(index, 1);
- var sites = obj[ciphersKey];
- if (id in sites) {
- var index = sites.indexOf(sites[id]);
- sites.splice(index, 1);
-
- obj[ciphersKey] = sites;
- chrome.storage.local.set(obj, function () {
+ obj[sitesKey] = sites;
+ chrome.storage.local.set(obj, function () {
+ callback();
+ });
+ }
+ else {
callback();
- });
- }
- else {
- callback();
- }
+ }
+ });
});
- });
+ }
+ };
+
+ function handleError() {
+ // TODO: check for unauth or forbidden and logout
}
- };
-
-
- function handleError() {
- // TODO: check for unauth or forbidden and logout
- }
-}();
+ });
+};
diff --git a/src/services/tokenService.js b/src/services/tokenService.js
index 131225045a..0d447caadd 100644
--- a/src/services/tokenService.js
+++ b/src/services/tokenService.js
@@ -1,8 +1,8 @@
function TokenService() {
-
+ initTokenService();
};
-!function () {
+function initTokenService() {
var _token;
TokenService.prototype.setToken = function (token, callback) {
@@ -101,4 +101,4 @@
}
return window.decodeURIComponent(escape(window.atob(output))); //polyfill https://github.com/davidchambers/Base64.js
};
-}();
+};
diff --git a/src/services/userService.js b/src/services/userService.js
index 9509d68424..85c289a2e4 100644
--- a/src/services/userService.js
+++ b/src/services/userService.js
@@ -1,9 +1,11 @@
function UserService(tokenService, apiService) {
this.tokenService = tokenService;
this.apiService = apiService;
+
+ initUserService();
};
-!function () {
+function initUserService() {
var _userProfile = null;
UserService.prototype.getUserId = function (callback) {
@@ -90,4 +92,4 @@
callback(profile !== null && profile.twoFactor);
});
};
-}();
+};