1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-11-23 11:56:00 +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) {
var deferred = Q.defer();
self.tokenService.getToken(function (accessToken) {
if (self.tokenService.tokenNeedsRefresh()) {
self.tokenService.getRefreshToken(function (refreshToken) {
if (!refreshToken || refreshToken === '') {
deferred.reject();
return;
}
$.ajax({
type: 'POST',
url: self.baseUrl + '/connect/token',
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);
self.tokenService.getAuthBearer(function (authBearer) {
self.tokenService.getToken(function (accessToken) {
// handle transferring from old auth bearer
if (authBearer && !accessToken) {
postConnectToken({
grant_type: 'password',
oldAuthBearer: authBearer,
scope: 'api offline_access',
client_id: 'browser'
}, function (token) {
self.tokenService.clearAuthBearer(function () {
tokenService.setTokens(token.accessToken, token.refreshToken, function () {
deferred.resolve(token.accessToken);
});
},
error: function (jqXHR, textStatus, errorThrown) {
deferred.reject(jqXHR);
}
});
}, function (jqXHR) {
deferred.reject(jqXHR);
});
});
}
else {
deferred.resolve(accessToken);
}
} // handle token refresh
else if (self.tokenService.tokenNeedsRefresh()) {
self.tokenService.getRefreshToken(function (refreshToken) {
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
}
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() {
var _token,
_authBearer,
_decodedToken,
_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) {
if (!callback || typeof callback !== 'function') {
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) {
if (!callback || typeof callback !== 'function') {
throw 'callback function required';
}
_token = _decodedToken = _refreshToken = null;
chrome.storage.local.remove('accessToken', function () {
chrome.storage.local.remove('refreshToken', function () {
callback();
_token = _decodedToken = _refreshToken = _authBearer = null;
chrome.storage.local.remove('authBearer', function () {
chrome.storage.local.remove('accessToken', function () {
chrome.storage.local.remove('refreshToken', function () {
callback();
});
});
});
};