1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-11-24 12:06:15 +01:00

handle traferring old auth bearer

This commit is contained in:
Kyle Spearrin 2017-01-21 22:00:02 -05:00
parent e1ed7587dc
commit 4968a00dcf
2 changed files with 95 additions and 33 deletions

View File

@ -338,41 +338,71 @@ function initApiService() {
function handleTokenState(self) { function handleTokenState(self) {
var deferred = Q.defer(); var deferred = Q.defer();
self.tokenService.getToken(function (accessToken) { self.tokenService.getAuthBearer(function (authBearer) {
if (self.tokenService.tokenNeedsRefresh()) { self.tokenService.getToken(function (accessToken) {
self.tokenService.getRefreshToken(function (refreshToken) { // handle transferring from old auth bearer
if (!refreshToken || refreshToken === '') { if (authBearer && !accessToken) {
deferred.reject(); postConnectToken({
return; grant_type: 'password',
} oldAuthBearer: authBearer,
scope: 'api offline_access',
$.ajax({ client_id: 'browser'
type: 'POST', }, function (token) {
url: self.baseUrl + '/connect/token', self.tokenService.clearAuthBearer(function () {
data: {
grant_type: 'refresh_token',
client_id: 'browser',
refresh_token: refreshToken
},
contentType: 'application/x-www-form-urlencoded; charset=utf-8',
dataType: 'json',
success: function (response) {
var token = new IdentityTokenResponse(response);
tokenService.setTokens(token.accessToken, token.refreshToken, function () { tokenService.setTokens(token.accessToken, token.refreshToken, function () {
deferred.resolve(token.accessToken); deferred.resolve(token.accessToken);
}); });
}, });
error: function (jqXHR, textStatus, errorThrown) { }, function (jqXHR) {
deferred.reject(jqXHR); deferred.reject(jqXHR);
}
}); });
}); } // handle token refresh
} else if (self.tokenService.tokenNeedsRefresh()) {
else { self.tokenService.getRefreshToken(function (refreshToken) {
deferred.resolve(accessToken); if (!refreshToken || refreshToken === '') {
} deferred.reject();
return;
}
postConnectToken({
grant_type: 'refresh_token',
client_id: 'browser',
refresh_token: refreshToken
}, function (token) {
tokenService.setTokens(token.accessToken, token.refreshToken, function () {
deferred.resolve(token.accessToken);
});
}, function (jqXHR) {
deferred.reject(jqXHR);
});
});
}
else {
if (authBearer) {
self.tokenService.clearAuthBearer(function () { });
}
deferred.resolve(accessToken);
}
});
}); });
return deferred.promise return deferred.promise
} }
function postConnectToken(data, success, error) {
$.ajax({
type: 'POST',
url: self.baseUrl + '/connect/token',
data: data,
contentType: 'application/x-www-form-urlencoded; charset=utf-8',
dataType: 'json',
success: function (response) {
success(new IdentityTokenResponse(response));
},
error: function (jqXHR, textStatus, errorThrown) {
error(jqXHR);
}
});
}
}; };

View File

@ -4,6 +4,7 @@
function initTokenService() { function initTokenService() {
var _token, var _token,
_authBearer,
_decodedToken, _decodedToken,
_refreshToken; _refreshToken;
@ -52,6 +53,24 @@ 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';
@ -83,15 +102,28 @@ 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 = null; _token = _decodedToken = _refreshToken = _authBearer = null;
chrome.storage.local.remove('accessToken', function () { chrome.storage.local.remove('authBearer', function () {
chrome.storage.local.remove('refreshToken', function () { chrome.storage.local.remove('accessToken', function () {
callback(); chrome.storage.local.remove('refreshToken', function () {
callback();
});
}); });
}); });
}; };