oauth: add tenant suffix to oauth flow (#1096)
This commit is contained in:
parent
f2ed4ef3b9
commit
695972a404
2 changed files with 72 additions and 9 deletions
|
|
@ -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",
|
||||
);
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
};
|
||||
|
|
|
|||
Loading…
Reference in a new issue