1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-11-09 09:51:02 +01:00

remove old auth bearer code. move sync methods

This commit is contained in:
Kyle Spearrin 2017-08-31 14:17:52 -04:00
parent eae2efea67
commit 19a59e6177
4 changed files with 41 additions and 105 deletions

View File

@ -516,47 +516,17 @@ function initApiService() {
function handleTokenState(self) { function handleTokenState(self) {
var deferred = Q.defer(); var deferred = Q.defer();
self.tokenService.getAuthBearer(function (authBearer) { self.tokenService.getToken(function (accessToken) {
self.tokenService.getToken(function (accessToken) { if (self.tokenService.tokenNeedsRefresh()) {
// handle transferring from old auth bearer refreshToken(self, function (accessToken) {
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 () { });
}
resolveTokenQs(accessToken, self, deferred); resolveTokenQs(accessToken, self, deferred);
} }, function (err) {
}); deferred.reject(err);
});
}
else {
resolveTokenQs(accessToken, self, deferred);
}
}); });
return deferred.promise; return deferred.promise;

View File

@ -28,23 +28,23 @@ function initSyncService() {
return; return;
} }
self.userService.getUserId(function (userId) { var now = new Date();
var now = new Date(); needsSyncing(self, forceSync, function (needsSync, skipped) {
needsSyncing(self, forceSync, function (needsSync, skipped) { if (skipped) {
if (skipped) { self.syncCompleted(false);
callback(false);
return;
}
if (!needsSync) {
self.setLastSync(now, function () {
self.syncCompleted(false); self.syncCompleted(false);
callback(false); callback(false);
return; });
} return;
}
if (!needsSync) {
self.setLastSync(now, function () {
self.syncCompleted(false);
callback(false);
});
return;
}
self.userService.getUserId(function (userId) {
syncProfile(self).then(function () { syncProfile(self).then(function () {
return syncFolders(self, userId); return syncFolders(self, userId);
}).then(function () { }).then(function () {
@ -75,18 +75,18 @@ function initSyncService() {
return; return;
} }
self.getLastSync(function (lastSync) { self.apiService.getAccountRevisionDate(function (response) {
self.apiService.getAccountRevisionDate(function (response) { var accountRevisionDate = new Date(response);
var accountRevisionDate = new Date(response); self.getLastSync(function (lastSync) {
if (lastSync && accountRevisionDate <= lastSync) { if (lastSync && accountRevisionDate <= lastSync) {
callback(false, false); callback(false, false);
return; return;
} }
callback(true, false); callback(true, false);
}, function () {
callback(false, true);
}); });
}, function () {
callback(false, true);
}); });
} }

View File

@ -4,7 +4,6 @@
function initTokenService() { function initTokenService() {
var _token, var _token,
_authBearer,
_decodedToken, _decodedToken,
_refreshToken; _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) { TokenService.prototype.setRefreshToken = function (refreshToken, callback) {
if (!callback || typeof callback !== 'function') { if (!callback || typeof callback !== 'function') {
throw 'callback function required'; 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) { TokenService.prototype.clearToken = function (callback) {
if (!callback || typeof callback !== 'function') { if (!callback || typeof callback !== 'function') {
throw 'callback function required'; throw 'callback function required';
} }
_token = _decodedToken = _refreshToken = _authBearer = null; _token = _decodedToken = _refreshToken = null;
chrome.storage.local.remove('authBearer', function () { chrome.storage.local.remove('accessToken', function () {
chrome.storage.local.remove('accessToken', function () { chrome.storage.local.remove('refreshToken', function () {
chrome.storage.local.remove('refreshToken', function () { callback();
callback();
});
}); });
}); });
}; };

View File

@ -130,16 +130,14 @@ function initUserService() {
var self = this; var self = this;
self.tokenService.getToken(function (token) { self.tokenService.getToken(function (token) {
self.tokenService.getAuthBearer(function (authBearer) { if (!token) {
if (!token && !authBearer) { callback(false);
callback(false); }
} else {
else { self.getUserId(function (userId) {
self.getUserId(function (userId) { callback(userId !== null);
callback(userId !== null); });
}); }
}
});
}); });
}; };
} }