@@ -68,6 +68,16 @@ export enum CertificateFormat {
68
68
JWK = 'JWK' ,
69
69
}
70
70
71
+ /**
72
+ * The client authentication type. Supported values are basic, post, and none.
73
+ * https://datatracker.ietf.org/doc/html/rfc7591#section-2
74
+ */
75
+ export enum ClientAuthentication {
76
+ ClientSecretPost = 'ClientSecretPost' ,
77
+ ClientSecretBasic = 'ClientSecretBasic' ,
78
+ None = 'None' ,
79
+ }
80
+
71
81
export interface GetTokenOptions {
72
82
code : string ;
73
83
codeVerifier ?: string ;
@@ -86,6 +96,19 @@ export interface GetTokenOptions {
86
96
redirect_uri ?: string ;
87
97
}
88
98
99
+ /**
100
+ * An interface for preparing {@link GetTokenOptions} as a querystring.
101
+ */
102
+ interface GetTokenQuery {
103
+ client_id ?: string ;
104
+ client_secret ?: string ;
105
+ code_verifier ?: string ;
106
+ code : string ;
107
+ grant_type : 'authorization_code' ;
108
+ redirect_uri ?: string ;
109
+ [ key : string ] : string | undefined ;
110
+ }
111
+
89
112
export interface TokenInfo {
90
113
/**
91
114
* The application that is the intended user of the access token.
@@ -475,6 +498,12 @@ export interface OAuth2ClientOptions extends AuthClientOptions {
475
498
* The allowed OAuth2 token issuers.
476
499
*/
477
500
issuers ?: string [ ] ;
501
+ /**
502
+ * The client authentication type. Supported values are basic, post, and none.
503
+ * Defaults to post if not provided.
504
+ * https://datatracker.ietf.org/doc/html/rfc7591#section-2
505
+ */
506
+ clientAuthentication ?: ClientAuthentication ;
478
507
}
479
508
480
509
// Re-exporting here for backwards compatibility
@@ -491,6 +520,7 @@ export class OAuth2Client extends AuthClient {
491
520
protected refreshTokenPromises = new Map < string , Promise < GetTokenResponse > > ( ) ;
492
521
readonly endpoints : Readonly < OAuth2ClientEndpoints > ;
493
522
readonly issuers : string [ ] ;
523
+ readonly clientAuthentication : ClientAuthentication ;
494
524
495
525
// TODO: refactor tests to make this private
496
526
_clientId ?: string ;
@@ -542,6 +572,8 @@ export class OAuth2Client extends AuthClient {
542
572
oauth2IapPublicKeyUrl : 'https://www.gstatic.com/iap/verify/public_key' ,
543
573
...opts . endpoints ,
544
574
} ;
575
+ this . clientAuthentication =
576
+ opts . clientAuthentication || ClientAuthentication . ClientSecretPost ;
545
577
546
578
this . issuers = opts . issuers || [
547
579
'accounts.google.com' ,
@@ -660,20 +692,30 @@ export class OAuth2Client extends AuthClient {
660
692
options : GetTokenOptions
661
693
) : Promise < GetTokenResponse > {
662
694
const url = this . endpoints . oauth2TokenUrl . toString ( ) ;
663
- const values = {
664
- code : options . code ,
695
+ const headers : Headers = {
696
+ 'Content-Type' : 'application/x-www-form-urlencoded' ,
697
+ } ;
698
+ const values : GetTokenQuery = {
665
699
client_id : options . client_id || this . _clientId ,
666
- client_secret : this . _clientSecret ,
667
- redirect_uri : options . redirect_uri || this . redirectUri ,
668
- grant_type : 'authorization_code' ,
669
700
code_verifier : options . codeVerifier ,
701
+ code : options . code ,
702
+ grant_type : 'authorization_code' ,
703
+ redirect_uri : options . redirect_uri || this . redirectUri ,
670
704
} ;
705
+ if ( this . clientAuthentication === ClientAuthentication . ClientSecretBasic ) {
706
+ const basic = Buffer . from ( `${ this . _clientId } :${ this . _clientSecret } ` ) ;
707
+
708
+ headers [ 'Authorization' ] = `Basic ${ basic . toString ( 'base64' ) } ` ;
709
+ }
710
+ if ( this . clientAuthentication === ClientAuthentication . ClientSecretPost ) {
711
+ values . client_secret = this . _clientSecret ;
712
+ }
671
713
const res = await this . transporter . request < CredentialRequest > ( {
672
714
...OAuth2Client . RETRY_CONFIG ,
673
715
method : 'POST' ,
674
716
url,
675
717
data : querystring . stringify ( values ) ,
676
- headers : { 'Content-Type' : 'application/x-www-form-urlencoded' } ,
718
+ headers,
677
719
} ) ;
678
720
const tokens = res . data as Credentials ;
679
721
if ( res . data && res . data . expires_in ) {
0 commit comments