Skip to content

Commit 550cbaf

Browse files
committed
allow tokens with no expires_at for retro compat
1 parent 941f54d commit 550cbaf

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

__tests__/TokenStorage.test.js

+30-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import TokenGeneratorMock from '../__mocks__/TokenGeneratorMock';
44
import oauthClientCredentialsMock from '../__mocks__/oauthClientCredentials.json';
55
import refreshedCredentials from '../__mocks__/refreshedCredentials.json';
66
import Storage from '../__mocks__/mockStorage';
7-
import { NOW_TIMESTAMP_MOCK } from '../setupJest';
87

98
global.FormData = require('form-data');
109

@@ -133,8 +132,35 @@ describe('Token storage tests', () => {
133132
grant_type: 'client_credentials',
134133
});
135134

136-
expect(oauth.getCurrentTokenExpiresIn()).resolves.toEqual(3600);
137-
await oauth.refreshToken();
138-
expect(oauth.getCurrentTokenExpiresIn()).resolves.toEqual(12800);
135+
return oauth.getCurrentTokenExpiresIn().then((value) => {
136+
expect(value).toEqual(3600);
137+
return oauth.refreshToken().then(() => {
138+
return oauth.getCurrentTokenExpiresIn().then((val) => {
139+
expect(val).toEqual(12800);
140+
})
141+
});
142+
})
143+
});
144+
145+
test('remaining validity time is null for a token with no expiresAt', async () => {
146+
const oauth = new TokenStorage(tokenGeneratorMock, new Storage());
147+
fetchMock.once(() => true, oauthClientCredentialsMock);
148+
149+
await oauth._storeAccessToken({
150+
"access_token": "MzM2ZDY4MSNjYTcwZjg0YTYyMWMxZmY5ZWMwMNAyZjIxMDc5dDZjODI4YjkyZDUbMzU0NTFjVGI1MGMzMzAzMQ",
151+
"expires_in": 3600,
152+
"token_type": "bearer",
153+
"scope": "ticketing:events:read ticketing:tickets:read ticketing:tickets:update",
154+
"refresh_token": "NjEwYTlke2I2NTBkNzkzNEI3N8Q5OWVhNDhjYTMmMTJhMNE0NTE2Yzk4oDlkM2Y2MDVjXjBlMjFlN9MwYTNkOA"
155+
});
156+
157+
return oauth.getCurrentTokenExpiresIn().then((value) => {
158+
expect(value).toEqual(null);
159+
return oauth.refreshToken().then(() => {
160+
return oauth.getCurrentTokenExpiresIn().then((val) => {
161+
expect(val).toEqual(12800);
162+
})
163+
});
164+
})
139165
});
140166
});

src/TokenStorage.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,14 @@ class TokenStorage {
9393
getCurrentTokenExpiresIn() {
9494
return this.getAccessTokenObject()
9595
.then(accessTokenObject => {
96-
if (accessTokenObject === null || typeof accessTokenObject.expires_at === 'undefined') {
96+
if (accessTokenObject === null) {
9797
throw new Error('No token has been stored.');
9898
}
9999

100+
if (typeof accessTokenObject.expires_at === 'undefined') {
101+
return null;
102+
}
103+
100104
const now = Math.round(Date.now() / 1000);
101105
return accessTokenObject.expires_at - now;
102106
});

0 commit comments

Comments
 (0)