File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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 ;
Original file line number Diff line number Diff 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 ,
You can’t perform that action at this time.
0 commit comments