oauth: add tenant suffix to oauth flow (#1096)

This commit is contained in:
John Howard 2026-02-19 05:00:33 -08:00 • committed by GitHub
commit 695972a404
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 72 additions and 9 deletions

View file

@ -2,6 +2,7 @@ import {
generateOAuthErrorDescription,
parseOAuthCallbackParams,
generateOAuthState,
getAuthorizationServerMetadataDiscoveryUrl,
} from "@/utils/oauthUtils.ts";
describe("parseOAuthCallbackParams", () => {
@ -84,3 +85,29 @@ describe("generateOAuthErrorDescription", () => {
});
});
});
describe("getAuthorizationServerMetadataDiscoveryUrl", () => {
it("uses root discovery URL for root authorization server URL", () => {
expect(
getAuthorizationServerMetadataDiscoveryUrl("https://example.com"),
).toBe("https://example.com/.well-known/oauth-authorization-server");
});
it("inserts tenant path for non-root authorization server URL", () => {
expect(
getAuthorizationServerMetadataDiscoveryUrl("https://example.com/tenant1"),
).toBe(
"https://example.com/.well-known/oauth-authorization-server/tenant1",
);
});
it("strips trailing slash before appending tenant path", () => {
expect(
getAuthorizationServerMetadataDiscoveryUrl(
"https://example.com/tenant1/",
),
).toBe(
"https://example.com/.well-known/oauth-authorization-server/tenant1",
);
});
});

View file

@ -87,3 +87,31 @@ export const generateOAuthErrorDescription = (
.filter(Boolean)
.join("\n");
};
/**
* Returns the primary OAuth authorization server metadata discovery URL
* for a given authorization server URL, including tenant path handling.
*/
export const getAuthorizationServerMetadataDiscoveryUrl = (
authorizationServerUrl: string | URL,
): string => {
const url =
typeof authorizationServerUrl === "string"
? new URL(authorizationServerUrl)
: authorizationServerUrl;
const hasPath = url.pathname !== "/";
if (!hasPath) {
return new URL("/.well-known/oauth-authorization-server", url.origin).href;
}
// Strip trailing slash to avoid double slashes in tenant-aware discovery URLs.
const pathname = url.pathname.endsWith("/")
? url.pathname.slice(0, -1)
: url.pathname;
return new URL(
`/.well-known/oauth-authorization-server${pathname}`,
url.origin,
).href;
};