Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions packages/client/src/client/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1262,10 +1262,15 @@ export async function discoverAuthorizationServerMetadata(
);
}

let metadata: unknown;
try {
metadata = await response.json();
} catch {
continue;
}

// Parse and validate based on type
return type === 'oauth'
? OAuthMetadataSchema.parse(await response.json())
: OpenIdProviderDiscoveryMetadataSchema.parse(await response.json());
return type === 'oauth' ? OAuthMetadataSchema.parse(metadata) : OpenIdProviderDiscoveryMetadataSchema.parse(metadata);
}

return undefined;
Expand Down
25 changes: 25 additions & 0 deletions packages/client/test/client/auth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -924,6 +924,31 @@ describe('OAuth Authorization', () => {
expect(metadata).toEqual(validOpenIdMetadata);
});

it('continues when a successful metadata response is not JSON', async () => {
mockFetch.mockResolvedValueOnce({
ok: true,
status: 200,
json: async () => {
throw new SyntaxError('Unexpected token <');
}
});

mockFetch.mockResolvedValueOnce({
ok: true,
status: 200,
json: async () => validOpenIdMetadata
});

const metadata = await discoverAuthorizationServerMetadata('https://auth.example.com/tenant1');

expect(metadata).toEqual(validOpenIdMetadata);

const calls = mockFetch.mock.calls;
expect(calls.length).toBe(2);
expect(calls[0]![0].toString()).toBe('https://auth.example.com/.well-known/oauth-authorization-server/tenant1');
expect(calls[1]![0].toString()).toBe('https://auth.example.com/.well-known/openid-configuration/tenant1');
});

it('continues on 502 and tries next URL', async () => {
// First URL (OAuth) returns 502 (reverse proxy with no route)
mockFetch.mockResolvedValueOnce({
Expand Down
Loading