@@ -3,6 +3,7 @@ import { LATEST_PROTOCOL_VERSION } from '../types.js';
33import {
44 OAuthClientMetadata ,
55 OAuthClientInformation ,
6+ OAuthClientInformationMixed ,
67 OAuthTokens ,
78 OAuthMetadata ,
89 OAuthClientInformationFull ,
@@ -58,7 +59,7 @@ export interface OAuthClientProvider {
5859 * server, or returns `undefined` if the client is not registered with the
5960 * server.
6061 */
61- clientInformation ( ) : OAuthClientInformation | undefined | Promise < OAuthClientInformation | undefined > ;
62+ clientInformation ( ) : OAuthClientInformationMixed | undefined | Promise < OAuthClientInformationMixed | undefined > ;
6263
6364 /**
6465 * If implemented, this permits the OAuth client to dynamically register with
@@ -68,7 +69,7 @@ export interface OAuthClientProvider {
6869 * This method is not required to be implemented if client information is
6970 * statically known (e.g., pre-registered).
7071 */
71- saveClientInformation ?( clientInformation : OAuthClientInformationFull ) : void | Promise < void > ;
72+ saveClientInformation ?( clientInformation : OAuthClientInformationMixed ) : void | Promise < void > ;
7273
7374 /**
7475 * Loads any existing OAuth tokens for the current session, or returns
@@ -151,6 +152,10 @@ export class UnauthorizedError extends Error {
151152
152153type ClientAuthMethod = 'client_secret_basic' | 'client_secret_post' | 'none' ;
153154
155+ function isClientAuthMethod ( method : string ) : method is ClientAuthMethod {
156+ return [ 'client_secret_basic' , 'client_secret_post' , 'none' ] . includes ( method ) ;
157+ }
158+
154159const AUTHORIZATION_CODE_RESPONSE_TYPE = 'code' ;
155160const AUTHORIZATION_CODE_CHALLENGE_METHOD = 'S256' ;
156161
@@ -166,14 +171,24 @@ const AUTHORIZATION_CODE_CHALLENGE_METHOD = 'S256';
166171 * @param supportedMethods - Authentication methods supported by the authorization server
167172 * @returns The selected authentication method
168173 */
169- function selectClientAuthMethod ( clientInformation : OAuthClientInformation , supportedMethods : string [ ] ) : ClientAuthMethod {
174+ export function selectClientAuthMethod ( clientInformation : OAuthClientInformationMixed , supportedMethods : string [ ] ) : ClientAuthMethod {
170175 const hasClientSecret = clientInformation . client_secret !== undefined ;
171176
172177 // If server doesn't specify supported methods, use RFC 6749 defaults
173178 if ( supportedMethods . length === 0 ) {
174179 return hasClientSecret ? 'client_secret_post' : 'none' ;
175180 }
176181
182+ // Prefer the method returned by the server during client registration if valid and supported
183+ if (
184+ 'token_endpoint_auth_method' in clientInformation &&
185+ clientInformation . token_endpoint_auth_method &&
186+ isClientAuthMethod ( clientInformation . token_endpoint_auth_method ) &&
187+ supportedMethods . includes ( clientInformation . token_endpoint_auth_method )
188+ ) {
189+ return clientInformation . token_endpoint_auth_method ;
190+ }
191+
177192 // Try methods in priority order (most secure first)
178193 if ( hasClientSecret && supportedMethods . includes ( 'client_secret_basic' ) ) {
179194 return 'client_secret_basic' ;
@@ -796,7 +811,7 @@ export async function startAuthorization(
796811 resource
797812 } : {
798813 metadata ?: AuthorizationServerMetadata ;
799- clientInformation : OAuthClientInformation ;
814+ clientInformation : OAuthClientInformationMixed ;
800815 redirectUrl : string | URL ;
801816 scope ?: string ;
802817 state ?: string ;
@@ -879,7 +894,7 @@ export async function exchangeAuthorization(
879894 fetchFn
880895 } : {
881896 metadata ?: AuthorizationServerMetadata ;
882- clientInformation : OAuthClientInformation ;
897+ clientInformation : OAuthClientInformationMixed ;
883898 authorizationCode : string ;
884899 codeVerifier : string ;
885900 redirectUri : string | URL ;
@@ -958,7 +973,7 @@ export async function refreshAuthorization(
958973 fetchFn
959974 } : {
960975 metadata ?: AuthorizationServerMetadata ;
961- clientInformation : OAuthClientInformation ;
976+ clientInformation : OAuthClientInformationMixed ;
962977 refreshToken : string ;
963978 resource ?: URL ;
964979 addClientAuthentication ?: OAuthClientProvider [ 'addClientAuthentication' ] ;
0 commit comments