diff --git a/plugins/oidc/server/oidcDiscovery.test.ts b/plugins/oidc/server/oidcDiscovery.test.ts index 7f7640afea..3f581b1dd4 100644 --- a/plugins/oidc/server/oidcDiscovery.test.ts +++ b/plugins/oidc/server/oidcDiscovery.test.ts @@ -97,4 +97,84 @@ describe("fetchOIDCConfiguration", () => { "Missing authorization_endpoint in OIDC configuration" ); }); + + it("should handle issuer URL with subdirectory path", async () => { + const mockConfig = { + issuer: "https://auth.example.com/application/o/outline/", + authorization_endpoint: + "https://auth.example.com/application/o/outline/auth", + token_endpoint: "https://auth.example.com/application/o/outline/token", + userinfo_endpoint: + "https://auth.example.com/application/o/outline/userinfo", + }; + + fetchMock.mockResponseOnce(JSON.stringify(mockConfig)); + + const result = await fetchOIDCConfiguration( + "https://auth.example.com/application/o/outline/" + ); + + expect(fetchMock).toHaveBeenCalledWith( + "https://auth.example.com/application/o/outline/.well-known/openid-configuration", + expect.objectContaining({ + method: "GET", + headers: expect.objectContaining({ + Accept: "application/json", + }), + }) + ); + + expect(result).toEqual(mockConfig); + }); + + it("should handle issuer URL with subdirectory path without trailing slash", async () => { + const mockConfig = { + issuer: "https://auth.example.com/application/o/outline", + authorization_endpoint: + "https://auth.example.com/application/o/outline/auth", + token_endpoint: "https://auth.example.com/application/o/outline/token", + userinfo_endpoint: + "https://auth.example.com/application/o/outline/userinfo", + }; + + fetchMock.mockResponseOnce(JSON.stringify(mockConfig)); + + const result = await fetchOIDCConfiguration( + "https://auth.example.com/application/o/outline" + ); + + expect(fetchMock).toHaveBeenCalledWith( + "https://auth.example.com/application/o/outline/.well-known/openid-configuration", + expect.objectContaining({ + method: "GET", + headers: expect.objectContaining({ + Accept: "application/json", + }), + }) + ); + + expect(result).toEqual(mockConfig); + }); + + it("should handle issuer URL that already contains well-known path", async () => { + const mockConfig = { + issuer: "https://example.com", + authorization_endpoint: "https://example.com/auth", + token_endpoint: "https://example.com/token", + userinfo_endpoint: "https://example.com/userinfo", + }; + + fetchMock.mockResponseOnce(JSON.stringify(mockConfig)); + + const result = await fetchOIDCConfiguration( + "https://example.com/.well-known/openid-configuration" + ); + + expect(fetchMock).toHaveBeenCalledWith( + "https://example.com/.well-known/openid-configuration", + expect.any(Object) + ); + + expect(result).toEqual(mockConfig); + }); }); diff --git a/plugins/oidc/server/oidcDiscovery.ts b/plugins/oidc/server/oidcDiscovery.ts index ce0f388546..bf6a31c0b5 100644 --- a/plugins/oidc/server/oidcDiscovery.ts +++ b/plugins/oidc/server/oidcDiscovery.ts @@ -25,9 +25,24 @@ export async function fetchOIDCConfiguration( ): Promise { try { const wellKnownPath = "/.well-known/openid-configuration"; - const wellKnownUrl = issuerUrl.includes(wellKnownPath) - ? issuerUrl - : new URL(wellKnownPath, issuerUrl).toString(); + + let wellKnownUrl: string; + + // If the issuer URL already includes the well-known path, use it as-is + if (issuerUrl.includes(wellKnownPath)) { + wellKnownUrl = issuerUrl; + } else { + // Properly append well-known path to the issuer URL path + const url = new URL(issuerUrl); + // If pathname doesn't end with slash, append the full wellKnownPath (with leading slash) + if (!url.pathname.endsWith("/")) { + url.pathname += wellKnownPath; + } else { + // If pathname ends with slash, append wellKnownPath without leading slash + url.pathname += wellKnownPath.substring(1); + } + wellKnownUrl = url.toString(); + } Logger.info("plugins", `Fetching OIDC configuration from ${wellKnownUrl}`);