Skip to content

Commit 5ec00c2

Browse files
author
Lodewijk Wensveen
committed
Merge branch 'poke-master' into feature/upgrade-angular-8
2 parents 563a177 + 69860d0 commit 5ec00c2

File tree

2 files changed

+43
-18
lines changed

2 files changed

+43
-18
lines changed

projects/lib/src/oauth-service.ts

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -373,24 +373,36 @@ export class OAuthService extends AuthConfig {
373373
password: string,
374374
headers: HttpHeaders = new HttpHeaders()
375375
): Promise<object> {
376-
if (!this.validateUrlForHttps(this.tokenEndpoint)) {
376+
const parameters = {
377+
username: userName,
378+
password: password,
379+
};
380+
return this.fetchTokenUsingGrant('password', parameters, headers);
381+
}
382+
383+
/**
384+
* Uses a custom grant type to retrieve tokens.
385+
* @param grantType Grant type.
386+
* @param parameters Parameters to pass.
387+
* @param headers Optional additional HTTP headers.
388+
*/
389+
public fetchTokenUsingGrant(grantType: string, parameters: object, headers: HttpHeaders = new HttpHeaders()): Promise<TokenResponse> {if (!this.validateUrlForHttps(this.tokenEndpoint)) {
377390
throw new Error(
378391
'tokenEndpoint must use http, or config value for property requireHttps must allow http'
379392
);
380393
}
381394

382-
return new Promise((resolve, reject) => {
395+
383396
/**
384397
* A `HttpParameterCodec` that uses `encodeURIComponent` and `decodeURIComponent` to
385398
* serialize and parse URL parameter keys and values.
386399
*
387400
* @stable
388401
*/
389402
let params = new HttpParams({encoder: new WebHttpUrlEncodingCodec()})
390-
.set('grant_type', 'password')
403+
.set('grant_type', grantType)
391404
.set('scope', this.scope)
392-
.set('username', userName)
393-
.set('password', password);
405+
;
394406

395407
if (this.useHttpBasicAuthForPasswordFlow) {
396408
const header = btoa(`${this.clientId}:${this.dummyClientSecret}`);
@@ -413,15 +425,18 @@ export class OAuthService extends AuthConfig {
413425
}
414426
}
415427

416-
headers = headers.set(
428+
// set explicit parameters last, to allow overwriting
429+
for (const key of Object.keys(parameters)) {
430+
params = params.set(key, parameters[key]);
431+
}headers = headers.set(
417432
'Content-Type',
418433
'application/x-www-form-urlencoded'
419434
);
420435

421-
this.http
436+
returnthis.http
422437
.post<TokenResponse>(this.tokenEndpoint, params, {headers})
423-
.subscribe(
424-
tokenResponse => {
438+
.toPromise()
439+
.then(tokenResponse => {
425440
this.debug('tokenResponse', tokenResponse);
426441
this.storeAccessTokenResponse(
427442
tokenResponse.access_token,
@@ -430,16 +445,25 @@ export class OAuthService extends AuthConfig {
430445
tokenResponse.scope
431446
);
432447

433-
this.eventsSubject.next(new OAuthSuccessEvent('token_received'));
434-
resolve(tokenResponse);
435-
},
436-
err => {
437-
this.logger.error('Error performing password flow', err);
448+
if (tokenResponse.id_token) {
449+
return this.processIdToken(tokenResponse.id_token, tokenResponse.access_token)
450+
.then(idTokenResult => {
451+
this.storeIdToken(idTokenResult);
452+
return tokenResponse;
453+
});
454+
}
455+
456+
return tokenResponse;
457+
})
458+
.then(tokenResponse => {this.eventsSubject.next(new OAuthSuccessEvent('token_received'));
459+
return tokenResponse;
460+
})
461+
.catch(err => {
462+
this.logger.error(`Error performing ${grantType} flow`, err);
438463
this.eventsSubject.next(new OAuthErrorEvent('token_error', err));
439-
reject(err);
440-
}
441-
);
442-
});
464+
throw err;
465+
});
466+
443467
}
444468

445469
/**

projects/lib/src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ export interface ParsedIdToken {
110110
* http://openid.net/specs/openid-connect-core-1_0.html#TokenEndpoint
111111
*/
112112
export interface TokenResponse {
113+
id_token?: string;
113114
access_token: string;
114115
token_type: string;
115116
expires_in: number;

0 commit comments

Comments
 (0)