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_cryptoService = new CryptoService(bg_constantsService);
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_environmentService = new EnvironmentService(bg_constantsService, bg_apiService);
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) {
email = email.toLowerCase();
var key = cryptoService.makeKey(masterPassword, email);
var deferred = $q.defer();
cryptoService.hashPassword(masterPassword, key, function (hashedPassword) {
appIdService.getAppId(function (appId) {
tokenService.getTwoFactorToken(email, function (twoFactorRememberedToken) {
var deviceRequest = new DeviceRequest(appId, utilsService);
var request;
var key = cryptoService.makeKey(masterPassword, email),
deferred = $q.defer(),
deviceRequest = null;
if (twoFactorToken && typeof (twoFactorProvider) !== 'undefined' && twoFactorProvider !== null) {
request = new TokenRequest(email, hashedPassword, twoFactorProvider, twoFactorToken, remember,
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);
appIdService.getAppId().then(function (appId) {
deviceRequest = new DeviceRequest(appId, utilsService);
return tokenService.getTwoFactorToken(email);
}).then(function (twoFactorRememberedToken) {
cryptoService.hashPassword(masterPassword, key, function (hashedPassword) {
var request;
if (twoFactorToken && typeof (twoFactorProvider) !== 'undefined' && twoFactorProvider !== null) {
request = new TokenRequest(email, hashedPassword, twoFactorProvider, twoFactorToken, remember,
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) {
// success
if (!response || !response.accessToken) {
return;
}
if (response.twoFactorToken) {
tokenService.setTwoFactorToken(response.twoFactorToken, email, function () { });
}
if (response.twoFactorToken) {
tokenService.setTwoFactorToken(response.twoFactorToken, email, function () { });
}
tokenService.setTokens(response.accessToken, response.refreshToken, function () {
cryptoService.setKey(key, function () {
cryptoService.setKeyHash(hashedPassword, function () {
userService.setUserIdAndEmail(tokenService.getUserId(), tokenService.getEmail(),
function () {
cryptoService.setEncKey(response.key).then(function () {
return cryptoService.setEncPrivateKey(response.privateKey);
}).then(function () {
chrome.runtime.sendMessage({ command: 'loggedIn' });
deferred.resolve({
twoFactor: false,
twoFactorProviders: null
});
tokenService.setTokens(response.accessToken, response.refreshToken, function () {
cryptoService.setKey(key, function () {
cryptoService.setKeyHash(hashedPassword, function () {
userService.setUserIdAndEmail(tokenService.getUserId(), tokenService.getEmail(),
function () {
cryptoService.setEncKey(response.key).then(function () {
return cryptoService.setEncPrivateKey(response.privateKey);
}).then(function () {
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);
}
bgPage.bg_appIdService.getAnonymousAppId(function (gaAnonAppId) {
bgPage.bg_appIdService.getAnonymousAppId().then(function (gaAnonAppId) {
gaFunc = function (action, param1, param2, param3, param4) {
if (action !== 'send' || !param1) {
return;

View File

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

View File

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