Skip to content

Commit 4156226

Browse files
Merge pull request #919 from alexandis/CustomGrantType
Custom grant type added
2 parents c96a94c + df05eb0 commit 4156226

File tree

1 file changed

+57
-35
lines changed

1 file changed

+57
-35
lines changed

projects/lib/src/oauth-service.ts

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

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

800-
if (this.useHttpBasicAuth) {
801-
const header = btoa(`${this.clientId}:${this.dummyClientSecret}`);
802-
headers = headers.set('Authorization', 'Basic ' + header);
803-
}
811+
if (this.useHttpBasicAuth) {
812+
const header = btoa(`${this.clientId}:${this.dummyClientSecret}`);
813+
headers = headers.set('Authorization', 'Basic ' + header);
814+
}
804815

805-
if (!this.useHttpBasicAuth) {
806-
params = params.set('client_id', this.clientId);
807-
}
816+
if (!this.useHttpBasicAuth) {
817+
params = params.set('client_id', this.clientId);
818+
}
808819

809-
if (!this.useHttpBasicAuth && this.dummyClientSecret) {
810-
params = params.set('client_secret', this.dummyClientSecret);
811-
}
820+
if (!this.useHttpBasicAuth && this.dummyClientSecret) {
821+
params = params.set('client_secret', this.dummyClientSecret);
822+
}
812823

813-
if (this.customQueryParams) {
814-
for (const key of Object.getOwnPropertyNames(this.customQueryParams)) {
815-
params = params.set(key, this.customQueryParams[key]);
816-
}
824+
if (this.customQueryParams) {
825+
for (const key of Object.getOwnPropertyNames(this.customQueryParams)) {
826+
params = params.set(key, this.customQueryParams[key]);
817827
}
828+
}
818829

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

835+
headers = headers.set(
836+
'Content-Type',
837+
'application/x-www-form-urlencoded'
838+
);
839+
840+
return new Promise((resolve, reject) => {
824841
this.http
825842
.post<TokenResponse>(this.tokenEndpoint, params, { headers })
826843
.subscribe(
@@ -829,21 +846,26 @@ export class OAuthService extends AuthConfig implements OnDestroy {
829846
this.storeAccessTokenResponse(
830847
tokenResponse.access_token,
831848
tokenResponse.refresh_token,
832-
tokenResponse.expires_in ||
833-
this.fallbackAccessTokenExpirationTimeInSec,
849+
tokenResponse.expires_in || this.fallbackAccessTokenExpirationTimeInSec,
834850
tokenResponse.scope,
835851
this.extractRecognizedCustomParameters(tokenResponse)
836852
);
837-
853+
if (this.oidc && tokenResponse.id_token) {
854+
this.processIdToken(tokenResponse.id_token, tokenResponse.access_token)
855+
.then(result => {
856+
this.storeIdToken(result);
857+
resolve(tokenResponse);
858+
});
859+
}
838860
this.eventsSubject.next(new OAuthSuccessEvent('token_received'));
839861
resolve(tokenResponse);
840862
},
841863
err => {
842-
this.logger.error('Error performing password flow', err);
864+
this.logger.error('Error performing ${grantType} flow', err);
843865
this.eventsSubject.next(new OAuthErrorEvent('token_error', err));
844866
reject(err);
845867
}
846-
);
868+
)
847869
});
848870
}
849871

0 commit comments

Comments
 (0)