Skip to content

feat: allow OIDC configuration for OAuth server #585

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
14 changes: 14 additions & 0 deletions src/client/auth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,20 @@ describe("OAuth Authorization", () => {
});
});

it("returns metadata when discovery succeeds", async () => {
mockFetch.mockResolvedValueOnce({
ok: true,
status: 200,
json: async () => validMetadata,
});

await discoverOAuthMetadata("https://auth.example.com", {useOidcConfig: true});
const calls = mockFetch.mock.calls;
expect(calls.length).toBe(1);
const [url] = calls[0];
expect(url.toString()).toBe("https://auth.example.com/.well-known/openid-configuration");
});

it("returns metadata when first fetch fails but second without MCP header succeeds", async () => {
// Set up a counter to control behavior
let callCount = 0;
Expand Down
21 changes: 18 additions & 3 deletions src/client/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ export interface OAuthClientProvider {
* the authorization result.
*/
codeVerifier(): string | Promise<string>;

/**
* Use OpenID Provider configuration information for authorization
* server metadata.
* https://openid.net/specs/openid-connect-discovery-1_0.html#ProviderConfig
*/
useOidcProviderConfiguration?(): boolean | Promise<boolean>;
}

export type AuthResult = "AUTHORIZED" | "REDIRECT";
Expand Down Expand Up @@ -111,7 +118,9 @@ export async function auth(
console.warn("Could not load OAuth Protected Resource metadata, falling back to /.well-known/oauth-authorization-server", error)
}

const metadata = await discoverOAuthMetadata(authorizationServerUrl);
const metadata = await discoverOAuthMetadata(authorizationServerUrl, {
useOidcConfig: await provider.useOidcProviderConfiguration?.()
});

// Handle client registration if needed
let clientInformation = await Promise.resolve(provider.clientInformation());
Expand Down Expand Up @@ -267,9 +276,15 @@ export async function discoverOAuthProtectedResourceMetadata(
*/
export async function discoverOAuthMetadata(
authorizationServerUrl: string | URL,
opts?: { protocolVersion?: string },
opts?: {
protocolVersion?: string
useOidcConfig?: boolean
},
): Promise<OAuthMetadata | undefined> {
const url = new URL("/.well-known/oauth-authorization-server", authorizationServerUrl);
const metadataPath = opts?.useOidcConfig ?
"openid-configuration" :
"oauth-authorization-server";
const url = new URL(`/.well-known/${metadataPath}`, authorizationServerUrl);
let response: Response;
try {
response = await fetch(url, {
Expand Down