@@ -779,48 +779,65 @@ export class OAuthService extends AuthConfig implements OnDestroy {
779
779
password : string ,
780
780
headers : HttpHeaders = new HttpHeaders ( )
781
781
) : 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 > {
782
796
this . assertUrlNotNullAndCorrectProtocol (
783
797
this . tokenEndpoint ,
784
798
'tokenEndpoint'
785
799
) ;
786
800
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 ) ;
799
810
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
+ }
804
815
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
+ }
808
819
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
+ }
812
823
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 ] ) ;
817
827
}
828
+ }
818
829
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
+ }
823
834
835
+ headers = headers . set (
836
+ 'Content-Type' ,
837
+ 'application/x-www-form-urlencoded'
838
+ ) ;
839
+
840
+ return new Promise ( ( resolve , reject ) => {
824
841
this . http
825
842
. post < TokenResponse > ( this . tokenEndpoint , params , { headers } )
826
843
. subscribe (
@@ -829,21 +846,26 @@ export class OAuthService extends AuthConfig implements OnDestroy {
829
846
this . storeAccessTokenResponse (
830
847
tokenResponse . access_token ,
831
848
tokenResponse . refresh_token ,
832
- tokenResponse . expires_in ||
833
- this . fallbackAccessTokenExpirationTimeInSec ,
849
+ tokenResponse . expires_in || this . fallbackAccessTokenExpirationTimeInSec ,
834
850
tokenResponse . scope ,
835
851
this . extractRecognizedCustomParameters ( tokenResponse )
836
852
) ;
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
+ }
838
860
this . eventsSubject . next ( new OAuthSuccessEvent ( 'token_received' ) ) ;
839
861
resolve ( tokenResponse ) ;
840
862
} ,
841
863
err => {
842
- this . logger . error ( 'Error performing password flow' , err ) ;
864
+ this . logger . error ( 'Error performing ${grantType} flow' , err ) ;
843
865
this . eventsSubject . next ( new OAuthErrorEvent ( 'token_error' , err ) ) ;
844
866
reject ( err ) ;
845
867
}
846
- ) ;
868
+ )
847
869
} ) ;
848
870
}
849
871
0 commit comments