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