From 19a59e61774faa965f560948e51d779db530831f Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Thu, 31 Aug 2017 14:17:52 -0400 Subject: [PATCH] remove old auth bearer code. move sync methods --- src/services/apiService.js | 50 ++++++++---------------------------- src/services/syncService.js | 38 +++++++++++++-------------- src/services/tokenService.js | 40 +++-------------------------- src/services/userService.js | 18 ++++++------- 4 files changed, 41 insertions(+), 105 deletions(-) diff --git a/src/services/apiService.js b/src/services/apiService.js index c1df3da573..b910d222c4 100644 --- a/src/services/apiService.js +++ b/src/services/apiService.js @@ -516,47 +516,17 @@ function initApiService() { function handleTokenState(self) { var deferred = Q.defer(); - self.tokenService.getAuthBearer(function (authBearer) { - self.tokenService.getToken(function (accessToken) { - // handle transferring from old auth bearer - if (authBearer && !accessToken) { - self.appIdService.getAppId(function (appId) { - postConnectToken(self, { - grant_type: 'password', - oldAuthBearer: authBearer, - username: 'abcdefgh', // has to be something - password: 'abcdefgh', // has to be something - scope: 'api offline_access', - client_id: 'browser', - deviceIdentifier: appId, - deviceType: self.utilsService.getDeviceType(), - deviceName: self.utilsService.getBrowser() - }, function (token) { - self.tokenService.clearAuthBearer(function () { - self.tokenService.setTokens(token.accessToken, token.refreshToken, function () { - resolveTokenQs(token.accessToken, self, deferred); - }); - }); - }, function (jqXHR) { - deferred.reject(jqXHR); - }); - }); - } // handle token refresh - else if (self.tokenService.tokenNeedsRefresh()) { - refreshToken(self, function (accessToken) { - resolveTokenQs(accessToken, self, deferred); - }, function (err) { - deferred.reject(err); - }); - } - else { - if (authBearer) { - self.tokenService.clearAuthBearer(function () { }); - } - + self.tokenService.getToken(function (accessToken) { + if (self.tokenService.tokenNeedsRefresh()) { + refreshToken(self, function (accessToken) { resolveTokenQs(accessToken, self, deferred); - } - }); + }, function (err) { + deferred.reject(err); + }); + } + else { + resolveTokenQs(accessToken, self, deferred); + } }); return deferred.promise; diff --git a/src/services/syncService.js b/src/services/syncService.js index 8f975adf71..387c54168d 100644 --- a/src/services/syncService.js +++ b/src/services/syncService.js @@ -28,23 +28,23 @@ function initSyncService() { return; } - self.userService.getUserId(function (userId) { - var now = new Date(); - needsSyncing(self, forceSync, function (needsSync, skipped) { - if (skipped) { + var now = new Date(); + needsSyncing(self, forceSync, function (needsSync, skipped) { + if (skipped) { + self.syncCompleted(false); + callback(false); + return; + } + + if (!needsSync) { + self.setLastSync(now, function () { self.syncCompleted(false); callback(false); - return; - } - - if (!needsSync) { - self.setLastSync(now, function () { - self.syncCompleted(false); - callback(false); - }); - return; - } + }); + return; + } + self.userService.getUserId(function (userId) { syncProfile(self).then(function () { return syncFolders(self, userId); }).then(function () { @@ -75,18 +75,18 @@ function initSyncService() { return; } - self.getLastSync(function (lastSync) { - self.apiService.getAccountRevisionDate(function (response) { - var accountRevisionDate = new Date(response); + self.apiService.getAccountRevisionDate(function (response) { + var accountRevisionDate = new Date(response); + self.getLastSync(function (lastSync) { if (lastSync && accountRevisionDate <= lastSync) { callback(false, false); return; } callback(true, false); - }, function () { - callback(false, true); }); + }, function () { + callback(false, true); }); } diff --git a/src/services/tokenService.js b/src/services/tokenService.js index 12c1c8ac2e..c7291fb00a 100644 --- a/src/services/tokenService.js +++ b/src/services/tokenService.js @@ -4,7 +4,6 @@ function initTokenService() { var _token, - _authBearer, _decodedToken, _refreshToken; @@ -53,24 +52,6 @@ function initTokenService() { }); }; - TokenService.prototype.getAuthBearer = function (callback) { - if (!callback || typeof callback !== 'function') { - throw 'callback function required'; - } - - if (_authBearer) { - return callback(_authBearer); - } - - chrome.storage.local.get('authBearer', function (obj) { - if (obj && obj.authBearer) { - _authBearer = obj.authBearer; - } - - return callback(_authBearer); - }); - }; - TokenService.prototype.setRefreshToken = function (refreshToken, callback) { if (!callback || typeof callback !== 'function') { throw 'callback function required'; @@ -142,28 +123,15 @@ function initTokenService() { }); }; - TokenService.prototype.clearAuthBearer = function (callback) { - if (!callback || typeof callback !== 'function') { - throw 'callback function required'; - } - - _authBearer = null; - chrome.storage.local.remove('authBearer', function () { - callback(); - }); - }; - TokenService.prototype.clearToken = function (callback) { if (!callback || typeof callback !== 'function') { throw 'callback function required'; } - _token = _decodedToken = _refreshToken = _authBearer = null; - chrome.storage.local.remove('authBearer', function () { - chrome.storage.local.remove('accessToken', function () { - chrome.storage.local.remove('refreshToken', function () { - callback(); - }); + _token = _decodedToken = _refreshToken = null; + chrome.storage.local.remove('accessToken', function () { + chrome.storage.local.remove('refreshToken', function () { + callback(); }); }); }; diff --git a/src/services/userService.js b/src/services/userService.js index 408d039b80..d7b2046ace 100644 --- a/src/services/userService.js +++ b/src/services/userService.js @@ -130,16 +130,14 @@ function initUserService() { var self = this; self.tokenService.getToken(function (token) { - self.tokenService.getAuthBearer(function (authBearer) { - if (!token && !authBearer) { - callback(false); - } - else { - self.getUserId(function (userId) { - callback(userId !== null); - }); - } - }); + if (!token) { + callback(false); + } + else { + self.getUserId(function (userId) { + callback(userId !== null); + }); + } }); }; }