Skip to content

Commit 72e63e8

Browse files
committed
fix: continue auth discovery after invalid JSON
1 parent ab552c3 commit 72e63e8

2 files changed

Lines changed: 32 additions & 3 deletions

File tree

packages/client/src/client/auth.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1262,10 +1262,15 @@ export async function discoverAuthorizationServerMetadata(
12621262
);
12631263
}
12641264

1265+
let metadata: unknown;
1266+
try {
1267+
metadata = await response.json();
1268+
} catch {
1269+
continue;
1270+
}
1271+
12651272
// Parse and validate based on type
1266-
return type === 'oauth'
1267-
? OAuthMetadataSchema.parse(await response.json())
1268-
: OpenIdProviderDiscoveryMetadataSchema.parse(await response.json());
1273+
return type === 'oauth' ? OAuthMetadataSchema.parse(metadata) : OpenIdProviderDiscoveryMetadataSchema.parse(metadata);
12691274
}
12701275

12711276
return undefined;

packages/client/test/client/auth.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -907,6 +907,30 @@ describe('OAuth Authorization', () => {
907907
expect(calls[1]![0].toString()).toBe('https://auth.example.com/.well-known/openid-configuration/tenant1');
908908
});
909909

910+
it('continues when an endpoint returns non-JSON metadata', async () => {
911+
mockFetch.mockResolvedValueOnce({
912+
ok: true,
913+
status: 200,
914+
json: async () => {
915+
throw new SyntaxError('Unexpected token < in JSON');
916+
}
917+
});
918+
919+
mockFetch.mockResolvedValueOnce({
920+
ok: true,
921+
status: 200,
922+
json: async () => validOpenIdMetadata
923+
});
924+
925+
const metadata = await discoverAuthorizationServerMetadata('https://auth.example.com/tenant1');
926+
927+
expect(metadata).toEqual(validOpenIdMetadata);
928+
const calls = mockFetch.mock.calls;
929+
expect(calls.length).toBe(2);
930+
expect(calls[0]![0].toString()).toBe('https://auth.example.com/.well-known/oauth-authorization-server/tenant1');
931+
expect(calls[1]![0].toString()).toBe('https://auth.example.com/.well-known/openid-configuration/tenant1');
932+
});
933+
910934
it('continues on 4xx errors', async () => {
911935
mockFetch.mockResolvedValueOnce({
912936
ok: false,

0 commit comments

Comments
 (0)