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

AuthorizationCode.createToken get wrong expires_at value if I have created_at and expires_in properties. #454

Open
nickygb opened this issue Oct 25, 2024 · 0 comments

Comments

@nickygb
Copy link

nickygb commented Oct 25, 2024

Context

  • node version: 18.20.4
  • module version with issue: 5.0.0
  • last module version without issue:
  • environment (e.g. node, browser, native): node
  • any other relevant information:

What are you trying to achieve or the steps to reproduce?

I have authorization_code grant type and trying to create an AccessToken instance from what I have stored in database. The access token stored in the database has the following properties:

const tokenFromDB = {
  access_token: 'MY ACCES TOKEN',
  refresh_token: 'MY REFRESH TOKEN',
  expires_in: 43200,
  scope: 'offline_access eq1',
  token_type: 'Bearer',
  created_at: 1729699637, // October 23, 2024 4:07:17 PM
};

When I execute the createToken method I got always a token that said is not expired, despite it is.

What was the result you got?

The result I got is the following:

const accessToken = client.createToken(tokenFromDB);
console.log(accessToken);
// AccessToken {
//   token: {
//     access_token: 'MY ACCESS TOKEN',
//     refresh_token: 'MY REFRESH TOKEN',
//     expires_in: 43200,
//     scope: 'offline_access eq1',
//     token_type: 'Bearer',
//     created_at: 1729699637,
//     expires_at: 2024-10-26T10:48:59.998Z // <=== THIS SHOULD BE   2024-10-24T04:07:17.000Z
//   }
// }

What result did you expect?

const accessToken = client.createToken(tokenFromDB);
console.log(accessToken);
// AccessToken {
//   token: {
//     access_token: 'MY ACCESS TOKEN',
//     refresh_token: 'MY REFRESH TOKEN',
//     expires_in: 43200,
//     scope: 'offline_access eq1',
//     token_type: 'Bearer',
//     created_at: 1729699637,
//     expires_at:  2024-10-24T04:07:17.000Z
//   }
// }

I think the problem is here:

function getExpirationDate(expiresIn) {
return new Date(Date.now() + Number.parseInt(expiresIn, 10) * 1000);
}

This function should accept also the parameter created_at. It always get the expiration date relative to now. I think the function should be something like that:

function getExpirationDate(expiresIn, createdAt) {
  return new Date( (createdAt || Date.now()) + Number.parseInt(expiresIn, 10) * 1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant