Skip to content

Support custom grant types #344

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

Closed
wants to merge 1 commit into from
Closed
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
131 changes: 78 additions & 53 deletions projects/lib/src/oauth-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -632,73 +632,98 @@ export class OAuthService extends AuthConfig {
password: string,
headers: HttpHeaders = new HttpHeaders()
): Promise<object> {
const parameters = {
username: userName,
password: password,
};
return this.fetchTokenUsingGrant('password', parameters, headers);
}

/**
* Uses a custom grant type to retrieve tokens.
* @param grantType Grant type.
* @param parameters Parameters to pass.
* @param headers Optional additional HTTP headers.
*/
public fetchTokenUsingGrant(grantType: string, parameters: object, headers: HttpHeaders = new HttpHeaders()): Promise<TokenResponse> {
if (!this.validateUrlForHttps(this.tokenEndpoint)) {
throw new Error(
'tokenEndpoint must use http, or config value for property requireHttps must allow http'
);
}

return new Promise((resolve, reject) => {
/**
* A `HttpParameterCodec` that uses `encodeURIComponent` and `decodeURIComponent` to
* serialize and parse URL parameter keys and values.
*
* @stable
*/
let params = new HttpParams({ encoder: new WebHttpUrlEncodingCodec() })
.set('grant_type', 'password')
.set('scope', this.scope)
.set('username', userName)
.set('password', password);

if (this.useHttpBasicAuthForPasswordFlow) {
const header = btoa(`${this.clientId}:${this.dummyClientSecret}`);
headers = headers.set(
'Authorization',
'Basic ' + header);
}
/**
* A `HttpParameterCodec` that uses `encodeURIComponent` and `decodeURIComponent` to
* serialize and parse URL parameter keys and values.
*
* @stable
*/
let params = new HttpParams({ encoder: new WebHttpUrlEncodingCodec() })
.set('grant_type', grantType)
.set('scope', this.scope);

if (!this.useHttpBasicAuthForPasswordFlow) {
params = params.set('client_id', this.clientId);
}
if (this.useHttpBasicAuthForPasswordFlow) {
const header = btoa(`${this.clientId}:${this.dummyClientSecret}`);
headers = headers.set(
'Authorization',
'Basic ' + header);
}

if (!this.useHttpBasicAuthForPasswordFlow && this.dummyClientSecret) {
params = params.set('client_secret', this.dummyClientSecret);
}
if (!this.useHttpBasicAuthForPasswordFlow) {
params = params.set('client_id', this.clientId);
}

if (this.customQueryParams) {
for (const key of Object.getOwnPropertyNames(this.customQueryParams)) {
params = params.set(key, this.customQueryParams[key]);
}
if (!this.useHttpBasicAuthForPasswordFlow && this.dummyClientSecret) {
params = params.set('client_secret', this.dummyClientSecret);
}

if (this.customQueryParams) {
for (const key of Object.getOwnPropertyNames(this.customQueryParams)) {
params = params.set(key, this.customQueryParams[key]);
}
}

headers = headers.set(
'Content-Type',
'application/x-www-form-urlencoded'
);
// set explicit parameters last, to allow overwriting
for (const key of Object.keys(parameters)) {
params = params.set(key, parameters[key]);
}

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,
tokenResponse.scope
);
headers = headers.set(
'Content-Type',
'application/x-www-form-urlencoded'
);

this.eventsSubject.next(new OAuthSuccessEvent('token_received'));
resolve(tokenResponse);
},
err => {
this.logger.error('Error performing password flow', err);
this.eventsSubject.next(new OAuthErrorEvent('token_error', err));
reject(err);
}
return this.http
.post<TokenResponse>(this.tokenEndpoint, params, { headers })
.toPromise()
.then(tokenResponse => {
this.debug('tokenResponse', tokenResponse);
this.storeAccessTokenResponse(
tokenResponse.access_token,
tokenResponse.refresh_token,
tokenResponse.expires_in,
tokenResponse.scope
);
});

if (tokenResponse.id_token) {
return this.processIdToken(tokenResponse.id_token, tokenResponse.access_token)
.then(idTokenResult => {
this.storeIdToken(idTokenResult);
return tokenResponse;
});
}

return tokenResponse;
})
.then(tokenResponse => {
this.eventsSubject.next(new OAuthSuccessEvent('token_received'));
return tokenResponse;
})
.catch(err => {
this.logger.error(`Error performing ${grantType} flow`, err);
this.eventsSubject.next(new OAuthErrorEvent('token_error', err));
throw err;
});
}

/**
Expand Down
1 change: 1 addition & 0 deletions projects/lib/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ export interface ParsedIdToken {
* http://openid.net/specs/openid-connect-core-1_0.html#TokenEndpoint
*/
export interface TokenResponse {
id_token?: string;
access_token: string;
token_type: string;
expires_in: number;
Expand Down