Skip to content
This repository has been archived by the owner on Mar 3, 2022. It is now read-only.

Attach extra token response claims as property on user object #1346

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,8 @@ export class User {
expires_at: number;
/** The custom state transferred in the last signin */
state: any;
/** Other values sent in the token response */
otherValues: Record<string, any>;

toStorageString(): string;
static fromStorageString(storageString: string): User;
Expand Down
6 changes: 4 additions & 2 deletions src/ResponseValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,10 @@ export class ResponseValidator {

return this._tokenClient.exchangeCode(request).then(tokenResponse => {

for(var key in tokenResponse) {
response[key] = tokenResponse[key];
for (var key in tokenResponse) {
if (Object.prototype.hasOwnProperty.call(tokenResponse, key)) {
response[key] = tokenResponse[key];
}
}

if (response.id_token) {
Expand Down
18 changes: 5 additions & 13 deletions src/SigninResponse.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,12 @@ export class SigninResponse {

var values = UrlUtility.parseUrlFragment(url, delimiter);

this.error = values.error;
this.error_description = values.error_description;
this.error_uri = values.error_uri;

this.code = values.code;
this.state = values.state;
this.id_token = values.id_token;
this.session_state = values.session_state;
this.access_token = values.access_token;
this.token_type = values.token_type;
this.scope = values.scope;
for (var key in values) {
if (Object.prototype.hasOwnProperty.call(values, key)) {
this[key] = values[key];
}
}
this.profile = undefined; // will be set from ResponseValidator

this.expires_in = values.expires_in;
}

get expires_in() {
Expand Down
6 changes: 4 additions & 2 deletions src/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { Log } from './Log.js';

export class User {
constructor({id_token, session_state, access_token, refresh_token, token_type, scope, profile, expires_at, state}) {
constructor({id_token, session_state, access_token, refresh_token, token_type, scope, profile, expires_at, state, ...otherValues}) {
this.id_token = id_token;
this.session_state = session_state;
this.access_token = access_token;
Expand All @@ -14,6 +14,7 @@ export class User {
this.profile = profile;
this.expires_at = expires_at;
this.state = state;
this.otherValues = otherValues;
}

get expires_in() {
Expand Down Expand Up @@ -53,7 +54,8 @@ export class User {
token_type: this.token_type,
scope: this.scope,
profile: this.profile,
expires_at: this.expires_at
expires_at: this.expires_at,
...this.otherClaims
Copy link

@lvendrame lvendrame Jun 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be ...this.otherValues, as the constructor is storing the value in this property

});
}

Expand Down