Skip to content

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

Description

@nickygb

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);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions