@@ -72,25 +72,25 @@ export interface OAuthClientProvider {
7272 * the authorization result.
7373 */
7474 codeVerifier ( ) : string | Promise < string > ;
75-
75+
7676 /**
7777 * Adds custom client authentication to OAuth token requests.
78- *
78+ *
7979 * This optional method allows implementations to customize how client credentials
8080 * are included in token exchange and refresh requests. When provided, this method
8181 * is called instead of the default authentication logic, giving full control over
8282 * the authentication mechanism.
83- *
83+ *
8484 * Common use cases include:
8585 * - Supporting authentication methods beyond the standard OAuth 2.0 methods
8686 * - Adding custom headers for proprietary authentication schemes
8787 * - Implementing client assertion-based authentication (e.g., JWT bearer tokens)
88- *
88+ *
8989 * @param url - The token endpoint URL being called
9090 * @param headers - The request headers (can be modified to add authentication)
9191 * @param params - The request body parameters (can be modified to add credentials)
9292 */
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 > ;
9494
9595 /**
9696 * If defined, overrides the selection and validation of the
@@ -112,12 +112,12 @@ export class UnauthorizedError extends Error {
112112
113113/**
114114 * Determines the best client authentication method to use based on server support and client configuration.
115- *
115+ *
116116 * Priority order (highest to lowest):
117117 * 1. client_secret_basic (if client secret is available)
118118 * 2. client_secret_post (if client secret is available)
119119 * 3. none (for public clients)
120- *
120+ *
121121 * @param clientInformation - OAuth client information containing credentials
122122 * @param supportedMethods - Authentication methods supported by the authorization server
123123 * @returns The selected authentication method
@@ -127,7 +127,7 @@ function selectClientAuthMethod(
127127 supportedMethods : string [ ]
128128) : string {
129129 const hasClientSecret = ! ! clientInformation . client_secret ;
130-
130+
131131 // If server doesn't specify supported methods, use RFC 6749 defaults
132132 if ( supportedMethods . length === 0 ) {
133133 return hasClientSecret ? "client_secret_post" : "none" ;
@@ -137,11 +137,11 @@ function selectClientAuthMethod(
137137 if ( hasClientSecret && supportedMethods . includes ( "client_secret_basic" ) ) {
138138 return "client_secret_basic" ;
139139 }
140-
140+
141141 if ( hasClientSecret && supportedMethods . includes ( "client_secret_post" ) ) {
142142 return "client_secret_post" ;
143143 }
144-
144+
145145 if ( supportedMethods . includes ( "none" ) ) {
146146 return "none" ;
147147 }
@@ -152,12 +152,12 @@ function selectClientAuthMethod(
152152
153153/**
154154 * Applies client authentication to the request based on the specified method.
155- *
155+ *
156156 * Implements OAuth 2.1 client authentication methods:
157157 * - client_secret_basic: HTTP Basic authentication (RFC 6749 Section 2.3.1)
158158 * - client_secret_post: Credentials in request body (RFC 6749 Section 2.3.1)
159159 * - none: Public client authentication (RFC 6749 Section 2.1)
160- *
160+ *
161161 * @param method - The authentication method to use
162162 * @param clientInformation - OAuth client information containing credentials
163163 * @param headers - HTTP headers object to modify
@@ -197,7 +197,7 @@ function applyBasicAuth(clientId: string, clientSecret: string | undefined, head
197197 if ( ! clientSecret ) {
198198 throw new Error ( "client_secret_basic authentication requires a client_secret" ) ;
199199 }
200-
200+
201201 const credentials = btoa ( `${ clientId } :${ clientSecret } ` ) ;
202202 headers . set ( "Authorization" , `Basic ${ credentials } ` ) ;
203203}
@@ -593,11 +593,11 @@ export async function startAuthorization(
593593
594594/**
595595 * Exchanges an authorization code for an access token with the given server.
596- *
596+ *
597597 * Supports multiple client authentication methods as specified in OAuth 2.1:
598598 * - Automatically selects the best authentication method based on server support
599599 * - Falls back to appropriate defaults when server metadata is unavailable
600- *
600+ *
601601 * @param authorizationServerUrl - The authorization server's base URL
602602 * @param options - Configuration object containing client info, auth code, etc.
603603 * @returns Promise resolving to OAuth tokens
@@ -650,12 +650,12 @@ export async function exchangeAuthorization(
650650 } ) ;
651651
652652 if ( addClientAuthentication ) {
653- addClientAuthentication ( tokenUrl , headers , params ) ;
653+ addClientAuthentication ( headers , params , authorizationServerUrl , metadata ) ;
654654 } else {
655655 // Determine and apply client authentication method
656656 const supportedMethods = metadata ?. token_endpoint_auth_methods_supported ?? [ ] ;
657657 const authMethod = selectClientAuthMethod ( clientInformation , supportedMethods ) ;
658-
658+
659659 applyClientAuthentication ( authMethod , clientInformation , headers , params ) ;
660660 }
661661
@@ -678,11 +678,11 @@ export async function exchangeAuthorization(
678678
679679/**
680680 * Exchange a refresh token for an updated access token.
681- *
681+ *
682682 * Supports multiple client authentication methods as specified in OAuth 2.1:
683683 * - Automatically selects the best authentication method based on server support
684684 * - Preserves the original refresh token if a new one is not returned
685- *
685+ *
686686 * @param authorizationServerUrl - The authorization server's base URL
687687 * @param options - Configuration object containing client info, refresh token, etc.
688688 * @returns Promise resolving to OAuth tokens (preserves original refresh_token if not replaced)
@@ -732,12 +732,12 @@ export async function refreshAuthorization(
732732 } ) ;
733733
734734 if ( addClientAuthentication ) {
735- addClientAuthentication ( tokenUrl , headers , params ) ;
735+ addClientAuthentication ( headers , params , authorizationServerUrl , metadata ) ;
736736 } else {
737737 // Determine and apply client authentication method
738738 const supportedMethods = metadata ?. token_endpoint_auth_methods_supported ?? [ ] ;
739739 const authMethod = selectClientAuthMethod ( clientInformation , supportedMethods ) ;
740-
740+
741741 applyClientAuthentication ( authMethod , clientInformation , headers , params ) ;
742742 }
743743
0 commit comments