Skip to content

add method to store and retrieve additional parameters from token htt… #1

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

Open
wants to merge 2 commits into
base: master
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 README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Archiving this repository: Original angular-oauth2-oidc now does codeflow. Will request to add contextual parameters.

# angular-oauth2-oidc-codeflow
[![Build Status](https://travis-ci.org/bechhansen/angular-oauth2-oidc.svg?branch=master)](https://travis-ci.org/bechhansen/angular-oauth2-oidc)

Expand Down
32 changes: 32 additions & 0 deletions projects/lib/src/oauth-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,8 @@ export class OAuthService extends AuthConfig {
tokenResponse.scope
);

this.storeAdditionalParameters(tokenResponse);

this.eventsSubject.next(new OAuthSuccessEvent('token_received'));
resolve(tokenResponse);
},
Expand Down Expand Up @@ -737,6 +739,8 @@ export class OAuthService extends AuthConfig {
this.debug('refresh tokenResponse', tokenResponse);
this.storeAccessTokenResponse(tokenResponse.access_token, tokenResponse.refresh_token, tokenResponse.expires_in, tokenResponse.scope);

this.storeAdditionalParameters(tokenResponse);

if (this.oidc && tokenResponse.id_token) {
this.processIdToken(tokenResponse.id_token, tokenResponse.access_token).
then(result => {
Expand Down Expand Up @@ -1302,6 +1306,23 @@ export class OAuthService extends AuthConfig {
}
}

/**
* Store additional parameters received in the tokenResponse
* @param tokenResponse the parameters from the token post request
*/
private storeAdditionalParameters(tokenResponse) {
const additionalParams = JSON.parse(this._storage.getItem('additional_params')) || {};

const tokenKeys = ['access_token', 'refresh_token', 'expires_in', 'scope'];
Object.keys(tokenResponse).forEach(key => {
if (!tokenKeys.includes(key)) { // don't store parameters related to token, stored elsewhere
additionalParams[key] = tokenResponse[key];
}
});

this._storage.setItem('additional_params', JSON.stringify(additionalParams));
}

/**
* Checks whether there are tokens in the hash fragment
* as a result of the implicit flow. These tokens are
Expand Down Expand Up @@ -1737,6 +1758,17 @@ export class OAuthService extends AuthConfig {
return parseInt(this._storage.getItem('id_token_expires_at'), 10);
}

/**
* Get additional parameters that have been saved after a log in
* @returns {object} key:value pairs of additional parameters from oAuth login
*/
public getAdditionalParameters(): object {
if (!this._storage.getItem('additional_params')) {
return null;
}
return JSON.parse(this._storage.getItem('additional_params'));
}

/**
* Checkes, whether there is a valid access_token.
*/
Expand Down