Commit 4e51fe3e authored by Sean Hammond's avatar Sean Hammond

Don't reuse OAuth grant tokens

Don't reuse the OAuth grant token if the access token has expired.

The grant token is only intended to be used once, and if the access
token has expired then the grant token will likely have expired as well anyway.

A later commit will add support for refreshing an expired access token
using its refresh token, instead.
parent 480e1bae
...@@ -41,16 +41,12 @@ function auth($http, settings) { ...@@ -41,16 +41,12 @@ function auth($http, settings) {
} }
function tokenGetter() { function tokenGetter() {
// performance.now() is used instead of Date.now() because it is if (cachedToken) {
// monotonically increasing.
if (cachedToken && cachedToken.expiresAt > performance.now()) {
return Promise.resolve(cachedToken.token); return Promise.resolve(cachedToken.token);
} else if (grantToken) { } else if (grantToken) {
var refreshStart = performance.now();
return exchangeToken(grantToken).then(function (tokenInfo) { return exchangeToken(grantToken).then(function (tokenInfo) {
cachedToken = { cachedToken = {
token: tokenInfo.access_token, token: tokenInfo.access_token,
expiresAt: refreshStart + tokenInfo.expires_in * 1000,
}; };
return cachedToken.token; return cachedToken.token;
}); });
......
...@@ -72,23 +72,6 @@ describe('oauth auth', function () { ...@@ -72,23 +72,6 @@ describe('oauth auth', function () {
assert.equal(token, null); assert.equal(token, null);
}); });
}); });
it('should refresh the access token if it has expired', function () {
return auth.tokenGetter().then(function () {
var now = performance.now();
nowStub.returns(now + DEFAULT_TOKEN_EXPIRES_IN_SECS * 1000 + 100);
fakeHttp.post.returns(Promise.resolve({
status: 200,
data: {
access_token: 'a-different-access-token',
expires_in: DEFAULT_TOKEN_EXPIRES_IN_SECS,
},
}));
return auth.tokenGetter();
}).then(function (token) {
assert.equal(token, 'a-different-access-token');
});
});
}); });
describe('#clearCache', function () { describe('#clearCache', function () {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment