mirror of
https://github.com/bitwarden/browser
synced 2025-12-14 07:13:32 +00:00
tuneing up mdoels and services for site add
This commit is contained in:
@@ -19,6 +19,8 @@
|
|||||||
"node_modules/sjcl/core/bitArray.js",
|
"node_modules/sjcl/core/bitArray.js",
|
||||||
"models/api/requestModels.js",
|
"models/api/requestModels.js",
|
||||||
"models/api/responseModels.js",
|
"models/api/responseModels.js",
|
||||||
|
"models/dataModels.js",
|
||||||
|
"models/domainModels.js",
|
||||||
"services/cryptoService.js",
|
"services/cryptoService.js",
|
||||||
"services/cryptoService.js",
|
"services/cryptoService.js",
|
||||||
"services/tokenService.js",
|
"services/tokenService.js",
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ var CipherString = function (encryptedString) {
|
|||||||
CipherString.prototype.decrypt = function (callback) {
|
CipherString.prototype.decrypt = function (callback) {
|
||||||
if (!_decryptedValue) {
|
if (!_decryptedValue) {
|
||||||
var cryptoService = chrome.extension.getBackgroundPage().cryptoService;
|
var cryptoService = chrome.extension.getBackgroundPage().cryptoService;
|
||||||
_decryptedValue = cryptoService.Decrypt(this);
|
_decryptedValue = cryptoService.decrypt(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
return _decryptedValue;
|
return _decryptedValue;
|
||||||
@@ -39,7 +39,7 @@ var Site = function (obj, alreadyEncrypted) {
|
|||||||
this.notes = new CipherString(obj.notes);
|
this.notes = new CipherString(obj.notes);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.favorite = new obj.favorite;
|
this.favorite = obj.favorite ? true : false;
|
||||||
};
|
};
|
||||||
|
|
||||||
var Folder = function (obj, alreadyEncrypted) {
|
var Folder = function (obj, alreadyEncrypted) {
|
||||||
|
|||||||
@@ -1,15 +1,15 @@
|
|||||||
angular
|
angular
|
||||||
.module('bit.services')
|
.module('bit.services')
|
||||||
|
|
||||||
.factory('loginService', function (cryptoService, apiService, apiService, userService, tokenService, $q) {
|
.factory('loginService', function (cryptoService, apiService, userService, tokenService, $q) {
|
||||||
var _service = {};
|
var _service = {};
|
||||||
|
|
||||||
_service.logIn = function (email, masterPassword) {
|
_service.logIn = function (email, masterPassword) {
|
||||||
var key = cryptoService.makeKey(masterPassword, email);
|
var key = cryptoService.makeKey(masterPassword, email);
|
||||||
|
var deferred = $q.defer();
|
||||||
cryptoService.hashPassword(masterPassword, key, function (hashedPassword) {
|
cryptoService.hashPassword(masterPassword, key, function (hashedPassword) {
|
||||||
var request = new TokenRequest(email, hashedPassword);
|
var request = new TokenRequest(email, hashedPassword);
|
||||||
|
|
||||||
var deferred = $q.defer();
|
|
||||||
apiService.postToken(request, function (response) {
|
apiService.postToken(request, function (response) {
|
||||||
if (!response || !response.token) {
|
if (!response || !response.token) {
|
||||||
return;
|
return;
|
||||||
@@ -25,9 +25,8 @@
|
|||||||
}, function (error) {
|
}, function (error) {
|
||||||
deferred.reject(error);
|
deferred.reject(error);
|
||||||
});
|
});
|
||||||
|
|
||||||
return deferred.promise;
|
|
||||||
});
|
});
|
||||||
|
return deferred.promise;
|
||||||
};
|
};
|
||||||
|
|
||||||
_service.logInTwoFactor = function (code, provider) {
|
_service.logInTwoFactor = function (code, provider) {
|
||||||
|
|||||||
@@ -7,8 +7,7 @@
|
|||||||
};
|
};
|
||||||
|
|
||||||
$scope.createSite = function (model) {
|
$scope.createSite = function (model) {
|
||||||
var newModel = model;
|
encryptSite(model, function (siteModel) {
|
||||||
encryptSite(newModel, function (siteModel) {
|
|
||||||
var site = new Site(siteModel, true);
|
var site = new Site(siteModel, true);
|
||||||
siteService.save(site, function () {
|
siteService.save(site, function () {
|
||||||
$scope.close();
|
$scope.close();
|
||||||
@@ -20,16 +19,17 @@
|
|||||||
$scope.parentScope.closeAddSite();
|
$scope.parentScope.closeAddSite();
|
||||||
};
|
};
|
||||||
|
|
||||||
function encryptSite(siteModel, callback) {
|
function encryptSite(model, callback) {
|
||||||
cryptoService.encrypt(siteModel.name, function (nameCipherString) {
|
var siteModel = {};
|
||||||
|
cryptoService.encrypt(model.name, function (nameCipherString) {
|
||||||
siteModel.name = nameCipherString;
|
siteModel.name = nameCipherString;
|
||||||
cryptoService.encrypt(siteModel.uri, function (uriCipherString) {
|
cryptoService.encrypt(model.uri, function (uriCipherString) {
|
||||||
siteModel.uri = uriCipherString;
|
siteModel.uri = uriCipherString;
|
||||||
cryptoService.encrypt(siteModel.username, function (usernameCipherString) {
|
cryptoService.encrypt(model.username, function (usernameCipherString) {
|
||||||
siteModel.username = usernameCipherString;
|
siteModel.username = usernameCipherString;
|
||||||
cryptoService.encrypt(siteModel.password, function (passwordCipherString) {
|
cryptoService.encrypt(model.password, function (passwordCipherString) {
|
||||||
siteModel.password = passwordCipherString;
|
siteModel.password = passwordCipherString;
|
||||||
cryptoService.encrypt(siteModel.notes, function (notesCipherString) {
|
cryptoService.encrypt(model.notes, function (notesCipherString) {
|
||||||
siteModel.notes = notesCipherString;
|
siteModel.notes = notesCipherString;
|
||||||
callback(siteModel);
|
callback(siteModel);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -15,7 +15,7 @@
|
|||||||
</label>
|
</label>
|
||||||
<label class="item item-input item-stacked-label">
|
<label class="item item-input item-stacked-label">
|
||||||
<span class="input-label">URI</span>
|
<span class="input-label">URI</span>
|
||||||
<input type="url" ng-model="site.uri">
|
<input type="text" ng-model="site.uri">
|
||||||
</label>
|
</label>
|
||||||
<label class="item item-input item-stacked-label">
|
<label class="item item-input item-stacked-label">
|
||||||
<span class="input-label">Username</span>
|
<span class="input-label">Username</span>
|
||||||
|
|||||||
@@ -7,6 +7,8 @@ function initCryptoService() {
|
|||||||
_b64Key,
|
_b64Key,
|
||||||
_aes;
|
_aes;
|
||||||
|
|
||||||
|
sjcl.beware["CBC mode is dangerous because it doesn't protect message integrity."]();
|
||||||
|
|
||||||
CryptoService.prototype.setKey = function (key, callback) {
|
CryptoService.prototype.setKey = function (key, callback) {
|
||||||
if (!callback || typeof callback !== 'function') {
|
if (!callback || typeof callback !== 'function') {
|
||||||
throw 'callback function required';
|
throw 'callback function required';
|
||||||
@@ -101,6 +103,10 @@ function initCryptoService() {
|
|||||||
throw 'callback function required';
|
throw 'callback function required';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (plaintextValue === null || plaintextValue === undefined) {
|
||||||
|
callback(null);
|
||||||
|
}
|
||||||
|
|
||||||
this.getKey(false, function (key) {
|
this.getKey(false, function (key) {
|
||||||
if (!key) {
|
if (!key) {
|
||||||
throw 'Encryption key unavailable.';
|
throw 'Encryption key unavailable.';
|
||||||
@@ -121,11 +127,15 @@ function initCryptoService() {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
CryptoService.prototype.decrypt = function (cipherStrin, callback) {
|
CryptoService.prototype.decrypt = function (cipherString, callback) {
|
||||||
if (!callback || typeof callback !== 'function') {
|
if (!callback || typeof callback !== 'function') {
|
||||||
throw 'callback function required';
|
throw 'callback function required';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (cipherString === null || cipherString === undefined) {
|
||||||
|
throw 'cannot decrypt nothing';
|
||||||
|
}
|
||||||
|
|
||||||
this.getAes(function (aes) {
|
this.getAes(function (aes) {
|
||||||
if (!aes) {
|
if (!aes) {
|
||||||
throw 'AES encryption unavailable.';
|
throw 'AES encryption unavailable.';
|
||||||
|
|||||||
@@ -7,13 +7,13 @@
|
|||||||
};
|
};
|
||||||
|
|
||||||
function initFolderService() {
|
function initFolderService() {
|
||||||
this.userService.getUserId(function (userId) {
|
FolderService.prototype.get = function (id, callback) {
|
||||||
var foldersKey = 'folders_' + userId;
|
if (!callback || typeof callback !== 'function') {
|
||||||
|
throw 'callback function required';
|
||||||
|
}
|
||||||
|
|
||||||
FolderService.prototype.get = function (id, callback) {
|
this.userService.getUserId(function (userId) {
|
||||||
if (!callback || typeof callback !== 'function') {
|
var foldersKey = 'folders_' + userId;
|
||||||
throw 'callback function required';
|
|
||||||
}
|
|
||||||
|
|
||||||
chrome.storage.local.get(foldersKey, function (obj) {
|
chrome.storage.local.get(foldersKey, function (obj) {
|
||||||
var folders = obj[foldersKey];
|
var folders = obj[foldersKey];
|
||||||
@@ -24,12 +24,16 @@ function initFolderService() {
|
|||||||
|
|
||||||
callback(null);
|
callback(null);
|
||||||
});
|
});
|
||||||
};
|
});
|
||||||
|
};
|
||||||
|
|
||||||
FolderService.prototype.getAll = function (callback) {
|
FolderService.prototype.getAll = function (callback) {
|
||||||
if (!callback || typeof callback !== 'function') {
|
if (!callback || typeof callback !== 'function') {
|
||||||
throw 'callback function required';
|
throw 'callback function required';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.userService.getUserId(function (userId) {
|
||||||
|
var foldersKey = 'folders_' + userId;
|
||||||
|
|
||||||
chrome.storage.local.get(foldersKey, function (obj) {
|
chrome.storage.local.get(foldersKey, function (obj) {
|
||||||
var folders = obj[foldersKey];
|
var folders = obj[foldersKey];
|
||||||
@@ -41,10 +45,10 @@ function initFolderService() {
|
|||||||
|
|
||||||
callback(response);
|
callback(response);
|
||||||
});
|
});
|
||||||
};
|
});
|
||||||
|
};
|
||||||
|
|
||||||
function handleError() {
|
function handleError() {
|
||||||
// TODO: check for unauth or forbidden and logout
|
// TODO: check for unauth or forbidden and logout
|
||||||
}
|
}
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -7,13 +7,13 @@
|
|||||||
};
|
};
|
||||||
|
|
||||||
function initSiteService() {
|
function initSiteService() {
|
||||||
this.userService.getUserId(function (userId) {
|
SiteService.prototype.get = function (id, callback) {
|
||||||
var sitesKey = 'sites_' + userId;
|
if (!callback || typeof callback !== 'function') {
|
||||||
|
throw 'callback function required';
|
||||||
|
}
|
||||||
|
|
||||||
SiteService.prototype.get = function (id, callback) {
|
this.userService.getUserId(function (userId) {
|
||||||
if (!callback || typeof callback !== 'function') {
|
var sitesKey = 'sites_' + userId;
|
||||||
throw 'callback function required';
|
|
||||||
}
|
|
||||||
|
|
||||||
chrome.storage.local.get(sitesKey, function (obj) {
|
chrome.storage.local.get(sitesKey, function (obj) {
|
||||||
var sites = obj[sitesKey];
|
var sites = obj[sitesKey];
|
||||||
@@ -24,93 +24,105 @@ function initSiteService() {
|
|||||||
|
|
||||||
callback(null);
|
callback(null);
|
||||||
});
|
});
|
||||||
};
|
});
|
||||||
|
};
|
||||||
|
|
||||||
SiteService.prototype.getAll = function (callback) {
|
SiteService.prototype.getAll = function (callback) {
|
||||||
if (!callback || typeof callback !== 'function') {
|
if (!callback || typeof callback !== 'function') {
|
||||||
throw 'callback function required';
|
throw 'callback function required';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.userService.getUserId(function (userId) {
|
||||||
|
var sitesKey = 'sites_' + userId;
|
||||||
|
|
||||||
chrome.storage.local.get(sitesKey, function (obj) {
|
chrome.storage.local.get(sitesKey, function (obj) {
|
||||||
var sites = obj[sitesKey];
|
var sites = obj[sitesKey];
|
||||||
var response = [];
|
var response = [];
|
||||||
for (var id in sites) {
|
for (var id in sites) {
|
||||||
|
if (!id) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
response.push(new Site(sites[id]));
|
response.push(new Site(sites[id]));
|
||||||
}
|
}
|
||||||
|
|
||||||
callback(response);
|
callback(response);
|
||||||
});
|
});
|
||||||
};
|
});
|
||||||
|
};
|
||||||
|
|
||||||
SiteService.prototype.save = function (site, callback) {
|
SiteService.prototype.save = function (site, callback) {
|
||||||
if (!callback || typeof callback !== 'function') {
|
if (!callback || typeof callback !== 'function') {
|
||||||
throw 'callback function required';
|
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(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;
|
|
||||||
}
|
|
||||||
|
|
||||||
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';
|
|
||||||
}
|
|
||||||
|
|
||||||
self.apiService.deleteCipher(id, apiSuccess, handleError);
|
|
||||||
|
|
||||||
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);
|
|
||||||
|
|
||||||
obj[sitesKey] = sites;
|
|
||||||
chrome.storage.local.set(obj, function () {
|
|
||||||
callback();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
callback();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
function handleError() {
|
|
||||||
// TODO: check for unauth or forbidden and logout
|
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
var newRecord = site.id ? false : true,
|
||||||
|
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) {
|
||||||
|
site.id = response.id;
|
||||||
|
|
||||||
|
userService.getUserId(function (userId) {
|
||||||
|
var data = new SiteData(response, userId);
|
||||||
|
var sitesKey = 'sites_' + userId;
|
||||||
|
|
||||||
|
chrome.storage.local.get(sitesKey, function (obj) {
|
||||||
|
var sites = obj[sitesKey];
|
||||||
|
if (!sites) {
|
||||||
|
sites = {};
|
||||||
|
}
|
||||||
|
|
||||||
|
sites[site.id] = data;
|
||||||
|
site.id = data.id;
|
||||||
|
|
||||||
|
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';
|
||||||
|
}
|
||||||
|
|
||||||
|
self.apiService.deleteCipher(id, apiSuccess, handleError);
|
||||||
|
|
||||||
|
function apiSuccess(response) {
|
||||||
|
userService.getUserId(function (userId) {
|
||||||
|
var sitesKey = 'sites_' + userId;
|
||||||
|
|
||||||
|
chrome.storage.local.get(sitesKey, function (obj) {
|
||||||
|
var sites = obj[sitesKey];
|
||||||
|
if (!sites) {
|
||||||
|
sites = {};
|
||||||
|
}
|
||||||
|
if (id in sites) {
|
||||||
|
delete obj[sitesKey];
|
||||||
|
chrome.storage.local.set(obj, function () {
|
||||||
|
callback();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
callback();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
function handleError() {
|
||||||
|
// TODO: check for unauth or forbidden and logout
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user