Skip to content

feat(claims): static custom claims for roles and OpenID clients - #584

Merged
Bccorb merged 18 commits into
mainfrom
feat/custom-claims
Nov 8, 2026
Merged

Bccorb merged 18 commits into
mainfrom
feat/custom-claims

Conversation

@Bccorb

@Bccorb Bccorb commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

Stacked on #583 (feat/login-hooks), which is stacked on #580 (feat/signed-webhooks). Merge those first; the base moves down as each lands. Builds against the published @seamless-auth/types 0.40.0.

What it adds

ADR 0005 E6: static custom claims.

  • Table custom_claims (sourceKind, sourceName, claims jsonb, timestamps), keyed by source, with a check on the kind.
  • Admin routes per CustomClaimRoutes: GET /admin/custom-claims (sources, reserved names and prefixes, budget) and GET/PUT/DELETE /admin/custom-claims/:sourceKind/:sourceName. Writes take requireAdmin('write') and step-up, are in GUARDED, and are audited (admin_custom_claims_updated, admin_custom_claims_deleted) by claim name only, since values are free text in a permanent log.
  • Validation uses the shared SetCustomClaimsRequestSchema directly, and the responses use the shared CustomClaimSourceResponseSchema and CustomClaimListResponseSchema: reserved names and prefixes, the name pattern, depth 3 and 1 KiB per set are refused. The source must exist: an instance role in available_roles, an organization role in the catalogue, or an existing OpenID Provider client. oidc_client sets are stored and served only; seamless-auth-oidc-provider#47 applies them.
  • Minting. mintAccessToken merges the user's instance role sets, then the active organization's role sets (only while the membership exists), then the session's pre_token hook claims. A later source wins per name. Among roles of one kind, sets apply in role-name order (Brandon decided this on 2026-11-07, recorded in ADR 0005 E6), so the result does not depend on grant order, which admins cannot see. Reserved names are filtered again. Recovery tokens carry none.
  • One token budget (Brandon chose this on 2026-11-07). Permission claims (permissions, org_roles, org_permissions) and the merged custom claims share AUTHORIZATION_CLAIMS_BUDGET_BYTES (1 KiB, replacing PERMISSION_CLAIMS_BUDGET_BYTES). If both fit, the token carries both. Otherwise the permission claims give way first, replaced with permissions_omitted: true, because a guard can read GET /users/me/permissions and custom claims have no fallback. Only when the custom claims do not fit even beside the marker are they all dropped, and the mint writes custom_claims_omitted (bytes, budgetBytes). Nothing is trimmed partially. CUSTOM_CLAIMS_BUDGET_BYTES stays the per-set write limit.
  • Clean-up. Retiring an instance role, deleting an organization role (both through deleteRoleMappings) or deleting an OpenID application deletes its set, so adding the name back cannot restore old claims.

Cookie budget

At the shared budget the worst-case adapter seamless-access cookie (five long roles, a long email) measures 3868 bytes (token 2311 bytes) when the budget is split between permissions and custom claims, and 3852 bytes when it is all permissions, under the 4096 limit. Separate budgets measured 5670 bytes, which is why the budget is now shared.

Contract changes

  • New admin routes and audit types above.
  • Access tokens can carry custom claims from roles.
  • custom_claims_omitted is categorized as a token event.
  • A token with custom claims can now carry permissions_omitted at a smaller permission set than before, since the 1 KiB is shared (ADR 0004 P4 and docs/api-contract.md updated).

Deviations from ADR 0005

Recorded in the ADR: the shared budget and its order (E6, chosen by Brandon on 2026-11-07, replacing a separate custom claims budget), role-name order between roles of one kind (E6, decided 2026-11-07), and under "Found while building" writes require an existing source with clean-up on removal. It also notes that z.json() overflowed the OpenAPI generator until 0.40.0 bounded the claim value schema, so the route schemas no longer wrap the shared ones.

Checks

On head e32e823, with @seamless-auth/types 0.40.0 from npm:

  • The pre-commit hook ran lint, format, typecheck, coverage and build: 190 files, 3320 passed, 1 todo. Statements 99.06%, branches 95.69%, functions 99.36%, lines 99.25%.
  • npm run generate:api runs on the shared claim schemas ("Generated openapi.json, src/generated/api.ts and adapter-manifest.json from 148 paths."), and the document now spells out a claim value instead of any JSON. Committed; no further diff.
  • Postgres project (TEST_DB=postgres, local container) on 108a784 with 0.39.0: 16 files, 267 passed, including tests/postgres/customClaims.spec.ts (merge order on the real schema and loss on leaving the organization, an overflow that drops the custom claims writes the audit row, sets deleted with the role and the client, the kind check constraint). Not rerun on this head; the change touches no database code.
  • Unit tests cover each branch of the budget rule: exactly at the budget, one byte over (permissions omitted, custom kept), custom exactly at the budget beside the marker, and both omitted. GET /users/me/permissions still returns the full set when the token omitted it. Role-name order is pinned for two instance roles and two organization roles setting the same claim, in both grant orders.
  • Each rule was confirmed by mutation: with it removed, its test fails (reserved filtering, source kind separation, role-name order, custom claims counted in the shared budget, permissions giving way first, custom claims dropped whole, the omission audit, recovery tokens, source must exist).
  • Not run: the e2e suite.

