Skip to content

Get granted scopes #274

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

Merged
merged 2 commits into from
May 9, 2018
Merged
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
22 changes: 18 additions & 4 deletions angular-oauth2-oidc/src/oauth-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export class OAuthService
this.configure(config);
}


try {
if (storage) {
this.setStorage(storage);
Expand Down Expand Up @@ -571,7 +572,7 @@ export class OAuthService
this.http.post<TokenResponse>(this.tokenEndpoint, params, { headers }).subscribe(
(tokenResponse) => {
this.debug('tokenResponse', tokenResponse);
this.storeAccessTokenResponse(tokenResponse.access_token, tokenResponse.refresh_token, tokenResponse.expires_in);
this.storeAccessTokenResponse(tokenResponse.access_token, tokenResponse.refresh_token, tokenResponse.expires_in, tokenResponse.scope);

this.eventsSubject.next(new OAuthSuccessEvent('token_received'));
resolve(tokenResponse);
Expand Down Expand Up @@ -622,7 +623,7 @@ export class OAuthService
this.http.post<TokenResponse>(this.tokenEndpoint, params, { headers }).subscribe(
(tokenResponse) => {
this.debug('refresh tokenResponse', tokenResponse);
this.storeAccessTokenResponse(tokenResponse.access_token, tokenResponse.refresh_token, tokenResponse.expires_in);
this.storeAccessTokenResponse(tokenResponse.access_token, tokenResponse.refresh_token, tokenResponse.expires_in, tokenResponse.scope);

this.eventsSubject.next(new OAuthSuccessEvent('token_received'));
this.eventsSubject.next(new OAuthSuccessEvent('token_refreshed'));
Expand Down Expand Up @@ -726,6 +727,7 @@ export class OAuthService
document.body.appendChild(iframe);
});


let errors = this.events.pipe(filter(e => e instanceof OAuthErrorEvent), first());
let success = this.events.pipe(filter(e => e.type === 'silently_refreshed'), first());
let timeout = of(new OAuthErrorEvent('silent_refresh_timeout', null))
Expand Down Expand Up @@ -1055,8 +1057,9 @@ export class OAuthService
}
}

private storeAccessTokenResponse(accessToken: string, refreshToken: string, expiresIn: number): void {
private storeAccessTokenResponse(accessToken: string, refreshToken: string, expiresIn: number, grantedScopes: String): void {
this._storage.setItem('access_token', accessToken);
this._storage.setItem('granted_scopes', JSON.stringify(grantedScopes.split('+')));
this._storage.setItem('access_token_stored_at', '' + Date.now());
if (expiresIn) {
let expiresInMilliSeconds = expiresIn * 1000;
Expand Down Expand Up @@ -1105,6 +1108,7 @@ export class OAuthService
let idToken = parts['id_token'];
let state = decodeURIComponent(parts['state']);
let sessionState = parts['session_state'];
let grantedScopes = parts['scope'];

if (!this.requestAccessToken && !this.oidc) {
return Promise.reject('Either requestAccessToken or oidc or both must be true.');
Expand Down Expand Up @@ -1146,7 +1150,7 @@ export class OAuthService
}

if (this.requestAccessToken) {
this.storeAccessTokenResponse(accessToken, null, parts['expires_in']);
this.storeAccessTokenResponse(accessToken, null, parts['expires_in'], grantedScopes);
}

if (!this.oidc) {
Expand Down Expand Up @@ -1292,6 +1296,7 @@ export class OAuthService
return Promise.reject(err);
}


if (!this.disableAtHashCheck && this.requestAccessToken && !claims['at_hash']) {
let err = 'An at_hash is needed!';
console.warn(err);
Expand Down Expand Up @@ -1352,6 +1357,15 @@ export class OAuthService
return JSON.parse(claims);
}

/**
* Returns the granted scopes from the server.
*/
public getGrantedScopes(): object {
let scopes = this._storage.getItem('granted_scopes');
if (!scopes) return null;
return JSON.parse(scopes);
}

/**
* Returns the current id_token.
*/
Expand Down