Skip to content

Commit 664e66e

Browse files
authored
Only use path-based discovery URLs from the authorization server to discover metadata (#1070)
1 parent 783d53b commit 664e66e

File tree

2 files changed

+11
-21
lines changed

2 files changed

+11
-21
lines changed

src/client/auth.test.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -712,16 +712,12 @@ describe('OAuth Authorization', () => {
712712
it('generates correct URLs for server with path', () => {
713713
const urls = buildDiscoveryUrls('https://auth.example.com/tenant1');
714714

715-
expect(urls).toHaveLength(4);
715+
expect(urls).toHaveLength(3);
716716
expect(urls.map(u => ({ url: u.url.toString(), type: u.type }))).toEqual([
717717
{
718718
url: 'https://auth.example.com/.well-known/oauth-authorization-server/tenant1',
719719
type: 'oauth'
720720
},
721-
{
722-
url: 'https://auth.example.com/.well-known/oauth-authorization-server',
723-
type: 'oauth'
724-
},
725721
{
726722
url: 'https://auth.example.com/.well-known/openid-configuration/tenant1',
727723
type: 'oidc'
@@ -736,7 +732,7 @@ describe('OAuth Authorization', () => {
736732
it('handles URL object input', () => {
737733
const urls = buildDiscoveryUrls(new URL('https://auth.example.com/tenant1'));
738734

739-
expect(urls).toHaveLength(4);
735+
expect(urls).toHaveLength(3);
740736
expect(urls[0].url.toString()).toBe('https://auth.example.com/.well-known/oauth-authorization-server/tenant1');
741737
});
742738
});
@@ -763,28 +759,28 @@ describe('OAuth Authorization', () => {
763759
};
764760

765761
it('tries URLs in order and returns first successful metadata', async () => {
766-
// First OAuth URL fails with 404
762+
// First OAuth URL (path before well-known) fails with 404
767763
mockFetch.mockResolvedValueOnce({
768764
ok: false,
769765
status: 404
770766
});
771767

772-
// Second OAuth URL (root) succeeds
768+
// Second OIDC URL (path before well-known) succeeds
773769
mockFetch.mockResolvedValueOnce({
774770
ok: true,
775771
status: 200,
776-
json: async () => validOAuthMetadata
772+
json: async () => validOpenIdMetadata
777773
});
778774

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

781-
expect(metadata).toEqual(validOAuthMetadata);
777+
expect(metadata).toEqual(validOpenIdMetadata);
782778

783779
// Verify it tried the URLs in the correct order
784780
const calls = mockFetch.mock.calls;
785781
expect(calls.length).toBe(2);
786782
expect(calls[0][0].toString()).toBe('https://auth.example.com/.well-known/oauth-authorization-server/tenant1');
787-
expect(calls[1][0].toString()).toBe('https://auth.example.com/.well-known/oauth-authorization-server');
783+
expect(calls[1][0].toString()).toBe('https://auth.example.com/.well-known/openid-configuration/tenant1');
788784
});
789785

790786
it('continues on 4xx errors', async () => {
@@ -878,7 +874,7 @@ describe('OAuth Authorization', () => {
878874
expect(metadata).toBeUndefined();
879875

880876
// Verify that all discovery URLs were attempted
881-
expect(mockFetch).toHaveBeenCalledTimes(8); // 4 URLs × 2 attempts each (with and without headers)
877+
expect(mockFetch).toHaveBeenCalledTimes(6); // 3 URLs × 2 attempts each (with and without headers)
882878
});
883879
});
884880

src/client/auth.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -669,8 +669,7 @@ export async function discoverOAuthMetadata(
669669
* Builds a list of discovery URLs to try for authorization server metadata.
670670
* URLs are returned in priority order:
671671
* 1. OAuth metadata at the given URL
672-
* 2. OAuth metadata at root (if URL has path)
673-
* 3. OIDC metadata endpoints
672+
* 2. OIDC metadata endpoints at the given URL
674673
*/
675674
export function buildDiscoveryUrls(authorizationServerUrl: string | URL): { url: URL; type: 'oauth' | 'oidc' }[] {
676675
const url = typeof authorizationServerUrl === 'string' ? new URL(authorizationServerUrl) : authorizationServerUrl;
@@ -706,18 +705,13 @@ export function buildDiscoveryUrls(authorizationServerUrl: string | URL): { url:
706705
type: 'oauth'
707706
});
708707

709-
// Root path: https://example.com/.well-known/oauth-authorization-server
710-
urlsToTry.push({
711-
url: new URL('/.well-known/oauth-authorization-server', url.origin),
712-
type: 'oauth'
713-
});
714-
715-
// 3. OIDC metadata endpoints
708+
// 2. OIDC metadata endpoints
716709
// RFC 8414 style: Insert /.well-known/openid-configuration before the path
717710
urlsToTry.push({
718711
url: new URL(`/.well-known/openid-configuration${pathname}`, url.origin),
719712
type: 'oidc'
720713
});
714+
721715
// OIDC Discovery 1.0 style: Append /.well-known/openid-configuration after the path
722716
urlsToTry.push({
723717
url: new URL(`${pathname}/.well-known/openid-configuration`, url.origin),

0 commit comments

Comments
 (0)