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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ test-results/
playwright-report/
blob-report/
playwright/.cache/
demo-assets/
2 changes: 1 addition & 1 deletion apps/api/src/local/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ async function main(): Promise<void> {
app.use(express.json());
app.use((_req, res, next) => {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "content-type, x-api-key");
res.header("Access-Control-Allow-Headers", "content-type, x-api-key, authorization");
next();
});

Expand Down
12 changes: 11 additions & 1 deletion apps/web/src/lib/cognito.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,17 @@ export interface IdTokenClaims {
export function decodeIdToken(idToken: string): IdTokenClaims {
const [, payloadB64] = idToken.split(".");
const payload = JSON.parse(atob(payloadB64.replace(/-/g, "+").replace(/_/g, "/"))) as Record<string, unknown>;
const groups = typeof payload["cognito:groups"] === "string" ? (payload["cognito:groups"] as string) : "";
// A real Cognito ID token encodes `cognito:groups` as a genuine JSON
// array (e.g. ["owner"]); API Gateway's HTTP API JWT authorizer is the
// only place that ever flattens it to a comma-separated string (see
// authContext.ts) — this decodes the token directly, so both shapes
// need handling here.
const rawGroups = payload["cognito:groups"];
const groups = Array.isArray(rawGroups)
? rawGroups.map(String)
: typeof rawGroups === "string"
? rawGroups.split(",").map((group) => group.trim())
: [];
const role = (["owner", "manager", "counter_staff"] as const).find((candidate) => groups.includes(candidate));
return {
sub: payload.sub as string,
Expand Down
7 changes: 6 additions & 1 deletion apps/web/test/lib/cognito.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,16 @@ describe("decodeIdToken", () => {
expect(decodeIdToken(token)).toEqual({ sub: "abc", email: "a@b.com", shopId: "shop-123", role: undefined });
});

it("resolves the role from a comma-flattened cognito:groups claim", () => {
it("resolves the role from a comma-flattened cognito:groups claim (API Gateway JWT authorizer shape)", () => {
const token = fakeIdToken({ sub: "abc", "cognito:groups": "owner" });
expect(decodeIdToken(token).role).toBe("owner");
});

it("resolves the role from a real Cognito ID token's array-shaped cognito:groups claim", () => {
const token = fakeIdToken({ sub: "abc", "cognito:groups": ["owner"] });
expect(decodeIdToken(token).role).toBe("owner");
});

it("returns undefined role when no known group is present", () => {
const token = fakeIdToken({ sub: "abc" });
expect(decodeIdToken(token).role).toBeUndefined();
Expand Down
Loading