@@ -453,19 +453,34 @@ async def _discover_auth_metadata(authorization_server_url: str) -> Authorizatio
453453
454454
455455@alru_cache (ttl = timedelta (minutes = 10 ).seconds )
456- async def _discover_resource_metadata (resource_server_url : str ) -> _ResourceServerMetadata | None :
456+ async def _discover_resource_metadata (resource_url : str ) -> _ResourceServerMetadata | None :
457+ parsed = urlparse (resource_url )
458+ resource_root_url = f"{ parsed .scheme } ://{ parsed .netloc } "
459+
457460 # RFC9728 hasn't been implemented yet in authlib
458461 # Reusing util from RFC8414
459- url = get_well_known_url (resource_server_url , external = True , suffix = "oauth-protected-resource" )
462+ path_url = get_well_known_url (resource_url , external = True , suffix = "oauth-protected-resource" )
463+ root_url = get_well_known_url (resource_root_url , external = True , suffix = "oauth-protected-resource" )
464+ urls = [path_url ]
465+ if path_url != root_url : # avoid duplicate
466+ urls .append (root_url )
467+ exceptions = []
460468 async with httpx .AsyncClient (
461469 headers = {"Accept" : "application/json" },
462470 follow_redirects = True ,
463471 ) as client :
464- response = await client .get (url )
465- if response .status_code == status .HTTP_404_NOT_FOUND :
466- return None
467- response .raise_for_status ()
468- return _ResourceServerMetadata .model_validate (response .json ())
472+ for url in urls :
473+ try :
474+ response = await client .get (url )
475+ response .raise_for_status ()
476+ return _ResourceServerMetadata .model_validate (response .json ())
477+ except Exception as exc :
478+ exceptions .append (exc )
479+ logger .warning (
480+ "Resource metadata discovery failed" ,
481+ exc_info = ExceptionGroup (f"Unable to discover metadata for resource { resource_url } " , exceptions ),
482+ )
483+ return None
469484
470485
471486def _render_success ():
0 commit comments