Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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" });
```
Expand Down
43 changes: 28 additions & 15 deletions src/__tests__/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, unknown>): 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`;
}

Expand Down Expand Up @@ -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(
Expand All @@ -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);
});

Expand All @@ -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();
Expand Down
15 changes: 6 additions & 9 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,13 @@ function decodeJwtPayload(token: string): Record<string, unknown> | undefined {
}
}

function partitionFromAud(
token: string,
partitionUrls: Record<Partition, string>,
): 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;
}
Expand Down Expand Up @@ -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.",
);
}
Expand Down
Loading