@@ -251,7 +251,9 @@ export async function auth(
251251
252252 const resource : URL | undefined = await selectResourceURL ( serverUrl , provider , resourceMetadata ) ;
253253
254- const metadata = await discoverOAuthMetadata ( authorizationServerUrl ) ;
254+ const metadata = await discoverOAuthMetadata ( serverUrl , {
255+ authorizationServerUrl
256+ } ) ;
255257
256258 // Handle client registration if needed
257259 let clientInformation = await Promise . resolve ( provider . clientInformation ( ) ) ;
@@ -469,7 +471,7 @@ function shouldAttemptFallback(response: Response | undefined, pathname: string)
469471async function discoverMetadataWithFallback (
470472 serverUrl : string | URL ,
471473 wellKnownType : 'oauth-authorization-server' | 'oauth-protected-resource' ,
472- opts ?: { protocolVersion ?: string ; metadataUrl ?: string | URL } ,
474+ opts ?: { protocolVersion ?: string ; metadataUrl ?: string | URL , metadataServerUrl ?: string | URL } ,
473475) : Promise < Response | undefined > {
474476 const issuer = new URL ( serverUrl ) ;
475477 const protocolVersion = opts ?. protocolVersion ?? LATEST_PROTOCOL_VERSION ;
@@ -480,7 +482,7 @@ async function discoverMetadataWithFallback(
480482 } else {
481483 // Try path-aware discovery first
482484 const wellKnownPath = buildWellKnownPath ( wellKnownType , issuer . pathname ) ;
483- url = new URL ( wellKnownPath , issuer ) ;
485+ url = new URL ( wellKnownPath , opts ?. metadataServerUrl ?? issuer ) ;
484486 url . search = issuer . search ;
485487 }
486488
@@ -502,13 +504,33 @@ async function discoverMetadataWithFallback(
502504 * return `undefined`. Any other errors will be thrown as exceptions.
503505 */
504506export async function discoverOAuthMetadata (
505- authorizationServerUrl : string | URL ,
506- opts ?: { protocolVersion ?: string } ,
507+ issuer : string | URL ,
508+ {
509+ authorizationServerUrl,
510+ protocolVersion,
511+ } : {
512+ authorizationServerUrl ?: string | URL ,
513+ protocolVersion ?: string ,
514+ } = { } ,
507515) : Promise < OAuthMetadata | undefined > {
516+ if ( typeof issuer === 'string' ) {
517+ issuer = new URL ( issuer ) ;
518+ }
519+ if ( ! authorizationServerUrl ) {
520+ authorizationServerUrl = issuer ;
521+ }
522+ if ( typeof authorizationServerUrl === 'string' ) {
523+ authorizationServerUrl = new URL ( authorizationServerUrl ) ;
524+ }
525+ protocolVersion ??= LATEST_PROTOCOL_VERSION ;
526+
508527 const response = await discoverMetadataWithFallback (
509- authorizationServerUrl ,
528+ issuer ,
510529 'oauth-authorization-server' ,
511- opts ,
530+ {
531+ protocolVersion,
532+ metadataServerUrl : authorizationServerUrl ,
533+ } ,
512534 ) ;
513535
514536 if ( ! response || response . status === 404 ) {
0 commit comments