From 8fafe2bd6d845112c6948cf114f8381fa257f0b5 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Fri, 2 Sep 2016 01:20:41 -0400 Subject: [PATCH] Few fixes to services and login --- .../app/accounts/accountsLoginController.js | 4 +- src/browser_action/index.html | 2 +- src/package.json | 3 +- src/services/authService.js | 56 +++++++++++++++++-- src/services/cryptoService.js | 2 +- src/services/tokenService.js | 14 ++++- 6 files changed, 72 insertions(+), 9 deletions(-) diff --git a/src/browser_action/app/accounts/accountsLoginController.js b/src/browser_action/app/accounts/accountsLoginController.js index 9c6bf4eb32..a3ba091a9a 100644 --- a/src/browser_action/app/accounts/accountsLoginController.js +++ b/src/browser_action/app/accounts/accountsLoginController.js @@ -3,7 +3,9 @@ .controller('accountsLoginController', function ($scope, $state) { $scope.login = function (model) { - $state.go('tabs.current'); + g_authService.logIn(model.email, model.masterPassword, function () { + $state.go('tabs.current'); + }); }; $scope.twoFactor = function (model) { diff --git a/src/browser_action/index.html b/src/browser_action/index.html index 032fd33b81..4dac44e5b9 100644 --- a/src/browser_action/index.html +++ b/src/browser_action/index.html @@ -8,7 +8,7 @@ - + diff --git a/src/package.json b/src/package.json index a700d5b960..861d05266a 100644 --- a/src/package.json +++ b/src/package.json @@ -3,6 +3,7 @@ "version": "0.0.1", "devDependencies": { "ionic-framework-v1": "1.3.1", - "sjcl": "1.0.3" + "sjcl": "1.0.3", + "angular-jwt": "0.0.9" } } diff --git a/src/services/authService.js b/src/services/authService.js index 2dbcb53dda..8f29b9dc1d 100644 --- a/src/services/authService.js +++ b/src/services/authService.js @@ -1,15 +1,47 @@ var g_authService = function () { var _service = {}, _userProfile = null; - _service.logIn = function (email, masterPassword) { - return; + _service.logIn = function (email, masterPassword, callback) { + if (!callback || typeof callback !== 'function') { + throw 'callback function required'; + } + + var key = g_cryptoService.makeKey(masterPassword, email); + + var request = { + email: email, + masterPasswordHash: g_cryptoService.hashPassword(masterPassword, key) + }; + + var response = { + Token: "", + Profile: { + + } + }; + + g_tokenService.setToken(response.Token, function () { + g_cryptoService.setKey(key, function () { + _service.setUserProfile(response.Profile, function () { + callback(); + }); + }); + }); }; - _service.logInTwoFactor = function (code, provider) { + _service.logInTwoFactor = function (code, provider, callback) { + if (!callback || typeof callback !== 'function') { + throw 'callback function required'; + } + return; }; _service.logOut = function (callback) { + if (!callback || typeof callback !== 'function') { + throw 'callback function required'; + } + g_tokenService.clearToken(function () { g_cryptoService.clearKey(function () { _userProfile = null; @@ -19,6 +51,10 @@ }; _service.getUserProfile = function (callback) { + if (!callback || typeof callback !== 'function') { + throw 'callback function required'; + } + if (!_userProfile) { _service.setUserProfile(null, function () { callback(_userProfile); @@ -29,6 +65,10 @@ }; _service.setUserProfile = function (profile, callback) { + if (!callback || typeof callback !== 'function') { + throw 'callback function required'; + } + g_tokenService.getToken(function (token) { if (!token) { return; @@ -63,16 +103,24 @@ } _service.isAuthenticated = function (callback) { + if (!callback || typeof callback !== 'function') { + throw 'callback function required'; + } + callback(_service.getUserProfile(function (profile) { return profile !== null && !profile.twoFactor; })); }; _service.isTwoFactorAuthenticated = function (callback) { + if (!callback || typeof callback !== 'function') { + throw 'callback function required'; + } + callback(_service.getUserProfile(function (profile) { return profile !== null && profile.twoFactor; })); }; return _service; -}; +}(); diff --git a/src/services/cryptoService.js b/src/services/cryptoService.js index 12de9f4e3e..33370a208c 100644 --- a/src/services/cryptoService.js +++ b/src/services/cryptoService.js @@ -125,4 +125,4 @@ }; return _service; -}; +}(); diff --git a/src/services/tokenService.js b/src/services/tokenService.js index 77446d468a..2ba7b09b37 100644 --- a/src/services/tokenService.js +++ b/src/services/tokenService.js @@ -2,6 +2,10 @@ var _service = {}, _token; _service.setToken = function (token, callback) { + if (!callback || typeof callback !== 'function') { + throw 'callback function required'; + } + _token = token; chrome.storage.local.set({ 'authBearer': token @@ -11,6 +15,10 @@ }; _service.getToken = function (callback) { + if (!callback || typeof callback !== 'function') { + throw 'callback function required'; + } + if (_token) { return callback(_token); } @@ -25,6 +33,10 @@ }; _service.clearToken = function (callback) { + if (!callback || typeof callback !== 'function') { + throw 'callback function required'; + } + _token = null; chrome.storage.local.remove('authBearer', function () { callback(); @@ -32,4 +44,4 @@ }; return _service; -}; +}();