Skip to content

SaveToken doesn't propagate client, user, scope to generateAcessToken if asyc is used #333

Open
@alfonsodev

Description

@alfonsodev

I'm using latest version from master
I found that when using my custom model generateAccessToken doesn't receive the arguments (client, user, scope) when using async generateAcessToken.

I found a solution by binding this context to each function when declaring the array fns.
In lib/grant-type/grant-type-password.js

PasswordGrantType.prototype.saveToken = function(user, client, scope) {
  var fns = [
    this.validateScope(user, client, scope),
    this.generateAccessToken(client, user, scope).bind(this), // <- adding bind here
    this.generateRefreshToken(client, user, scope),
    this.getAccessTokenExpiresAt(),
    this.getRefreshTokenExpiresAt()
  ];

  return Promise.all(fns)
    .bind(this)
    .spread(function(scope, accessToken, refreshToken, accessTokenExpiresAt, refreshTokenExpiresAt) {
      var token = {
        accessToken: accessToken,
        accessTokenExpiresAt: accessTokenExpiresAt,
        refreshToken: refreshToken,
        refreshTokenExpiresAt: refreshTokenExpiresAt,
        scope: scope
      };

      return promisify(this.model.saveToken, 3)(token, client, user);
    });
};

If this solution is acceptable I can prepare a pull request.
I guess the same problem will apply to validateScope and generateRefreshToken,
and other grant-types
Thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions