1
0
mirror of https://github.com/bitwarden/desktop.git synced 2024-11-07 09:20:06 +01:00

persist user profile information

This commit is contained in:
Kyle Spearrin 2016-09-20 23:30:16 -04:00
parent 34c2340a4d
commit 8b76668f1f
5 changed files with 139 additions and 72 deletions

View File

@ -1,7 +1,7 @@
var cryptoService = new CryptoService();
var tokenService = new TokenService();
var apiService = new ApiService(tokenService);
var userService = new UserService(tokenService, apiService);
var userService = new UserService(tokenService, apiService, cryptoService);
var siteService = new SiteService(cryptoService, userService, apiService);
var folderService = new FolderService(cryptoService, userService, apiService);
var syncService = new SyncService(siteService, folderService, userService, apiService);

View File

@ -32,9 +32,9 @@ var PasswordHintRequest = function (email) {
this.email = email;
};
var TokenTwoFactorRequest = function () {
this.code = null;
this.provider = null;
var TokenTwoFactorRequest = function (code) {
this.code = code;
this.provider = "Authenticator";
this.device = null;
};

View File

@ -20,11 +20,13 @@
$scope.loginPromise = loginService.logIn(model.email, model.masterPassword);
$scope.loginPromise.then(function () {
userService.getUserProfile(function (profile) {
if (false && profile.twoFactor) {
userService.isTwoFactorAuthenticated(function (isTwoFactorAuthenticated) {
if (isTwoFactorAuthenticated) {
$state.go('login.twoFactor');
}
else {
// TODO: do full sync
$state.go('tabs.current', { animation: 'in-slide-left' });
}
});

View File

@ -5,6 +5,7 @@
var _service = {};
_service.logIn = function (email, masterPassword) {
email = email.toLowerCase();
var key = cryptoService.makeKey(masterPassword, email);
var deferred = $q.defer();
cryptoService.hashPassword(masterPassword, key, function (hashedPassword) {
@ -17,9 +18,16 @@
tokenService.setToken(response.token, function () {
cryptoService.setKey(key, function () {
userService.setUserProfile(response.profile, function () {
if (response.profile) {
userService.setUserId(response.profile.id, function () {
userService.setEmail(response.profile.email, function () {
deferred.resolve(response);
});
});
}
else {
deferred.resolve(response);
});
}
});
});
}, function (error) {
@ -29,21 +37,21 @@
return deferred.promise;
};
_service.logInTwoFactor = function (code, provider) {
var request = {
code: code,
provider: provider
};
_service.logInTwoFactor = function (code) {
var request = new TokenTwoFactorRequest(code);
var deferred = $q.defer();
apiService.auth.tokenTwoFactor(request, function (response) {
if (!response || !response.Token) {
apiService.auth.postTokenTwoFactor(request, function (response) {
if (!response || !response.token) {
deferred.reject();
return;
}
tokenService.setToken(response.Token, function () {
userService.setUserProfile(response.Profile, function () {
deferred.resolve(response);
tokenService.setToken(response.token, function () {
userService.setUserId(response.profile.id, function () {
userService.setEmail(response.profile.email, function () {
deferred.resolve(response);
});
});
});
}, function (error) {
@ -56,8 +64,11 @@
_service.logOut = function (callback) {
tokenService.clearToken(function () {
cryptoService.clearKey(function () {
userService.clearUserProfile();
callback();
userService.clearUserId(function () {
userService.clearEmail(function () {
callback();
});
});
});
});
};

View File

@ -1,76 +1,100 @@
function UserService(tokenService, apiService) {
function UserService(tokenService, apiService, cryptoService) {
this.tokenService = tokenService;
this.apiService = apiService;
this.cryptoService = cryptoService;
initUserService();
};
function initUserService() {
var _userProfile = null;
var userIdKey = 'userId',
userEmailKey = 'userEmail';
var _userId = null,
_email = null;
UserService.prototype.setUserId = function (userId, callback) {
if (!callback || typeof callback !== 'function') {
throw 'callback function required';
}
_userId = userId;
var obj = {};
obj[userIdKey] = userId;
chrome.storage.local.set(obj, function () {
callback();
});
};
UserService.prototype.getUserId = function (callback) {
this.getUserProfile(function (profile) {
callback(profile.id);
});
};
UserService.prototype.getUserProfile = function (callback) {
if (!callback || typeof callback !== 'function') {
throw 'callback function required';
}
if (_userProfile) {
callback(_userProfile);
return;
if (_userId) {
return callback(_userId);
}
this.setUserProfile(null, function () {
callback(_userProfile);
chrome.storage.local.get(userIdKey, function (obj) {
if (obj && obj[userIdKey]) {
_userId = obj[userIdKey];
}
return callback(_userId);
});
};
UserService.prototype.setUserProfile = function (profile, callback) {
UserService.prototype.clearUserId = function (callback) {
if (!callback || typeof callback !== 'function') {
throw 'callback function required';
}
this.tokenService.getToken(function (token) {
if (!token) {
return;
}
var decodedToken = this.tokenService.decodeToken(token);
var twoFactor = decodedToken.authmethod === 'TwoFactor';
_userProfile = {
id: decodedToken.nameid,
email: decodedToken.email,
twoFactor: twoFactor
};
if (!twoFactor && profile) {
loadProfile(profile, callback);
}
else if (!twoFactor && !profile) {
this.apiService.getProfile(function (response) {
loadProfile(response, callback);
});
}
});
function loadProfile(profile, callback) {
_userProfile.extended = {
name: profile.name,
twoFactorEnabled: profile.twoFactorEnabled,
culture: profile.culture
};
_userId = null;
chrome.storage.local.remove(userIdKey, function () {
callback();
}
});
};
UserService.prototype.clearUserProfile = function () {
_userProfile = null;
UserService.prototype.setEmail = function (email, callback) {
if (!callback || typeof callback !== 'function') {
throw 'callback function required';
}
_email = email;
var obj = {};
obj[userEmailKey] = email;
chrome.storage.local.set(obj, function () {
callback();
});
};
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.clearEmail = function (callback) {
if (!callback || typeof callback !== 'function') {
throw 'callback function required';
}
_email = null;
chrome.storage.local.remove(userEmailKey, function () {
callback();
});
};
UserService.prototype.isAuthenticated = function (callback) {
@ -78,8 +102,23 @@ function initUserService() {
throw 'callback function required';
}
this.getUserProfile(function (profile) {
callback(profile !== null && !profile.twoFactor);
var self = this;
self.cryptoService.getKey(false, function (key) {
if (!key) {
callback(false);
}
else {
self.tokenService.getToken(function (token) {
if (!token) {
callback(false);
}
else {
self.getUserId(function (userId) {
callback(userId !== null);
});
}
});
}
});
};
@ -88,8 +127,23 @@ function initUserService() {
throw 'callback function required';
}
this.getUserProfile(function (profile) {
callback(profile !== null && profile.twoFactor);
var self = this;
self.cryptoService.getKey(false, function (key) {
if (!key) {
callback(false);
}
else {
self.tokenService.getToken(function (token) {
if (!token) {
callback(false);
}
else {
self.getUserId(function (userId) {
callback(userId === null);
});
}
});
}
});
};
};