Add per-enterprise custom HTTP headers and cookies
Some Seal tenants sit behind a gateway that only forwards requests carrying its own session cookie. Corp configs can now set seal.headers/seal.cookies, which the client merges into every request; cookies are rendered into one Cookie header and the session bearer token still wins over any configured Authorization. corps add-seal accepts both fields and applies them when verifying the tenant, so a gated enterprise can be registered in one step.
This commit is contained in:
1 parent
fc06434813
commit
bb8b5edec6
6 files changed
+203
-6
No files matched your search
@@ -1,6 +1,7 @@
|
||||
import ky, { HTTPError, KyInstance } from "ky";
|
||||
import { CorpConfig } from "../config/types.js";
|
||||
import { clearSealToken, getSealSessionForCorp } from "../auth/seal.js";
|
||||
import { resolveSealCustomHeaders } from "./request-headers.js";
|
||||
|
||||
export async function createSealClient(corp: CorpConfig): Promise<KyInstance> {
|
||||
const session = await getSealSessionForCorp(corp);
|
||||
@@ -9,6 +10,7 @@ export async function createSealClient(corp: CorpConfig): Promise<KyInstance> {
|
||||
prefix: session.enterprise.sealUrl,
|
||||
timeout: 30000,
|
||||
headers: {
|
||||
...resolveSealCustomHeaders(corp.seal),
|
||||
Authorization: `Bearer ${session.token}`
|
||||
},
|
||||
hooks: {
|
||||
@@ -32,14 +34,19 @@ export async function createSealClient(corp: CorpConfig): Promise<KyInstance> {
|
||||
export async function createSealClientForToken(corp: CorpConfig, token: string): Promise<KyInstance> {
|
||||
const session = await getSealSessionForCorp(corp);
|
||||
|
||||
return createSealClientForUrl(session.enterprise.sealUrl, token);
|
||||
return createSealClientForUrl(session.enterprise.sealUrl, token, resolveSealCustomHeaders(corp.seal));
|
||||
}
|
||||
|
||||
export function createSealClientForUrl(sealUrl: string, token: string): KyInstance {
|
||||
export function createSealClientForUrl(
|
||||
sealUrl: string,
|
||||
token: string,
|
||||
customHeaders: Record<string, string> = {}
|
||||
): KyInstance {
|
||||
return ky.create({
|
||||
prefix: sealUrl.replace(/\/+$/, ""),
|
||||
timeout: 30000,
|
||||
headers: {
|
||||
...customHeaders,
|
||||
Authorization: `Bearer ${token}`
|
||||
},
|
||||
hooks: {
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import { createSealClientForUrl } from "./factory.js";
|
||||
import { formatCookieHeader, resolveSealCustomHeaders } from "./request-headers.js";
|
||||
|
||||
describe("formatCookieHeader", () => {
|
||||
test("returns undefined when there are no cookies", () => {
|
||||
expect(formatCookieHeader(undefined)).toBeUndefined();
|
||||
expect(formatCookieHeader({})).toBeUndefined();
|
||||
});
|
||||
|
||||
test("joins cookies with a semicolon and a space", () => {
|
||||
expect(formatCookieHeader({ a: "1", b: "2" })).toBe("a=1; b=2");
|
||||
});
|
||||
});
|
||||
|
||||
describe("resolveSealCustomHeaders", () => {
|
||||
test("returns an empty map when no custom headers are configured", () => {
|
||||
expect(resolveSealCustomHeaders(undefined)).toEqual({});
|
||||
});
|
||||
|
||||
test("keeps configured headers and renders cookies", () => {
|
||||
expect(resolveSealCustomHeaders({
|
||||
headers: { "X-Tenant": "acme" },
|
||||
cookies: { sdp_session: "token" }
|
||||
})).toEqual({
|
||||
"X-Tenant": "acme",
|
||||
Cookie: "sdp_session=token"
|
||||
});
|
||||
});
|
||||
|
||||
test("lets configured cookies replace a raw Cookie header", () => {
|
||||
expect(resolveSealCustomHeaders({
|
||||
headers: { Cookie: "stale=1" },
|
||||
cookies: { fresh: "2" }
|
||||
})).toEqual({ Cookie: "fresh=2" });
|
||||
});
|
||||
});
|
||||
|
||||
describe("createSealClientForUrl", () => {
|
||||
test("sends custom headers and keeps the session Authorization", async () => {
|
||||
const originalFetch = globalThis.fetch;
|
||||
let sent: Headers | undefined;
|
||||
globalThis.fetch = (async (input: RequestInfo | URL, init?: RequestInit) => {
|
||||
const request = input instanceof Request ? input : new Request(input, init);
|
||||
sent = new Headers(request.headers);
|
||||
return new Response(JSON.stringify({ data: {} }), {
|
||||
headers: { "content-type": "application/json" }
|
||||
});
|
||||
}) as typeof fetch;
|
||||
|
||||
try {
|
||||
const client = createSealClientForUrl("https://corp-a.sealai.cc", "session-token", {
|
||||
Cookie: "sdp_session=token",
|
||||
Authorization: "Bearer stale"
|
||||
});
|
||||
await client.get("api/v1/auth/me");
|
||||
} finally {
|
||||
globalThis.fetch = originalFetch;
|
||||
}
|
||||
|
||||
expect(sent?.get("cookie")).toBe("sdp_session=token");
|
||||
expect(sent?.get("authorization")).toBe("Bearer session-token");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,20 @@
|
||||
import type { CorpConfig } from "../config/types.js";
|
||||
|
||||
// The subset of the Seal endpoint config that carries extra request headers.
|
||||
export type SealCustomHeaderSource = Pick<CorpConfig["seal"], "headers" | "cookies">;
|
||||
|
||||
// Renders configured cookies into one Cookie header, joined with "; ".
|
||||
export function formatCookieHeader(cookies: Record<string, string> | undefined): string | undefined {
|
||||
const pairs = Object.entries(cookies ?? {}).map(([name, value]) => `${name}=${value}`);
|
||||
return pairs.length ? pairs.join("; ") : undefined;
|
||||
}
|
||||
|
||||
// Extra tenant headers from corp config. A rendered Cookie header replaces a
|
||||
// same-named entry, while Authorization is always left to the caller so the
|
||||
// session bearer token can never be silently replaced by config.
|
||||
export function resolveSealCustomHeaders(seal: SealCustomHeaderSource | undefined): Record<string, string> {
|
||||
const headers: Record<string, string> = { ...(seal?.headers ?? {}) };
|
||||
const cookieHeader = formatCookieHeader(seal?.cookies);
|
||||
if (cookieHeader) headers.Cookie = cookieHeader;
|
||||
return headers;
|
||||
}
|
||||
Reference in new issue
Block a user