promise conversions

This commit is contained in:
Kyle Spearrin 2017-10-17 09:25:22 -04:00
parent 1b66b255f3
commit 0fdee0f13b
5 changed files with 71 additions and 87 deletions

View File

@ -33,7 +33,7 @@ var bg_isBackground = true,
bg_constantsService = new ConstantsService(bg_i18nService); bg_constantsService = new ConstantsService(bg_i18nService);
bg_cryptoService = new CryptoService(bg_constantsService); bg_cryptoService = new CryptoService(bg_constantsService);
bg_tokenService = new TokenService(bg_utilsService); bg_tokenService = new TokenService(bg_utilsService);
bg_appIdService = new AppIdService(); bg_appIdService = new AppIdService(bg_utilsService);
bg_apiService = new ApiService(bg_tokenService, bg_appIdService, bg_utilsService, bg_constantsService, logout); bg_apiService = new ApiService(bg_tokenService, bg_appIdService, bg_utilsService, bg_constantsService, logout);
bg_environmentService = new EnvironmentService(bg_constantsService, bg_apiService); bg_environmentService = new EnvironmentService(bg_constantsService, bg_apiService);
bg_userService = new UserService(bg_tokenService, bg_apiService, bg_cryptoService, bg_utilsService); bg_userService = new UserService(bg_tokenService, bg_apiService, bg_cryptoService, bg_utilsService);

View File

