@@ -251,7 +251,9 @@ export async function auth(
251
251
252
252
const resource : URL | undefined = await selectResourceURL ( serverUrl , provider , resourceMetadata ) ;
253
253
254
- const metadata = await discoverOAuthMetadata ( authorizationServerUrl ) ;
254
+ const metadata = await discoverOAuthMetadata ( serverUrl , {
255
+ authorizationServerUrl
256
+ } ) ;
255
257
256
258
// Handle client registration if needed
257
259
let clientInformation = await Promise . resolve ( provider . clientInformation ( ) ) ;
@@ -469,7 +471,7 @@ function shouldAttemptFallback(response: Response | undefined, pathname: string)
469
471
async function discoverMetadataWithFallback (
470
472
serverUrl : string | URL ,
471
473
wellKnownType : 'oauth-authorization-server' | 'oauth-protected-resource' ,
472
- opts ?: { protocolVersion ?: string ; metadataUrl ?: string | URL } ,
474
+ opts ?: { protocolVersion ?: string ; metadataUrl ?: string | URL , metadataServerUrl ?: string | URL } ,
473
475
) : Promise < Response | undefined > {
474
476
const issuer = new URL ( serverUrl ) ;
475
477
const protocolVersion = opts ?. protocolVersion ?? LATEST_PROTOCOL_VERSION ;
@@ -480,7 +482,7 @@ async function discoverMetadataWithFallback(
480
482
} else {
481
483
// Try path-aware discovery first
482
484
const wellKnownPath = buildWellKnownPath ( wellKnownType , issuer . pathname ) ;
483
- url = new URL ( wellKnownPath , issuer ) ;
485
+ url = new URL ( wellKnownPath , opts ?. metadataServerUrl ?? issuer ) ;
484
486
url . search = issuer . search ;
485
487
}
486
488
@@ -502,13 +504,33 @@ async function discoverMetadataWithFallback(
502
504
* return `undefined`. Any other errors will be thrown as exceptions.
503
505
*/
504
506
export 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
+ } = { } ,
507
515
) : 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
+
508
527
const response = await discoverMetadataWithFallback (
509
- authorizationServerUrl ,
528
+ issuer ,
510
529
'oauth-authorization-server' ,
511
- opts ,
530
+ {
531
+ protocolVersion,
532
+ metadataServerUrl : authorizationServerUrl ,
533
+ } ,
512
534
) ;
513
535
514
536
if ( ! response || response . status === 404 ) {
0 commit comments