diff --git a/lib/access-token.js b/lib/access-token.js index f2885a62..f2aa9b3a 100644 --- a/lib/access-token.js +++ b/lib/access-token.js @@ -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); diff --git a/test/access_token.js b/test/access_token.js index 65fe5fe5..ac8e1cbb 100644 --- a/test/access_token.js +++ b/test/access_token.js @@ -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); + + 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);