Skip to content

Commit 3da1a94

Browse files
Merge pull request #274 from MrJustreborn/scope
Get granted scopes
2 parents d4555f0 + 3d73e52 commit 3da1a94

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

angular-oauth2-oidc/src/oauth-service.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ export class OAuthService
8585
this.configure(config);
8686
}
8787

88+
8889
try {
8990
if (storage) {
9091
this.setStorage(storage);
@@ -571,7 +572,7 @@ export class OAuthService
571572
this.http.post<TokenResponse>(this.tokenEndpoint, params, { headers }).subscribe(
572573
(tokenResponse) => {
573574
this.debug('tokenResponse', tokenResponse);
574-
this.storeAccessTokenResponse(tokenResponse.access_token, tokenResponse.refresh_token, tokenResponse.expires_in);
575+
this.storeAccessTokenResponse(tokenResponse.access_token, tokenResponse.refresh_token, tokenResponse.expires_in, tokenResponse.scope);
575576

576577
this.eventsSubject.next(new OAuthSuccessEvent('token_received'));
577578
resolve(tokenResponse);
@@ -622,7 +623,7 @@ export class OAuthService
622623
this.http.post<TokenResponse>(this.tokenEndpoint, params, { headers }).subscribe(
623624
(tokenResponse) => {
624625
this.debug('refresh tokenResponse', tokenResponse);
625-
this.storeAccessTokenResponse(tokenResponse.access_token, tokenResponse.refresh_token, tokenResponse.expires_in);
626+
this.storeAccessTokenResponse(tokenResponse.access_token, tokenResponse.refresh_token, tokenResponse.expires_in, tokenResponse.scope);
626627

627628
this.eventsSubject.next(new OAuthSuccessEvent('token_received'));
628629
this.eventsSubject.next(new OAuthSuccessEvent('token_refreshed'));
@@ -726,6 +727,7 @@ export class OAuthService
726727
document.body.appendChild(iframe);
727728
});
728729

730+
729731
let errors = this.events.pipe(filter(e => e instanceof OAuthErrorEvent), first());
730732
let success = this.events.pipe(filter(e => e.type === 'silently_refreshed'), first());
731733
let timeout = of(new OAuthErrorEvent('silent_refresh_timeout', null))
@@ -1055,8 +1057,9 @@ export class OAuthService
10551057
}
10561058
}
10571059

1058-
private storeAccessTokenResponse(accessToken: string, refreshToken: string, expiresIn: number): void {
1060+
private storeAccessTokenResponse(accessToken: string, refreshToken: string, expiresIn: number, grantedScopes: String): void {
10591061
this._storage.setItem('access_token', accessToken);
1062+
this._storage.setItem('granted_scopes', JSON.stringify(grantedScopes.split('+')));
10601063
this._storage.setItem('access_token_stored_at', '' + Date.now());
10611064
if (expiresIn) {
10621065
let expiresInMilliSeconds = expiresIn * 1000;
@@ -1105,6 +1108,7 @@ export class OAuthService
11051108
let idToken = parts['id_token'];
11061109
let state = decodeURIComponent(parts['state']);
11071110
let sessionState = parts['session_state'];
1111+
let grantedScopes = parts['scope'];
11081112

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

11481152
if (this.requestAccessToken) {
1149-
this.storeAccessTokenResponse(accessToken, null, parts['expires_in']);
1153+
this.storeAccessTokenResponse(accessToken, null, parts['expires_in'], grantedScopes);
11501154
}
11511155

11521156
if (!this.oidc) {
@@ -1292,6 +1296,7 @@ export class OAuthService
12921296
return Promise.reject(err);
12931297
}
12941298

1299+
12951300
if (!this.disableAtHashCheck && this.requestAccessToken && !claims['at_hash']) {
12961301
let err = 'An at_hash is needed!';
12971302
console.warn(err);
@@ -1352,6 +1357,15 @@ export class OAuthService
13521357
return JSON.parse(claims);
13531358
}
13541359

1360+
/**
1361+
* Returns the granted scopes from the server.
1362+
*/
1363+
public getGrantedScopes(): object {
1364+
let scopes = this._storage.getItem('granted_scopes');
1365+
if (!scopes) return null;
1366+
return JSON.parse(scopes);
1367+
}
1368+
13551369
/**
13561370
* Returns the current id_token.
13571371
*/

0 commit comments

Comments
 (0)