Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow expires_at to be a UNIX timestamp #277

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/access-token.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ function parseToken(token) {

if ('expires_at' in token) {
if (!isDate(token.expires_at)) {
tokenProperties.expires_at = parseISO(token.expires_at);
if (typeof token.expires_at === 'number') {
tokenProperties.expires_at = new Date(token.expires_at * 1000);
} else {
tokenProperties.expires_at = parseISO(token.expires_at);
}
}
} else if ('expires_in' in token) {
tokenProperties.expires_at = getExpirationDate(token.expires_in);
Expand Down
20 changes: 19 additions & 1 deletion test/access_token.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,25 @@ test('@create => do not reassigns the expires at property when is already a date
t.true(isValid(accessToken.token.expires_at));
});

test('@create => parses the expires at property when is not a date', (t) => {
test('@create => parses the expires at property when is UNIX timestamp in seconds', (t) => {
const config = createModuleConfig();
const oauth2 = oauth2Module.create(config);

const accessTokenResponse = chance.accessToken({
expired: true,
parseDate: false,
expireMode: 'expires_at',
});
// Sometimes expires_at is also given as a UNIX timestamp in seconds:
accessTokenResponse.expires_at = Math.round(new Date(accessTokenResponse.expires_at).getTime() / 1000);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be improved if jonathansamines/chance-access-token#2 lands

Copy link
Collaborator

@jonathansamines jonathansamines Nov 21, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the mentioned change would require breaking the existing public API, will probably take us more time to land. This change looks good as is, will merge it and release a new version and we could update the tests later when the change lands.


const accessToken = oauth2.accessToken.create(accessTokenResponse);

t.true(isDate(accessToken.token.expires_at));
t.true(isValid(accessToken.token.expires_at));
});

test('@create => parses the expires at property when is ISO time', (t) => {
const config = createModuleConfig();
const oauth2 = oauth2Module.create(config);

Expand Down