fix: continue OAuth metadata discovery on 5xx responses (#1632)
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com> Co-authored-by: Felix Weinberger <3823880+felixweinberger@users.noreply.github.com> Co-authored-by: Paul Carleton <paulcarletonjr@gmail.com>
This commit is contained in:
parent
045c62a1e0
commit
d99f3ee527
1 changed files with 115 additions and 12 deletions
|
|
@ -1035,7 +1035,9 @@ async function tryMetadataDiscovery(url: URL, protocolVersion: string, fetchFn:
|
|||
* Determines if fallback to root discovery should be attempted
|
||||
*/
|
||||
function shouldAttemptFallback(response: Response | undefined, pathname: string): boolean {
|
||||
return !response || (response.status >= 400 && response.status < 500 && pathname !== '/');
|
||||
if (!response) return true; // CORS error — always try fallback
|
||||
if (pathname === '/') return false; // Already at root
|
||||
return (response.status >= 400 && response.status < 500) || response.status === 502;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -1062,7 +1064,7 @@ async function discoverMetadataWithFallback(
|
|||
|
||||
let response = await tryMetadataDiscovery(url, protocolVersion, fetchFn);
|
||||
|
||||
// If path-aware discovery fails with 404 and we're not already at root, try fallback to root discovery
|
||||
// If path-aware discovery fails (4xx or 502 Bad Gateway) and we're not already at root, try fallback to root discovery
|
||||
if (!opts?.metadataUrl && shouldAttemptFallback(response, issuer.pathname)) {
|
||||
const rootUrl = new URL(`/.well-known/${wellKnownType}`, issuer);
|
||||
response = await tryMetadataDiscovery(rootUrl, protocolVersion, fetchFn);
|
||||
|
|
@ -1230,9 +1232,8 @@ export async function discoverAuthorizationServerMetadata(
|
|||
|
||||
if (!response.ok) {
|
||||
await response.text?.().catch(() => {});
|
||||
// Continue looking for any 4xx response code.
|
||||
if (response.status >= 400 && response.status < 500) {
|
||||
continue; // Try next URL
|
||||
if ((response.status >= 400 && response.status < 500) || response.status === 502) {
|
||||
continue; // Try next URL for 4xx or 502 (Bad Gateway)
|
||||
}
|
||||
throw new Error(
|
||||
`HTTP ${response.status} trying to load ${type === 'oauth' ? 'OAuth' : 'OpenID provider'} metadata from ${endpointUrl}`
|
||||
|
|
|
|||
Loading…
Reference in a new issue