Closes #417.

DELETE /users/credentials removed a passkey without writing an audit
event, so a change to what an account can sign in with left no trace.
It now writes credentials_deleted naming the passkey.
Adds webhook endpoints and deliveries behind an audited admin API with
step-up on every write. Payloads use the Standard Webhooks signature
scheme, are built from committed audit rows past a cursor, and are
retried eight times over just under 42 hours before they are dead
lettered. An endpoint that answers 410 or fails for three days is
disabled.

Every callout to an adopter URL goes through one transport that checks
the address it connects to, never reaches link-local, multicast or the
API itself, never follows redirects, and in production refuses private
addresses unless CALLOUT_PRIVATE_HOSTS lists the host. Signing secrets
are encrypted under CALLOUT_SECRET_ENCRYPTION_KEY.

Writes session_created once per completed sign-in, which user.signed_in
is built from, since login_success precedes any factor.

Records the design in ADR 0005. Closes #418.
Adds the two login hooks from ADR 0005 (E5), signed with the Standard
Webhooks scheme like webhooks and reached through the shared callout
transport.

- post_login runs once the factor is proven and before the session
  exists, and may deny. pre_token runs for a new session and on
  organization switch, and may add claims or deny.
- pre_token claims are kept on the session (sessions.hookClaims), copied
  on refresh and to an exchanged session, so neither calls a hook. They
  are cleared when the user leaves the organization they were decided
  for.
- One attempt with the hook's timeout. A failure follows the hook's
  failure mode. A deny answers 403 login_denied with the hook's
  userMessage; a closed failure answers 503 login_hook_unavailable.
- Reserved claim names are stripped and audited, and filtered again at
  mint. Break-glass administrators and recovery sessions never call a
  hook.
- Admin routes under /admin/login-hooks/:trigger, with step-up and an
  audit event on every write, and an invocation log pruned after 30 days.

Closes #419.
Adds custom claims from ADR 0005 (E6).

- custom_claims holds one set per instance role, organization role or
  OpenID Provider client, behind /admin/custom-claims with step-up and
  an audit event (names only) on every write. Writes are validated with
  the shared schema and the source must exist.
- mintAccessToken merges instance role sets, then organization role
  sets while the membership exists, then the session's pre_token hook
  claims, a later source winning per name, and filters reserved names
  again. Recovery tokens carry none.
- When the sources together exceed 1 KiB the token carries no custom
  claims and the mint writes custom_claims_omitted. Nothing is trimmed.
- Retiring a role or deleting a client deletes its set.
- The cookie budget test now pins that a token at both budgets
  overflows the adapter cookie, which is an open decision.

Closes #417.
…laims

Separate 1 KiB budgets for permission claims and custom claims made the
server adapter's session cookie 5670 bytes at worst, over the 4096
limit. They now share AUTHORIZATION_CLAIMS_BUDGET_BYTES (1 KiB), which
replaces PERMISSION_CLAIMS_BUDGET_BYTES. CUSTOM_CLAIMS_BUDGET_BYTES stays
the per-set write limit.

- If the permission claims and the merged custom claims fit together,
  the token carries both.
- Otherwise the permission claims give way first, replaced with
  permissions_omitted, since a guard can read GET /users/me/permissions
  and custom claims have no fallback.
- Only when the custom claims do not fit beside the marker are they all
  dropped, and the mint writes custom_claims_omitted.
- Nothing is trimmed partially. Recovery tokens carry neither.

The worst-case adapter cookie at the shared budget measures 3868 bytes.
ADR 0004 P4, ADR 0005 E6 and the API contract describe the rule.
Bccorb added 12 commits November 7, 2026 21:12
Among roles of one kind, custom claim sets apply in role-name order. It
is deterministic and does not depend on grant order, which an
administrator cannot see. ADR 0005 E6 records the decision, and a test
now pins the order for organization roles as well as instance roles.
A disabled endpoint was given no deliveries, so events that happened
while it was off were never sent, including after the API disabled it
for a 410 or three days of failures. Its deliveries are now queued on
hold, and turning it back on releases them, back as far as the 30 days
deliveries are kept. The release uses the database clock, which the due
check compares against.
@Bccorb
Bccorb changed the base branch from feat/login-hooks to main November 8, 2026 03:41
@Bccorb
Bccorb marked this pull request as ready for review November 8, 2026 03:52
@Bccorb
Bccorb merged commit 48bafad into main Nov 8, 2026
9 of 17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: custom claims

1 participant