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

How to add custom fields in a Token response #67

Open
nanachimi opened this issue Apr 3, 2018 · 1 comment
Open

How to add custom fields in a Token response #67

nanachimi opened this issue Apr 3, 2018 · 1 comment

Comments

@nanachimi
Copy link

nanachimi commented Apr 3, 2018

After a long debugging session I found following configuration options:

oauth = new OAuthServer({ 
        model: dataModel, 
        debug: true , 
        allowExtendedTokenAttributes: true, 
        alwaysIssueNewRefreshToken: false, 
        requireClientAuthentication: {}, 
        accessTokenLifetime:1800,
        refreshTokenLifetime: 3600});

For my issue allowExtendedTokenAttributes seems to be the correct option. But a remaining question is how to specify the custom fields when the allowExtendedTokenAttributes option is set to true?

I found that in lib\models\token-model.js of the underline library oauth2-server some fields are filtered out and cannot be consider as custom fields.

var modelAttributes = ['accessToken', 'accessTokenExpiresAt', 'refreshToken', 'refreshTokenExpiresAt', 'scope', 'client', 'user'];

  if (options && options.allowExtendedTokenAttributes) {
    this.customAttributes = {};

    console.log("data: ", data)
    for (var key in data) {
      if (data.hasOwnProperty(key) && (modelAttributes.indexOf(key) < 0)) {
        this.customAttributes[key] = data[key];
      }
    }
  }

Now my responses look like this when I hit POST /oauth/token:

{
"access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZGVudGl0eSI6IjAwMjM3...",
"token_type": "Bearer",
"expires_in": 1799,
"refresh_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZGVudGl0eSI6IjA..."
}

How should I proceed if I want to add a new field user_id in the above response?

Here my source code: https://github.com/nanachimi/oauthserverjs

Thanks

@koocbor
Copy link

koocbor commented Oct 17, 2018

You have to add the additional properties to the object returned from the saveToken method. This is mentioned pretty far down this page: https://oauth2-server.readthedocs.io/en/latest/api/oauth2-server.html

If options.allowExtendedTokenAttributes is true any additional properties set on the object returned from Model#saveToken() are copied to the token response sent to the client.

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

2 participants