From f49e9b8c4dc8e3327e8821c790fb4369d0c024eb Mon Sep 17 00:00:00 2001 From: "gp42-ops (GitHub)" <91820843+gp42-ops@users.noreply.github.com> Date: Sun, 3 May 2026 18:46:36 +0300 Subject: [PATCH] feat: infer partition from homePartition claim, drop aud fallback Reads the access token's `homePartition` claim to default the SDK's target partition. Drops the previous fallback that inferred partition from a single-element `aud` claim - partition is now driven exclusively by the explicit user-metadata claim, with an explicit `partition` arg required when no claim is present. --- README.md | 7 ++++-- src/__tests__/client.test.ts | 43 +++++++++++++++++++++++------------- src/client.ts | 15 +++++-------- 3 files changed, 39 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index e6a8cbb..8bf875e 100644 --- a/README.md +++ b/README.md @@ -122,10 +122,13 @@ A discovery endpoint (`GET /v1/partitions`) returns the live list with labels. ### Default partition from token -If `partition` is omitted and the access token's `aud` claim is a single string identifying a known partition, the SDK uses it as the default. Multi-partition tokens (`aud` is an array) require an explicit `partition`. +If `partition` is omitted, the SDK reads the access token's `homePartition` claim and uses it as the default. To call a partition other than the user's home partition, pass `partition` explicitly. ```ts -// One token authorized for both partitions: pick which one to call. +// Default to the user's home partition (from the token's `homePartition` claim). +const home = new Hostsmith({ accessToken: token }); + +// One token authorized for both partitions: target a specific one. const us = new Hostsmith({ accessToken: token, partition: "us" }); const eu = new Hostsmith({ accessToken: token, partition: "eu" }); ``` diff --git a/src/__tests__/client.test.ts b/src/__tests__/client.test.ts index 3f01e16..bdcdc71 100644 --- a/src/__tests__/client.test.ts +++ b/src/__tests__/client.test.ts @@ -4,11 +4,9 @@ import { SitesResource } from "../resources/sites.js"; import { DomainsResource } from "../resources/domains.js"; import { AccountResource } from "../resources/account.js"; -function makeJwt(audience: string | string[] | undefined): string { +function makeJwt(claims: Record): string { const header = Buffer.from(JSON.stringify({ alg: "RS256", typ: "JWT" })).toString("base64url"); - const payload = Buffer.from( - JSON.stringify(audience === undefined ? {} : { aud: audience }), - ).toString("base64url"); + const payload = Buffer.from(JSON.stringify(claims)).toString("base64url"); return `${header}.${payload}.sig`; } @@ -58,9 +56,9 @@ describe("Hostsmith client", () => { ); }); - it("infers partition from single-string aud claim", async () => { + it("infers partition from homePartition claim", async () => { const client = new Hostsmith({ - accessToken: makeJwt("https://eu.api.hostsmith.net"), + accessToken: makeJwt({ homePartition: "eu" }), }); await client.sites.list(); expect(vi.mocked(globalThis.fetch)).toHaveBeenCalledWith( @@ -69,20 +67,35 @@ describe("Hostsmith client", () => { ); }); - it("requires partition when aud is an array (multi-partition token)", () => { + it("explicit partition wins over homePartition claim", async () => { + const client = new Hostsmith({ + accessToken: makeJwt({ homePartition: "eu" }), + partition: "us", + }); + await client.sites.list(); + expect(vi.mocked(globalThis.fetch)).toHaveBeenCalledWith( + "https://us.api.hostsmith.net/v1/sites", + expect.anything(), + ); + }); + + it("does not infer partition from aud", () => { expect(() => new Hostsmith({ - accessToken: makeJwt([ - "https://us.api.hostsmith.net", - "https://eu.api.hostsmith.net", - ]), + accessToken: makeJwt({ aud: "https://eu.api.hostsmith.net" }), }), ).toThrow(/partition/i); }); - it("requires partition when aud is missing", () => { + it("requires partition when homePartition claim is missing", () => { + expect(() => + new Hostsmith({ accessToken: makeJwt({}) }), + ).toThrow(/partition/i); + }); + + it("requires partition when homePartition claim is unknown", () => { expect(() => - new Hostsmith({ accessToken: makeJwt(undefined) }), + new Hostsmith({ accessToken: makeJwt({ homePartition: "ap" }) }), ).toThrow(/partition/i); }); @@ -102,9 +115,9 @@ describe("Hostsmith client", () => { ); }); - it("infers partition from aud against overridden partitionUrls", async () => { + it("infers partition from homePartition claim against overridden partitionUrls", async () => { const client = new Hostsmith({ - accessToken: makeJwt("https://eu.api.hostsmith-dev.com"), + accessToken: makeJwt({ homePartition: "eu" }), partitionUrls: { eu: "https://eu.api.hostsmith-dev.com" }, }); await client.sites.list(); diff --git a/src/client.ts b/src/client.ts index 1ab3ae8..ab6eb75 100644 --- a/src/client.ts +++ b/src/client.ts @@ -23,16 +23,13 @@ function decodeJwtPayload(token: string): Record | undefined { } } -function partitionFromAud( - token: string, - partitionUrls: Record, -): Partition | undefined { +function partitionFromToken(token: string): Partition | undefined { const payload = decodeJwtPayload(token); if (!payload) return undefined; - const aud = payload.aud; - if (typeof aud !== "string") return undefined; + const home = payload.homePartition; + if (typeof home !== "string") return undefined; for (const p of KNOWN_PARTITIONS) { - if (aud === partitionUrls[p]) return p; + if (home === p) return p; } return undefined; } @@ -60,11 +57,11 @@ export class Hostsmith { } baseUrl = url; } else { - const inferred = partitionFromAud(options.accessToken, partitionUrls); + const inferred = partitionFromToken(options.accessToken); if (!inferred) { throw new Error( "Hostsmith client requires `partition` (\"us\" | \"eu\") or `baseUrl`. " + - "The supplied access token does not have a single-partition `aud` claim, " + + "The supplied access token does not carry a `homePartition` claim, " + "so the partition cannot be inferred automatically.", ); }