Skip to content

Commit df05eb0

Browse files
committed
Custom grant type added
1 parent 8d152c2 commit df05eb0

File tree

1 file changed

+57
-35
lines changed

1 file changed

+57
-35
lines changed

projects/lib/src/oauth-service.ts

Lines changed: 57 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -777,48 +777,65 @@ export class OAuthService extends AuthConfig implements OnDestroy {
777777
password: string,
778778
headers: HttpHeaders = new HttpHeaders()
779779
): Promise<TokenResponse> {
780+
const parameters = {
781+
username: userName,
782+
password: password,
783+
};
784+
return this.fetchTokenUsingGrant('password', parameters, headers);
785+
}
786+
787+
/**
788+
* Uses a custom grant type to retrieve tokens.
789+
* @param grantType Grant type.
790+
* @param parameters Parameters to pass.
791+
* @param headers Optional additional HTTP headers.
792+
*/
793+
public fetchTokenUsingGrant(grantType: string, parameters: object, headers: HttpHeaders = new HttpHeaders()): Promise<TokenResponse> {
780794
this.assertUrlNotNullAndCorrectProtocol(
781795
this.tokenEndpoint,
782796
'tokenEndpoint'
783797
);
784798

785-
return new Promise((resolve, reject) => {
786-
/**
787-
* A `HttpParameterCodec` that uses `encodeURIComponent` and `decodeURIComponent` to
788-
* serialize and parse URL parameter keys and values.
789-
*
790-
* @stable
791-
*/
792-
let params = new HttpParams({ encoder: new WebHttpUrlEncodingCodec() })
793-
.set('grant_type', 'password')
794-
.set('scope', this.scope)
795-
.set('username', userName)
796-
.set('password', password);
799+
/**
800+
* A `HttpParameterCodec` that uses `encodeURIComponent` and `decodeURIComponent` to
801+
* serialize and parse URL parameter keys and values.
802+
*
803+
* @stable
804+
*/
805+
let params = new HttpParams({ encoder: new WebHttpUrlEncodingCodec() })
806+
.set('grant_type', grantType)
807+
.set('scope', this.scope);
797808

798-
if (this.useHttpBasicAuth) {
799-
const header = btoa(`${this.clientId}:${this.dummyClientSecret}`);
800-
headers = headers.set('Authorization', 'Basic ' + header);
801-
}
809+
if (this.useHttpBasicAuth) {
810+
const header = btoa(`${this.clientId}:${this.dummyClientSecret}`);
811+
headers = headers.set('Authorization', 'Basic ' + header);
812+
}
802813

803-
if (!this.useHttpBasicAuth) {
804-
params = params.set('client_id', this.clientId);
805-
}
814+
if (!this.useHttpBasicAuth) {
815+
params = params.set('client_id', this.clientId);
816+
}
806817

807-
if (!this.useHttpBasicAuth && this.dummyClientSecret) {
808-
params = params.set('client_secret', this.dummyClientSecret);
809-
}
818+
if (!this.useHttpBasicAuth && this.dummyClientSecret) {
819+
params = params.set('client_secret', this.dummyClientSecret);
820+
}
810821

811-
if (this.customQueryParams) {
812-
for (const key of Object.getOwnPropertyNames(this.customQueryParams)) {
813-
params = params.set(key, this.customQueryParams[key]);
814-
}
822+
if (this.customQueryParams) {
823+
for (const key of Object.getOwnPropertyNames(this.customQueryParams)) {
824+
params = params.set(key, this.customQueryParams[key]);
815825
}
826+
}
816827

817-
headers = headers.set(
818-
'Content-Type',
819-
'application/x-www-form-urlencoded'
820-
);
828+
// set explicit parameters last, to allow overwriting
829+
for (const key of Object.keys(parameters)) {
830+
params = params.set(key, parameters[key]);
831+
}
821832

833+
headers = headers.set(
834+
'Content-Type',
835+
'application/x-www-form-urlencoded'
836+
);
837+
838+
return new Promise((resolve, reject) => {
822839
this.http
823840
.post<TokenResponse>(this.tokenEndpoint, params, { headers })
824841
.subscribe(
@@ -827,21 +844,26 @@ export class OAuthService extends AuthConfig implements OnDestroy {
827844
this.storeAccessTokenResponse(
828845
tokenResponse.access_token,
829846
tokenResponse.refresh_token,
830-
tokenResponse.expires_in ||
831-
this.fallbackAccessTokenExpirationTimeInSec,
847+
tokenResponse.expires_in || this.fallbackAccessTokenExpirationTimeInSec,
832848
tokenResponse.scope,
833849
this.extractRecognizedCustomParameters(tokenResponse)
834850
);
835-
851+
if (this.oidc && tokenResponse.id_token) {
852+
this.processIdToken(tokenResponse.id_token, tokenResponse.access_token)
853+
.then(result => {
854+
this.storeIdToken(result);
855+
resolve(tokenResponse);
856+
});
857+
}
836858
this.eventsSubject.next(new OAuthSuccessEvent('token_received'));
837859
resolve(tokenResponse);
838860
},
839861
err => {
840-
this.logger.error('Error performing password flow', err);
862+
this.logger.error('Error performing ${grantType} flow', err);
841863
this.eventsSubject.next(new OAuthErrorEvent('token_error', err));
842864
reject(err);
843865
}
844-
);
866+
)
845867
});
846868
}
847869

0 commit comments

Comments
 (0)