From 79860da28c42bbf40afd6b29a1a0173c792053a2 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Sat, 3 Sep 2016 15:44:32 -0400 Subject: [PATCH] move api services to background page. wire up log in and log out. --- src/models/api/requestModels.js | 9 ++-- src/models/api/responseModels.js | 5 +- .../app/accounts/accountsLoginController.js | 2 +- src/popup/app/global/tabs.html | 2 +- src/popup/app/services/apiService.js | 32 ------------ src/popup/app/services/backgroundService.js | 3 ++ src/popup/app/services/loginService.js | 19 +++---- src/popup/app/settings/settingsController.js | 8 ++- src/popup/app/settings/views/settings.html | 16 ++++-- src/popup/index.html | 6 ++- src/services/apiService.js | 49 +++++++++++++++++-- 11 files changed, 90 insertions(+), 61 deletions(-) delete mode 100644 src/popup/app/services/apiService.js diff --git a/src/models/api/requestModels.js b/src/models/api/requestModels.js index 5f4f6751ab..f3212c2b73 100644 --- a/src/models/api/requestModels.js +++ b/src/models/api/requestModels.js @@ -12,9 +12,12 @@ var FolderRequest = function (folder) { this.name = folder.name ? folder.name.encryptedString : null; }; -var TokenRequest = function () { - this.email = null; - this.masterPasswordHash = null; +var TokenRequest = function (email, masterPasswordHash, device) { + this.email = email; + this.masterPasswordHash = masterPasswordHash; + if (device) { + this.device = new DeviceRequest(device); + } this.device = null; }; diff --git a/src/models/api/responseModels.js b/src/models/api/responseModels.js index db8514fc71..a569ec48ef 100644 --- a/src/models/api/responseModels.js +++ b/src/models/api/responseModels.js @@ -51,8 +51,9 @@ var ListResponse = function (data) { }; var ErrorResponse = function (response) { - this.message = response.Message; - this.validationErrors = response.ValidationErrors; + this.message = response.responseJSON.Message; + this.validationErrors = response.responseJSON.ValidationErrors; + this.statusCode = response.status; }; var DeviceResponse = function (response) { diff --git a/src/popup/app/accounts/accountsLoginController.js b/src/popup/app/accounts/accountsLoginController.js index 1768c33006..14183cded6 100644 --- a/src/popup/app/accounts/accountsLoginController.js +++ b/src/popup/app/accounts/accountsLoginController.js @@ -7,7 +7,7 @@ $scope.loginPromise.then(function () { userService.getUserProfile(function (profile) { - if (profile.twoFactor) { + if (false && profile.twoFactor) { $state.go('login.twoFactor'); } else { diff --git a/src/popup/app/global/tabs.html b/src/popup/app/global/tabs.html index 1e7a34a846..b732cd8aee 100644 --- a/src/popup/app/global/tabs.html +++ b/src/popup/app/global/tabs.html @@ -8,7 +8,7 @@ - + diff --git a/src/popup/app/services/apiService.js b/src/popup/app/services/apiService.js deleted file mode 100644 index b6eaea7226..0000000000 --- a/src/popup/app/services/apiService.js +++ /dev/null @@ -1,32 +0,0 @@ -angular - .module('bit.services') - - .factory('apiService', function ($resource, appSettings) { - var _service = {}, - _apiUri = appSettings.apiUri; - - _service.sites = $resource(_apiUri + '/sites/:id', {}, { - post: { method: 'POST', params: {} }, - put: { method: 'POST', params: { id: '@id' } }, - del: { url: _apiUri + '/sites/:id/delete', method: 'POST', params: { id: '@id' } } - }); - - _service.folders = $resource(_apiUri + '/folders/:id', {}, { - post: { method: 'POST', params: {} }, - put: { method: 'POST', params: { id: '@id' } }, - del: { url: _apiUri + '/folders/:id/delete', method: 'POST', params: { id: '@id' } } - }); - - _service.accounts = $resource(_apiUri + '/accounts', {}, { - register: { url: _apiUri + '/accounts/register', method: 'POST', params: {} }, - getProfile: { url: _apiUri + '/accounts/profile', method: 'GET', params: {} }, - postPasswordHint: { url: _apiUri + '/accounts/password-hint', method: 'POST', params: {} } - }); - - _service.auth = $resource(_apiUri + '/auth', {}, { - token: { url: _apiUri + '/auth/token', method: 'POST', params: {} }, - tokenTwoFactor: { url: _apiUri + '/auth/token/two-factor', method: 'POST', params: {} } - }); - - return _service; - }); diff --git a/src/popup/app/services/backgroundService.js b/src/popup/app/services/backgroundService.js index 1216809967..a47484361f 100644 --- a/src/popup/app/services/backgroundService.js +++ b/src/popup/app/services/backgroundService.js @@ -9,4 +9,7 @@ }) .factory('userService', function () { return chrome.extension.getBackgroundPage().userService; + }) + .factory('apiService', function () { + return chrome.extension.getBackgroundPage().apiService; }); diff --git a/src/popup/app/services/loginService.js b/src/popup/app/services/loginService.js index ca9620dce1..918520c9f7 100644 --- a/src/popup/app/services/loginService.js +++ b/src/popup/app/services/loginService.js @@ -1,26 +1,22 @@ angular .module('bit.services') - .factory('loginService', function (cryptoService, apiService, userService, tokenService, $q) { + .factory('loginService', function (cryptoService, apiService, apiService, userService, tokenService, $q) { var _service = {}; _service.logIn = function (email, masterPassword) { var key = cryptoService.makeKey(masterPassword, email); - - var request = { - email: email, - masterPasswordHash: cryptoService.hashPassword(masterPassword, key) - }; + var request = new TokenRequest(email, cryptoService.hashPassword(masterPassword, key)); var deferred = $q.defer(); - apiService.auth.token(request, function (response) { - if (!response || !response.Token) { + apiService.postToken(request, function (response) { + if (!response || !response.token) { return; } - tokenService.setToken(response.Token, function () { + tokenService.setToken(response.token, function () { cryptoService.setKey(key, function () { - userService.setUserProfile(response.Profile, function () { + userService.setUserProfile(response.profile, function () { deferred.resolve(response); }); }); @@ -56,10 +52,11 @@ return deferred.promise; }; - _service.logOut = function () { + _service.logOut = function (callback) { tokenService.clearToken(function () { cryptoService.clearKey(function () { userService.clearUserProfile(); + callback(); }); }); }; diff --git a/src/popup/app/settings/settingsController.js b/src/popup/app/settings/settingsController.js index ad0c63ebca..3fcc35e4d0 100644 --- a/src/popup/app/settings/settingsController.js +++ b/src/popup/app/settings/settingsController.js @@ -1,6 +1,10 @@ angular .module('bit.settings') - .controller('settingsController', function ($scope) { - + .controller('settingsController', function ($scope, loginService, $state) { + $scope.logOut = function (model) { + loginService.logOut(function () { + $state.go('login'); + }); + }; }); diff --git a/src/popup/app/settings/views/settings.html b/src/popup/app/settings/views/settings.html index 51f7e748f4..1b65c77e11 100644 --- a/src/popup/app/settings/views/settings.html +++ b/src/popup/app/settings/views/settings.html @@ -1,7 +1,15 @@  - -

- Some content for your settings. -

+ +
+
+ Current Session +
+ + Lock + + + Log out + +
diff --git a/src/popup/index.html b/src/popup/index.html index 099cbd59c1..b4af529bf8 100644 --- a/src/popup/index.html +++ b/src/popup/index.html @@ -14,9 +14,13 @@ + + + + + - diff --git a/src/services/apiService.js b/src/services/apiService.js index 6f67929017..6301f2ada7 100644 --- a/src/services/apiService.js +++ b/src/services/apiService.js @@ -4,6 +4,42 @@ }; !function () { + // Auth APIs + + ApiService.prototype.postToken = function (tokenRequest, success, error) { + var self = this; + $.ajax({ + type: 'POST', + url: self.baseUrl + '/auth/token', + data: JSON.stringify(tokenRequest), + contentType: "application/json; charset=utf-8", + dataType: 'json', + success: function (response) { + success(new TokenResponse(response)) + }, + error: function (jqXHR, textStatus, errorThrown) { + handleError(error, jqXHR, textStatus, errorThrown); + } + }); + }; + + ApiService.prototype.postTokenTwoFactor = function (twoFactorTokenRequest, success, error) { + var self = this; + $.ajax({ + type: 'POST', + url: self.baseUrl + '/auth/token/two-factor', + data: JSON.stringify(twoFactorTokenRequest), + contentType: "application/json; charset=utf-8", + dataType: 'json', + success: function (response) { + success(new TokenResponse(response)) + }, + error: function (jqXHR, textStatus, errorThrown) { + handleError(error, jqXHR, textStatus, errorThrown); + } + }); + }; + // Account APIs ApiService.prototype.getProfile = function (success, error) { @@ -48,7 +84,8 @@ $.ajax({ type: 'POST', url: self.baseUrl + '/sites?access_token=' + token, - data: siteRequest, + data: JSON.stringify(siteRequest), + contentType: "application/json; charset=utf-8", dataType: 'json', success: function (response) { success(new SiteResponse(response)) @@ -66,7 +103,8 @@ $.ajax({ type: 'POST', url: self.baseUrl + '/sites/' + id + '?access_token=' + token, - data: siteRequest, + data: JSON.stringify(siteRequest), + contentType: "application/json; charset=utf-8", dataType: 'json', success: function (response) { success(new SiteResponse(response)) @@ -103,7 +141,8 @@ $.ajax({ type: 'POST', url: self.baseUrl + '/folders?access_token=' + token, - data: folderRequest, + data: JSON.stringify(folderRequest), + contentType: "application/json; charset=utf-8", dataType: 'json', success: function (response) { success(new FolderResponse(response)) @@ -121,7 +160,8 @@ $.ajax({ type: 'POST', url: self.baseUrl + '/folders/' + id + '?access_token=' + token, - data: folderRequest, + data: JSON.stringify(folderRequest), + contentType: "application/json; charset=utf-8", dataType: 'json', success: function (response) { success(new FolderResponse(response)) @@ -180,6 +220,7 @@ $.ajax({ type: 'POST', url: self.baseUrl + '/ciphers/' + id + '/delete?access_token=' + token, + contentType: "application/json; charset=utf-8", dataType: 'json', success: success, error: function (jqXHR, textStatus, errorThrown) {