@ -7,64 +7,66 @@
_service.logIn = function (email, masterPassword, twoFactorProvider, twoFactorToken, remember) { _service.logIn = function (email, masterPassword, twoFactorProvider, twoFactorToken, remember) {
email = email.toLowerCase(); email = email.toLowerCase();
var key = cryptoService.makeKey(masterPassword, email); var key = cryptoService.makeKey(masterPassword, email),
var deferred = $q.defer(); deferred = $q.defer(),
cryptoService.hashPassword(masterPassword, key, function (hashedPassword) { deviceRequest = null;
appIdService.getAppId(function (appId) {
tokenService.getTwoFactorToken(email, function (twoFactorRememberedToken) {
var deviceRequest = new DeviceRequest(appId, utilsService);
var request;
if (twoFactorToken && typeof (twoFactorProvider) !== 'undefined' && twoFactorProvider !== null) { appIdService.getAppId().then(function (appId) {
request = new TokenRequest(email, hashedPassword, twoFactorProvider, twoFactorToken, remember, deviceRequest = new DeviceRequest(appId, utilsService);
deviceRequest); return tokenService.getTwoFactorToken(email);
} }).then(function (twoFactorRememberedToken) {
else if (twoFactorRememberedToken) { cryptoService.hashPassword(masterPassword, key, function (hashedPassword) {
request = new TokenRequest(email, hashedPassword, constantsService.twoFactorProvider.remember, var request;
twoFactorRememberedToken, false, deviceRequest);
} if (twoFactorToken && typeof (twoFactorProvider) !== 'undefined' && twoFactorProvider !== null) {
else { request = new TokenRequest(email, hashedPassword, twoFactorProvider, twoFactorToken, remember,
request = new TokenRequest(email, hashedPassword, null, null, false, deviceRequest); deviceRequest);
}
else if (twoFactorRememberedToken) {
request = new TokenRequest(email, hashedPassword, constantsService.twoFactorProvider.remember,
twoFactorRememberedToken, false, deviceRequest);
}
else {
request = new TokenRequest(email, hashedPassword, null, null, false, deviceRequest);
}
apiService.postIdentityToken(request, function (response) {
// success
if (!response || !response.accessToken) {
return;
} }
apiService.postIdentityToken(request, function (response) { if (response.twoFactorToken) {
// success tokenService.setTwoFactorToken(response.twoFactorToken, email, function () { });
if (!response || !response.accessToken) { }
return;
}
if (response.twoFactorToken) { tokenService.setTokens(response.accessToken, response.refreshToken, function () {
tokenService.setTwoFactorToken(response.twoFactorToken, email, function () { }); cryptoService.setKey(key, function () {
} cryptoService.setKeyHash(hashedPassword, function () {
userService.setUserIdAndEmail(tokenService.getUserId(), tokenService.getEmail(),
tokenService.setTokens(response.accessToken, response.refreshToken, function () { function () {
cryptoService.setKey(key, function () { cryptoService.setEncKey(response.key).then(function () {
cryptoService.setKeyHash(hashedPassword, function () { return cryptoService.setEncPrivateKey(response.privateKey);
userService.setUserIdAndEmail(tokenService.getUserId(), tokenService.getEmail(), }).then(function () {
function () { chrome.runtime.sendMessage({ command: 'loggedIn' });
cryptoService.setEncKey(response.key).then(function () { deferred.resolve({
return cryptoService.setEncPrivateKey(response.privateKey); twoFactor: false,
}).then(function () { twoFactorProviders: null
chrome.runtime.sendMessage({ command: 'loggedIn' });
deferred.resolve({
twoFactor: false,
twoFactorProviders: null
});
}); });
}); });
}); });
}); });
}); });
}, function (providers) {
// two factor required
deferred.resolve({
twoFactor: true,
twoFactorProviders: providers
});
}, function (error) {
// error
deferred.reject(error);
}); });
}, function (providers) {
// two factor required
deferred.resolve({
twoFactor: true,
twoFactorProviders: providers
});
}, function (error) {
// error
deferred.reject(error);
}); });
}); });
}); });

View File

@ -44,7 +44,7 @@
return encodeURIComponent(pagePath); return encodeURIComponent(pagePath);
} }
bgPage.bg_appIdService.getAnonymousAppId(function (gaAnonAppId) { bgPage.bg_appIdService.getAnonymousAppId().then(function (gaAnonAppId) {
gaFunc = function (action, param1, param2, param3, param4) { gaFunc = function (action, param1, param2, param3, param4) {
if (action !== 'send' || !param1) { if (action !== 'send' || !param1) {
return; return;

View File

@ -1,35 +1,27 @@
function AppIdService() { function AppIdService(utilsService) {
this.utilsService = utilsService;
initAppIdService(); initAppIdService();
} }
function initAppIdService() { function initAppIdService() {
AppIdService.prototype.getAppId = function (callback) { AppIdService.prototype.getAppId = function () {
if (!callback || typeof callback !== 'function') { return makeAndGetAppId('appId', this);
throw 'callback function required';
}
makeAndGetAppId('appId', callback);
}; };
AppIdService.prototype.getAnonymousAppId = function (callback) { AppIdService.prototype.getAnonymousAppId = function () {
if (!callback || typeof callback !== 'function') { return makeAndGetAppId('anonymousAppId', this);
throw 'callback function required';
}
makeAndGetAppId('anonymousAppId', callback);
}; };
function makeAndGetAppId(key, callback) { function makeAndGetAppId(key, self) {
chrome.storage.local.get(key, function (obj) { return self.utilsService.getObjFromStorage(key).then(function (obj) {
if (obj && obj[key]) { if (obj) {
callback(obj[key]); return obj;
return;
} }
var setObj = {}; var guid = newGuid();
setObj[key] = newGuid(); return self.utilsService.saveObjToStorage(key, guid).then(function () {
chrome.storage.local.set(setObj, function () { return guid;
callback(setObj[key]);
}); });
}); });
} }

View File

@ -1,5 +1,6 @@
function TokenService(utilsService) { function TokenService(utilsService) {
this.utilsService = utilsService; this.utilsService = utilsService;
initTokenService(); initTokenService();
} }
@ -97,20 +98,9 @@ function initTokenService() {
}); });
}; };
TokenService.prototype.getTwoFactorToken = function (email, callback) { TokenService.prototype.getTwoFactorToken = function (email) {
if (!callback || typeof callback !== 'function') { return this.utilsService.getObjFromStorage('twoFactorToken_' + email).then(function (token) {
throw 'callback function required'; return token;
}
var prop = 'twoFactorToken_' + email;
chrome.storage.local.get(prop, function (obj) {
if (obj && obj[prop]) {
callback(obj[prop]);
return;
}
return callback(null);
}); });
}; };