@@ -72,25 +72,25 @@ export interface OAuthClientProvider {
72
72
* the authorization result.
73
73
*/
74
74
codeVerifier ( ) : string | Promise < string > ;
75
-
75
+
76
76
/**
77
77
* Adds custom client authentication to OAuth token requests.
78
- *
78
+ *
79
79
* This optional method allows implementations to customize how client credentials
80
80
* are included in token exchange and refresh requests. When provided, this method
81
81
* is called instead of the default authentication logic, giving full control over
82
82
* the authentication mechanism.
83
- *
83
+ *
84
84
* Common use cases include:
85
85
* - Supporting authentication methods beyond the standard OAuth 2.0 methods
86
86
* - Adding custom headers for proprietary authentication schemes
87
87
* - Implementing client assertion-based authentication (e.g., JWT bearer tokens)
88
- *
88
+ *
89
89
* @param url - The token endpoint URL being called
90
90
* @param headers - The request headers (can be modified to add authentication)
91
91
* @param params - The request body parameters (can be modified to add credentials)
92
92
*/
93
- addClientAuthentication ?( url : URL , headers : Headers , params : URLSearchParams ) : void | Promise < void > ;
93
+ addClientAuthentication ?( headers : Headers , params : URLSearchParams , url : string | URL , metadata ?: OAuthMetadata ) : void | Promise < void > ;
94
94
95
95
/**
96
96
* If defined, overrides the selection and validation of the
@@ -112,12 +112,12 @@ export class UnauthorizedError extends Error {
112
112
113
113
/**
114
114
* Determines the best client authentication method to use based on server support and client configuration.
115
- *
115
+ *
116
116
* Priority order (highest to lowest):
117
117
* 1. client_secret_basic (if client secret is available)
118
118
* 2. client_secret_post (if client secret is available)
119
119
* 3. none (for public clients)
120
- *
120
+ *
121
121
* @param clientInformation - OAuth client information containing credentials
122
122
* @param supportedMethods - Authentication methods supported by the authorization server
123
123
* @returns The selected authentication method
@@ -127,7 +127,7 @@ function selectClientAuthMethod(
127
127
supportedMethods : string [ ]
128
128
) : string {
129
129
const hasClientSecret = ! ! clientInformation . client_secret ;
130
-
130
+
131
131
// If server doesn't specify supported methods, use RFC 6749 defaults
132
132
if ( supportedMethods . length === 0 ) {
133
133
return hasClientSecret ? "client_secret_post" : "none" ;
@@ -137,11 +137,11 @@ function selectClientAuthMethod(
137
137
if ( hasClientSecret && supportedMethods . includes ( "client_secret_basic" ) ) {
138
138
return "client_secret_basic" ;
139
139
}
140
-
140
+
141
141
if ( hasClientSecret && supportedMethods . includes ( "client_secret_post" ) ) {
142
142
return "client_secret_post" ;
143
143
}
144
-
144
+
145
145
if ( supportedMethods . includes ( "none" ) ) {
146
146
return "none" ;
147
147
}
@@ -152,12 +152,12 @@ function selectClientAuthMethod(
152
152
153
153
/**
154
154
* Applies client authentication to the request based on the specified method.
155
- *
155
+ *
156
156
* Implements OAuth 2.1 client authentication methods:
157
157
* - client_secret_basic: HTTP Basic authentication (RFC 6749 Section 2.3.1)
158
158
* - client_secret_post: Credentials in request body (RFC 6749 Section 2.3.1)
159
159
* - none: Public client authentication (RFC 6749 Section 2.1)
160
- *
160
+ *
161
161
* @param method - The authentication method to use
162
162
* @param clientInformation - OAuth client information containing credentials
163
163
* @param headers - HTTP headers object to modify
@@ -197,7 +197,7 @@ function applyBasicAuth(clientId: string, clientSecret: string | undefined, head
197
197
if ( ! clientSecret ) {
198
198
throw new Error ( "client_secret_basic authentication requires a client_secret" ) ;
199
199
}
200
-
200
+
201
201
const credentials = btoa ( `${ clientId } :${ clientSecret } ` ) ;
202
202
headers . set ( "Authorization" , `Basic ${ credentials } ` ) ;
203
203
}
@@ -593,11 +593,11 @@ export async function startAuthorization(
593
593
594
594
/**
595
595
* Exchanges an authorization code for an access token with the given server.
596
- *
596
+ *
597
597
* Supports multiple client authentication methods as specified in OAuth 2.1:
598
598
* - Automatically selects the best authentication method based on server support
599
599
* - Falls back to appropriate defaults when server metadata is unavailable
600
- *
600
+ *
601
601
* @param authorizationServerUrl - The authorization server's base URL
602
602
* @param options - Configuration object containing client info, auth code, etc.
603
603
* @returns Promise resolving to OAuth tokens
@@ -650,12 +650,12 @@ export async function exchangeAuthorization(
650
650
} ) ;
651
651
652
652
if ( addClientAuthentication ) {
653
- addClientAuthentication ( tokenUrl , headers , params ) ;
653
+ addClientAuthentication ( headers , params , authorizationServerUrl , metadata ) ;
654
654
} else {
655
655
// Determine and apply client authentication method
656
656
const supportedMethods = metadata ?. token_endpoint_auth_methods_supported ?? [ ] ;
657
657
const authMethod = selectClientAuthMethod ( clientInformation , supportedMethods ) ;
658
-
658
+
659
659
applyClientAuthentication ( authMethod , clientInformation , headers , params ) ;
660
660
}
661
661
@@ -678,11 +678,11 @@ export async function exchangeAuthorization(
678
678
679
679
/**
680
680
* Exchange a refresh token for an updated access token.
681
- *
681
+ *
682
682
* Supports multiple client authentication methods as specified in OAuth 2.1:
683
683
* - Automatically selects the best authentication method based on server support
684
684
* - Preserves the original refresh token if a new one is not returned
685
- *
685
+ *
686
686
* @param authorizationServerUrl - The authorization server's base URL
687
687
* @param options - Configuration object containing client info, refresh token, etc.
688
688
* @returns Promise resolving to OAuth tokens (preserves original refresh_token if not replaced)
@@ -732,12 +732,12 @@ export async function refreshAuthorization(
732
732
} ) ;
733
733
734
734
if ( addClientAuthentication ) {
735
- addClientAuthentication ( tokenUrl , headers , params ) ;
735
+ addClientAuthentication ( headers , params , authorizationServerUrl , metadata ) ;
736
736
} else {
737
737
// Determine and apply client authentication method
738
738
const supportedMethods = metadata ?. token_endpoint_auth_methods_supported ?? [ ] ;
739
739
const authMethod = selectClientAuthMethod ( clientInformation , supportedMethods ) ;
740
-
740
+
741
741
applyClientAuthentication ( authMethod , clientInformation , headers , params ) ;
742
742
}
743
743
0 commit comments