1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-11-23 11:56:00 +01:00

user service to ts

This commit is contained in:
Kyle Spearrin 2017-11-03 15:12:49 -04:00
parent 0c878d78f3
commit ee11bb5137
8 changed files with 105 additions and 175 deletions

View File

@ -5,7 +5,6 @@
<script type="text/javascript" src="lib/tldjs/tld.js"></script>
<script type="text/javascript" src="lib/forge/forge.js"></script>
<script type="text/javascript" src="models/domainModels.js"></script>
<script type="text/javascript" src="services/userService.js"></script>
<script type="text/javascript" src="services/settingsService.js"></script>
<script type="text/javascript" src="services/folderService.js"></script>
<script type="text/javascript" src="services/cipherService.js"></script>

View File

@ -9,6 +9,7 @@ import LockService from './services/lockService.js';
import PasswordGenerationService from './services/passwordGeneration.service';
import TokenService from './services/token.service';
import TotpService from './services/totp.service';
import UserService from './services/user.service';
import UtilsService from './services/utils.service';
// Model imports
@ -84,7 +85,7 @@ var bg_isBackground = true,
window.bg_appIdService = bg_appIdService = new AppIdService();
window.bg_apiService = bg_apiService = new ApiService(bg_tokenService, logout);
window.bg_environmentService = bg_environmentService = new EnvironmentService(bg_apiService);
window.bg_userService = bg_userService = new UserService(bg_tokenService, bg_apiService, bg_cryptoService, bg_utilsService);
window.bg_userService = bg_userService = new UserService(bg_tokenService);
window.bg_settingsService = bg_settingsService = new SettingsService(bg_userService, bg_utilsService);
window.bg_cipherService = bg_cipherService = new CipherService(bg_cryptoService, bg_userService, bg_apiService, bg_settingsService, bg_utilsService,
bg_constantsService);
@ -896,7 +897,7 @@ var bg_isBackground = true,
function logout(expired, callback) {
bg_syncService.setLastSync(new Date(0), function () {
bg_userService.getUserIdPromise().then(function (userId) {
bg_userService.getUserId().then(function (userId) {
return Q.all([
bg_tokenService.clearToken(),
bg_cryptoService.clearKeys(),

View File

@ -160,7 +160,7 @@ function initCipherService() {
key = null,
localData;
return self.userService.getUserIdPromise().then(function (userId) {
return self.userService.getUserId().then(function (userId) {
key = 'ciphers_' + userId;
return self.utilsService.getObjFromStorage(self.localDataKey);
}).then(function (data) {
@ -183,7 +183,7 @@ function initCipherService() {
key = null,
localData = null;
return self.userService.getUserIdPromise().then(function (userId) {
return self.userService.getUserId().then(function (userId) {
key = 'ciphers_' + userId;
return self.utilsService.getObjFromStorage(self.localDataKey);
}).then(function (data) {
@ -337,7 +337,7 @@ function initCipherService() {
function apiSuccess(response) {
cipher.id = response.id;
self.userService.getUserIdPromise().then(function (userId) {
self.userService.getUserId().then(function (userId) {
var data = new CipherData(response, userId);
return self.upsert(data);
}).then(function () {
@ -352,7 +352,7 @@ function initCipherService() {
var self = this,
key = null;
return self.userService.getUserIdPromise().then(function (userId) {
return self.userService.getUserId().then(function (userId) {
key = 'ciphers_' + userId;
return self.utilsService.getObjFromStorage(key);
}).then(function (ciphers) {
@ -412,7 +412,7 @@ function initCipherService() {
CipherService.prototype.replace = function (ciphers) {
var self = this;
return self.userService.getUserIdPromise().then(function (userId) {
return self.userService.getUserId().then(function (userId) {
return self.utilsService.saveObjToStorage('ciphers_' + userId, ciphers);
}).then(function () {
self.decryptedCipherCache = null;
@ -430,7 +430,7 @@ function initCipherService() {
var self = this,
key = null;
return self.userService.getUserIdPromise().then(function (userId) {
return self.userService.getUserId().then(function (userId) {
key = 'ciphers_' + userId;
return self.utilsService.getObjFromStorage(key);
}).then(function (ciphers) {
@ -514,7 +514,7 @@ function initCipherService() {
return self.apiService.postCipherAttachment(cipher.id, fd);
}).then(function (resp) {
response = resp;
return self.userService.getUserIdPromise();
return self.userService.getUserId();
}, function (resp) {
apiErrored = true;
handleErrorMessage(resp, deferred);
@ -542,7 +542,7 @@ function initCipherService() {
var self = this,
key = null;
return self.userService.getUserIdPromise().then(function (userId) {
return self.userService.getUserId().then(function (userId) {
key = 'ciphers_' + userId;
return self.utilsService.getObjFromStorage(key);
}).then(function (ciphers) {

View File

@ -28,7 +28,7 @@ function initFolderService() {
FolderService.prototype.get = function (id) {
var self = this;
return self.userService.getUserIdPromise().then(function (userId) {
return self.userService.getUserId().then(function (userId) {
return self.utilsService.getObjFromStorage('folders_' + userId);
}).then(function (folders) {
if (folders && id in folders) {
@ -42,7 +42,7 @@ function initFolderService() {
FolderService.prototype.getAll = function () {
var self = this;
return self.userService.getUserIdPromise().then(function (userId) {
return self.userService.getUserId().then(function (userId) {
return self.utilsService.getObjFromStorage('folders_' + userId);
}).then(function (folders) {
var response = [];
@ -120,7 +120,7 @@ function initFolderService() {
function apiSuccess(response) {
folder.id = response.id;
self.userService.getUserIdPromise().then(function (userId) {
self.userService.getUserId().then(function (userId) {
var data = new FolderData(response, userId);
return self.upsert(data);
}).then(function () {
@ -135,7 +135,7 @@ function initFolderService() {
var self = this,
key = null;
return self.userService.getUserIdPromise().then(function (userId) {
return self.userService.getUserId().then(function (userId) {
key = 'folders_' + userId;
return self.utilsService.getObjFromStorage(key);
}).then(function (folders) {
@ -160,7 +160,7 @@ function initFolderService() {
FolderService.prototype.replace = function (folders) {
var self = this;
return self.userService.getUserIdPromise().then(function (userId) {
return self.userService.getUserId().then(function (userId) {
return self.utilsService.saveObjToStorage('folders_' + userId, folders);
}).then(function () {
self.decryptedFolderCache = null;
@ -180,7 +180,7 @@ function initFolderService() {
// TODO: Delete folder reference for associated ciphers
return self.userService.getUserIdPromise().then(function (userId) {
return self.userService.getUserId().then(function (userId) {
key = 'folders_' + userId;
return self.utilsService.getObjFromStorage(key);
}).then(function (folders) {

View File

@ -23,7 +23,7 @@ function initSettingsService() {
return;
}
this.userService.getUserId(function (userId) {
this.userService.getUserId().then(function (userId) {
var key = 'settings_' + userId;
chrome.storage.local.get(key, function (obj) {
self.settingsCache = obj[key];
@ -66,7 +66,7 @@ function initSettingsService() {
throw 'callback function required';
}
self.userService.getUserId(function (userId) {
self.userService.getUserId().then(function (userId) {
var settingsKey = 'settings_' + userId;
self.getSettings(function (settings) {

View File

@ -44,7 +44,7 @@ function initSyncService() {
return;
}
self.userService.getUserId(function (userId) {
self.userService.getUserId().then(function (userId) {
self.apiService.getSync().then(function (response) {
syncProfile(self, response.profile).then(function () {
return syncFolders(self, userId, response.folders);
@ -173,7 +173,7 @@ function initSyncService() {
throw 'callback function required';
}
this.userService.getUserId(function (userId) {
this.userService.getUserId().then(function (userId) {
var lastSyncKey = 'lastSync_' + userId;
chrome.storage.local.get(lastSyncKey, function (obj) {
var lastSync = obj[lastSyncKey];
@ -192,7 +192,7 @@ function initSyncService() {
throw 'callback function required';
}
this.userService.getUserId(function (userId) {
this.userService.getUserId().then(function (userId) {
var lastSyncKey = 'lastSync_' + userId;
var obj = {};

View File

@ -0,0 +1,83 @@
import TokenService from './token.service';
import UtilsService from './utils.service';
const Keys = {
userId: 'userId',
userEmail: 'userEmail',
stamp: 'securityStamp',
};
export default class UserService {
userId: string;
email: string;
stamp: string;
constructor(private tokenService: TokenService) {
}
// TODO: fix callbacks
setUserIdAndEmail(userId: string, email: string): Promise<any> {
this.email = email;
this.userId = userId;
return Promise.all([
UtilsService.saveObjToStorage(Keys.userEmail, email),
UtilsService.saveObjToStorage(Keys.userId, userId),
]);
}
setSecurityStamp(stamp: string): Promise<any> {
this.stamp = stamp;
return UtilsService.saveObjToStorage(Keys.stamp, stamp);
}
// TODO: callbacks
async getUserId(): Promise<string> {
if (this.userId != null) {
return this.userId;
}
this.userId = await UtilsService.getObjFromStorage<string>(Keys.userId);
return this.userId;
}
// TODO: callbacks
async getEmail(): Promise<string> {
if (this.email != null) {
return this.email;
}
this.email = await UtilsService.getObjFromStorage<string>(Keys.userEmail);
return this.email;
}
async getSecurityStamp(): Promise<string> {
if (this.stamp != null) {
return this.stamp;
}
this.stamp = await UtilsService.getObjFromStorage<string>(Keys.stamp);
return this.stamp;
}
async clear(): Promise<any> {
await Promise.all([
UtilsService.removeFromStorage(Keys.userId),
UtilsService.removeFromStorage(Keys.userEmail),
UtilsService.removeFromStorage(Keys.stamp),
]);
this.userId = this.email = this.stamp = null;
}
// TODO: fix callbacks
async isAuthenticated(): Promise<boolean> {
const token = await this.tokenService.getToken();
if (token == null) {
return false;
}
const userId = await this.getUserId();
return userId != null;
}
}

View File

@ -1,153 +0,0 @@
function UserService(tokenService, apiService, cryptoService, utilsService) {
this.tokenService = tokenService;
this.apiService = apiService;
this.cryptoService = cryptoService;
this.utilsService = utilsService;
initUserService();
}
function initUserService() {
var userIdKey = 'userId',
userEmailKey = 'userEmail',
stampKey = 'securityStamp';
var _userId = null,
_email = null,
_stamp = null;
UserService.prototype.setUserIdAndEmail = function (userId, email, callback) {
if (!callback || typeof callback !== 'function') {
throw 'callback function required';
}
_email = email;
var emailObj = {};
emailObj[userEmailKey] = email;
_userId = userId;
var userIdObj = {};
userIdObj[userIdKey] = userId;
chrome.storage.local.set(userIdObj, function () {
chrome.storage.local.set(emailObj, function () {
callback();
});
});
};
UserService.prototype.setSecurityStamp = function (stamp) {
var deferred = Q.defer();
if (stamp === undefined) {
deferred.resolve();
return deferred.promise;
}
_stamp = stamp;
var stampObj = {};
stampObj[stampKey] = stamp;
chrome.storage.local.set(stampObj, function () {
deferred.resolve();
});
return deferred.promise;
};
UserService.prototype.getUserId = function (callback) {
if (!callback || typeof callback !== 'function') {
throw 'callback function required';
}
if (_userId) {
return callback(_userId);
}
chrome.storage.local.get(userIdKey, function (obj) {
if (obj && obj[userIdKey]) {
_userId = obj[userIdKey];
}
return callback(_userId);
});
};
UserService.prototype.getUserIdPromise = function () {
if (_userId) {
return Q(_userId);
}
return utilsService.getObjFromStorage(userIdKey).then(function (obj) {
if (obj) {
_userId = obj;
}
return _userId;
});
};
UserService.prototype.getEmail = function (callback) {
if (!callback || typeof callback !== 'function') {
throw 'callback function required';
}
if (_email) {
return callback(_email);
}
chrome.storage.local.get(userEmailKey, function (obj) {
if (obj && obj[userEmailKey]) {
_email = obj[userEmailKey];
}
return callback(_email);
});
};
UserService.prototype.getSecurityStamp = function () {
var deferred = Q.defer();
if (_stamp) {
deferred.resolve(_stamp);
}
chrome.storage.local.get(stampKey, function (obj) {
if (obj && obj[stampKey]) {
_stamp = obj[stampKey];
}
deferred.resolve(_stamp);
});
return deferred.promise;
};
UserService.prototype.clear = function () {
var self = this;
return Q.all([
self.utilsService.removeFromStorage(userIdKey),
self.utilsService.removeFromStorage(userEmailKey),
self.utilsService.removeFromStorage(stampKey)
]).then(function () {
_userId = _email = _stamp = null;
});
};
UserService.prototype.isAuthenticated = function (callback) {
if (!callback || typeof callback !== 'function') {
throw 'callback function required';
}
var self = this;
self.tokenService.getToken().then(function (token) {
if (!token) {
callback(false);
}
else {
self.getUserId(function (userId) {
callback(userId !== null);
});
}
});
};
}