feat(apikeys): service API keys with channel and role scope (#13) #118

Merged
amiryahaya merged 8 commits from feat/13-service-api-keys into main 2026-10-28 17:44:39 +01:00
Owner

What

Service API keys (#13): Authorization: Bearer kws_… for machine callers. A key is a service users row plus a platform-scope membership holding one existing custom role, so every guard, the audit actor and created_by work unchanged. Secret = 32 random bytes, stored only as SHA-256, shown once. Each key has a channel (website, mobile, usher, agent, tour_portal), an optional expiry and a revoked state.

  • Admin routes (platform scope, administration.manage): POST|GET /v1/admin/api-keys, DELETE /v1/admin/api-keys/{id}. Create is not replayable (same Idempotency-Key = 409 already_created) so the secret is never stored in idempotency_keys.
  • No escalation: the creator must hold every permission of the role at platform scope; org-scoped, missing and Super Admin roles are 422.
  • Revoke deletes the membership, evicts the key from the 5 s per-node cache and invalidates the authorizer.
  • Migration 0032: api_keys (audit with secret_hash and last_used_at excluded) and widens transactions.channel to include tour_portal. ADR 0027, CLAUDE.md section, OpenAPI (tag ApiKeys), live contract test.

Verification

  • make cover: 91.2% (minimum 80%), run on the final code commit.
  • make lint 0 issues, make contract ok, go test -race -count=1 ./... ok, scripts/generate-clients.sh ok.
  • Dual-pass review: Pass 1 (ponytail) no Must fix; Pass 2 two Important (upper-case id revoke did not evict the key; keys could use /v1/me buying routes), both fixed in one wave; scoped re-review APPROVE, Ponytail Must fix none.

Rulings made without you (owner to check, all marked [verify] in ADR 0027)

  • Keys are refused on /v1/me (403): a reports-only key must not buy tickets. GET /v1/me and /v1/me/access stay open.
  • Channel set is five values; Kiosk deferred. The 0032 down fails if tour_portal sales exist, and leaves the service users behind.
  • A role with no organization (for example Organization Administrator) becomes a platform-scope grant for a key, not an escalation but wider than "admin portal only".
  • Role edits widen existing keys; the role is read just before the grant inside create.
  • A key with administration.manage can mint keys that outlive its revocation.
  • Revocation takes up to 5 s on other nodes; last_used_at is written on the request path at most once a minute per key.
  • New narrow gosec exclusion in api/.golangci.yml: G117 for internal/apikeys/handler.go only (the create response carries the secret by design).

Not verified

  • No real integration calls this yet; no load test of the key path; a flood of well-formed random kws_ tokens costs one indexed query each (per-IP rate limiting is #62).
  • Some new tests (key material, organization-scoped role, flat schemas) were written with or after the code, not red-first.

Closes nothing; #13 stays open until you close it.

🤖 Generated with Claude Code

## What Service API keys (#13): `Authorization: Bearer kws_…` for machine callers. A key is a service `users` row plus a platform-scope membership holding one existing custom role, so every guard, the audit actor and `created_by` work unchanged. Secret = 32 random bytes, stored only as SHA-256, shown once. Each key has a channel (`website`, `mobile`, `usher`, `agent`, `tour_portal`), an optional expiry and a revoked state. - Admin routes (platform scope, `administration.manage`): `POST|GET /v1/admin/api-keys`, `DELETE /v1/admin/api-keys/{id}`. Create is not replayable (same `Idempotency-Key` = 409 `already_created`) so the secret is never stored in `idempotency_keys`. - No escalation: the creator must hold every permission of the role at platform scope; org-scoped, missing and Super Admin roles are 422. - Revoke deletes the membership, evicts the key from the 5 s per-node cache and invalidates the authorizer. - Migration 0032: `api_keys` (audit with `secret_hash` and `last_used_at` excluded) and widens `transactions.channel` to include `tour_portal`. ADR 0027, CLAUDE.md section, OpenAPI (tag `ApiKeys`), live contract test. ## Verification - `make cover`: **91.2%** (minimum 80%), run on the final code commit. - `make lint` 0 issues, `make contract` ok, `go test -race -count=1 ./...` ok, `scripts/generate-clients.sh` ok. - Dual-pass review: Pass 1 (ponytail) no Must fix; Pass 2 two Important (upper-case id revoke did not evict the key; keys could use `/v1/me` buying routes), both fixed in one wave; scoped re-review APPROVE, Ponytail Must fix none. ## Rulings made without you (owner to check, all marked [verify] in ADR 0027) - Keys are **refused on `/v1/me`** (403): a reports-only key must not buy tickets. `GET /v1/me` and `/v1/me/access` stay open. - Channel set is five values; `Kiosk` deferred. The 0032 `down` fails if `tour_portal` sales exist, and leaves the service users behind. - A role with no organization (for example Organization Administrator) becomes a platform-scope grant for a key, not an escalation but wider than "admin portal only". - Role edits widen existing keys; the role is read just before the grant inside create. - A key with `administration.manage` can mint keys that outlive its revocation. - Revocation takes up to 5 s on other nodes; `last_used_at` is written on the request path at most once a minute per key. - New narrow gosec exclusion in `api/.golangci.yml`: G117 for `internal/apikeys/handler.go` only (the create response carries the secret by design). ## Not verified - No real integration calls this yet; no load test of the key path; a flood of well-formed random `kws_` tokens costs one indexed query each (per-IP rate limiting is #62). - Some new tests (key material, organization-scoped role, flat schemas) were written with or after the code, not red-first. Closes nothing; #13 stays open until you close it. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
POST/GET /v1/admin/api-keys and DELETE /v1/admin/api-keys/{id}, platform
scope only with admin_portal.administration.manage. Create calls
idempotency Runner.Run directly: the record keeps the key without its
secret, the 201 shows the secret once and a repeated Idempotency-Key is
409 already_created. The creator must hold every permission of the role;
organization, unknown and Super Admin roles are 422. Revoke evicts the
key from this node's verifier and invalidates the authorizer.

Store.Create now runs inside the caller's transaction; idempotency
exposes WriteError (Handle's error mapping). The verifier no longer
re-caches a key evicted during an in-flight lookup, and Touch ignores a
client disconnect. Report and spec channel filters accept tour_portal.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Also: flat ApiKey/ApiKeyCreated schemas with additionalProperties false, a store test for organization-scoped roles, a race-test comment.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
docs(apikeys): note platform-scope business roles for keys (#13)
All checks were successful
ci / changes (pull_request) Successful in 25s
ci / clients (pull_request) Successful in 1m33s
ci / api (pull_request) Successful in 7m49s
ci / ci-gate (pull_request) Successful in 2s
3b91ecf6fd
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
primatekuntech/kws!118
No description provided.