Skip to content

Commit befac62

Browse files
Merge pull request #738 from vdveer/master
Support predefined custom parameters extraction from the TokenResponse
2 parents f9dcaf1 + edb379e commit befac62

File tree

2 files changed

+47
-8
lines changed

2 files changed

+47
-8
lines changed

projects/lib/src/auth.config.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ export class AuthConfig {
6464
*/
6565
public tokenEndpoint?: string = null;
6666

67+
/**
68+
* Names of known parameters sent out in the TokenResponse. https://tools.ietf.org/html/rfc6749#section-5.1
69+
*/
70+
public customTokenParameters?: string[] = [];
71+
6772
/**
6873
* Url of the userinfo endpoint as defined by OpenId Connect.
6974
*/

projects/lib/src/oauth-service.ts

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -735,7 +735,8 @@ export class OAuthService extends AuthConfig implements OnDestroy {
735735
tokenResponse.access_token,
736736
tokenResponse.refresh_token,
737737
tokenResponse.expires_in,
738-
tokenResponse.scope
738+
tokenResponse.scope,
739+
this.extractRecognizedCustomParameters(tokenResponse)
739740
);
740741

741742
this.eventsSubject.next(new OAuthSuccessEvent('token_received'));
@@ -812,7 +813,8 @@ export class OAuthService extends AuthConfig implements OnDestroy {
812813
tokenResponse.access_token,
813814
tokenResponse.refresh_token,
814815
tokenResponse.expires_in,
815-
tokenResponse.scope
816+
tokenResponse.scope,
817+
this.extractRecognizedCustomParameters(tokenResponse)
816818
);
817819

818820
this.eventsSubject.next(new OAuthSuccessEvent('token_received'));
@@ -1402,7 +1404,8 @@ export class OAuthService extends AuthConfig implements OnDestroy {
14021404
accessToken: string,
14031405
refreshToken: string,
14041406
expiresIn: number,
1405-
grantedScopes: String
1407+
grantedScopes: String,
1408+
customParameters?: Map<string, string>
14061409
): void {
14071410
this._storage.setItem('access_token', accessToken);
14081411
if (grantedScopes) {
@@ -1419,6 +1422,11 @@ export class OAuthService extends AuthConfig implements OnDestroy {
14191422
if (refreshToken) {
14201423
this._storage.setItem('refresh_token', refreshToken);
14211424
}
1425+
if (customParameters) {
1426+
customParameters.forEach((value : string, key: string) => {
1427+
this._storage.setItem(key, value);
1428+
});
1429+
}
14221430
}
14231431

14241432
/**
@@ -1582,7 +1590,8 @@ export class OAuthService extends AuthConfig implements OnDestroy {
15821590
tokenResponse.access_token,
15831591
tokenResponse.refresh_token,
15841592
tokenResponse.expires_in,
1585-
tokenResponse.scope);
1593+
tokenResponse.scope,
1594+
this.extractRecognizedCustomParameters(tokenResponse));
15861595

15871596
if (this.oidc && tokenResponse.id_token) {
15881597
this.processIdToken(tokenResponse.id_token, tokenResponse.access_token).
@@ -2086,6 +2095,16 @@ export class OAuthService extends AuthConfig implements OnDestroy {
20862095
return false;
20872096
}
20882097

2098+
/**
2099+
* Retrieve a saved custom property of the TokenReponse object. Only if predefined in authconfig.
2100+
*/
2101+
public getCustomTokenResponseProperty(requestedProperty: string): any {
2102+
return this._storage && this.config.customTokenParameters
2103+
&& (this.config.customTokenParameters.indexOf(requestedProperty) >= 0)
2104+
&& this._storage.getItem(requestedProperty) !== null
2105+
? JSON.parse(this._storage.getItem(requestedProperty)) : null;
2106+
}
2107+
20892108
/**
20902109
* Returns the auth-header that can be used
20912110
* to transmit the access_token to a service
@@ -2114,7 +2133,9 @@ export class OAuthService extends AuthConfig implements OnDestroy {
21142133
this._storage.removeItem('access_token_stored_at');
21152134
this._storage.removeItem('granted_scopes');
21162135
this._storage.removeItem('session_state');
2117-
2136+
if (this.config.customTokenParameters) {
2137+
this.config.customTokenParameters.forEach(customParam => this._storage.removeItem(customParam));
2138+
}
21182139
this.silentRefreshSubject = null;
21192140

21202141
this.eventsSubject.next(new OAuthInfoEvent('logout'));
@@ -2312,8 +2333,21 @@ export class OAuthService extends AuthConfig implements OnDestroy {
23122333

23132334
const verifier = await this.createNonce();
23142335
const challengeRaw = await this.crypto.calcHash(verifier, 'sha-256');
2315-
const challange = base64UrlEncode(challengeRaw);
2316-
2317-
return [challange, verifier];
2336+
const challenge = base64UrlEncode(challengeRaw);
2337+
2338+
return [challenge, verifier];
2339+
}
2340+
2341+
private extractRecognizedCustomParameters(tokenResponse: TokenResponse): Map<string, string> {
2342+
let foundParameters: Map<string, string> = new Map<string, string>();
2343+
if (!this.config.customTokenParameters) {
2344+
return foundParameters;
2345+
}
2346+
this.config.customTokenParameters.forEach((recognizedParameter: string) => {
2347+
if (tokenResponse[recognizedParameter]) {
2348+
foundParameters.set(recognizedParameter, JSON.stringify(tokenResponse[recognizedParameter]));
2349+
}
2350+
});
2351+
return foundParameters;
23182352
}
23192353
}

0 commit comments

Comments
 (0)