Implements step 5 of tenant-registry ADR 0001 (#70 there): the customer API accepts portal-issued API keys (Authorization: Bearer bp_…) next to Keycloak JWTs. Spec: research/2026-10-13_pruefwerk-api-key-acceptance-spec.md (decision D4).
How it works
Middleware: a new api_key_gate::require_api_key runs just outside the JWT middleware.
A bp_ bearer is verified with the registry (POST /v1/internal/api-keys/verify, using the existing pruefwerk-svc token) and becomes a TenantContext carrying ApiKeyAccess.
Every other bearer goes on to the JWT path unchanged.
Health, admin, Werkbank and platform paths are skipped, so runner keys still reach require_runner_token untouched.
Cache:
Valid and invalid verdicts are both cached for 60 s, keyed by sha256(key); the key itself is never stored, cached or logged.
At most 10,000 entries: when full, expired entries are dropped first; if none have expired, the new verdict is answered but not cached.
The registry being down with no fresh entry gives 503. An expired entry counts as no entry, so an outage never extends a revoked key's life.
The Werkbank runner path shares the same cached verify.
Principal:
Taken only from the verify answer: tenant, scopes, key_id, kind, holder, tenant_status and legacy.
The support tenant header is never read for a key, and a key holds no org role (product_access() is None, so any handler-level role check refuses it).
user_id is apikey:<key_id> and the display name is the holder, so the finding history attributes changes to the key.
Scopes (role_gate::KEY_SCOPES):
The table matches on method plus route template, exactly as the spec lists it.
Every other route answers 403 {"error":"scope_missing","required":…}, even for a key holding every scope.
In addition, every key write is audited like support writes, as pruefwerk.api_key_write, with X-BP-Actor recorded as metadata.on_behalf_of. That header is cleaned and capped at 120 characters and never used to authorize.
This is the stricter reading of spec §7; say so if attribution alone was meant.
Guide page:docs/guide/api-keys.md (in German).
Tests
Router tests (role_gate/router_tests.rs):
every table route exists in the real router and needs its own scope;
write implies read;
nothing outside the table is reachable with a key;
legacy keys can read but not write or run.
Registry (registry.rs):
valid and invalid verdicts are reused within the TTL;
an unreachable registry is answered from a warm cache and fails on a cold or expired one;
the cache holds digests only, and its cap holds.
Middleware (api_key_gate.rs): the context a key gets, legacy handling, a registry answer from before #70, and the X-BP-Actor cleanup.
tests/api_key_access.rs (needs Mongo, as CI provides; run locally against a temporary mongo:7), through the server's middleware order:
the tenant comes only from verify;
no second verify call and no entitlement call within the TTL;
unknown key → 401, JWT path untouched, cold cache with the registry down → 503;
restricted tenant → read 200, write 402;
the write audit entry has the expected shape.
Not done here
End-to-end on stage waits for tenant-registry #70 to be merged and deployed.
Prod promotion needs your go, timed around the K-00001 pilot.
No new endpoints; the MCP server does not accept keys.
Implements step 5 of tenant-registry ADR 0001 (#70 there): the customer API accepts portal-issued API keys (`Authorization: Bearer bp_…`) next to Keycloak JWTs. Spec: `research/2026-10-13_pruefwerk-api-key-acceptance-spec.md` (decision D4).
## How it works
- **Middleware:** a new `api_key_gate::require_api_key` runs just outside the JWT middleware.
- A `bp_` bearer is verified with the registry (`POST /v1/internal/api-keys/verify`, using the existing `pruefwerk-svc` token) and becomes a `TenantContext` carrying `ApiKeyAccess`.
- Every other bearer goes on to the JWT path unchanged.
- Health, admin, Werkbank and platform paths are skipped, so runner keys still reach `require_runner_token` untouched.
- **Cache:**
- Valid and invalid verdicts are both cached for **60 s**, keyed by `sha256(key)`; the key itself is never stored, cached or logged.
- At most 10,000 entries: when full, expired entries are dropped first; if none have expired, the new verdict is answered but not cached.
- The registry being down with no fresh entry gives `503`. An expired entry counts as no entry, so an outage never extends a revoked key's life.
- The Werkbank runner path shares the same cached verify.
- **Principal:**
- Taken only from the verify answer: tenant, scopes, `key_id`, `kind`, `holder`, `tenant_status` and `legacy`.
- The support tenant header is never read for a key, and a key holds no org role (`product_access()` is `None`, so any handler-level role check refuses it).
- `user_id` is `apikey:<key_id>` and the display name is the holder, so the finding history attributes changes to the key.
- **Scopes (`role_gate::KEY_SCOPES`):**
- The table matches on method plus route template, exactly as the spec lists it.
- Every other route answers `403 {"error":"scope_missing","required":…}`, even for a key holding every scope.
- `x.write` implies `x.read`; `scans.run` implies nothing.
- **Legacy keys** (no scopes, product `pruefwerk` or empty) get only the four read scopes.
- **Entitlement:**
- `require_entitlement` skips keys, because verify already intersects the key's scopes with the tenant's entitlements and returns the ladder status.
- `restricted`/`frozen` → reads work and writes get `402`, through the existing status middleware.
- A registry answer without `tenant_status` (one from before #70) is treated as read-only.
- **Audit:**
- Key attribution everywhere (`actor_type: "apikey"`).
- In addition, every key write is audited like support writes, as `pruefwerk.api_key_write`, with `X-BP-Actor` recorded as `metadata.on_behalf_of`. That header is cleaned and capped at 120 characters and never used to authorize.
- This is the stricter reading of spec §7; say so if attribution alone was meant.
- **Guide page:** `docs/guide/api-keys.md` (in German).
## Tests
- **Router tests (`role_gate/router_tests.rs`):**
- every table route exists in the real router and needs its own scope;
- write implies read;
- nothing outside the table is reachable with a key;
- legacy keys can read but not write or run.
- **Registry (`registry.rs`):**
- valid and invalid verdicts are reused within the TTL;
- an unreachable registry is answered from a warm cache and fails on a cold or expired one;
- the cache holds digests only, and its cap holds.
- **Middleware (`api_key_gate.rs`):** the context a key gets, legacy handling, a registry answer from before #70, and the `X-BP-Actor` cleanup.
- **`tests/api_key_access.rs`** (needs Mongo, as CI provides; run locally against a temporary `mongo:7`), through the server's middleware order:
- the tenant comes only from verify;
- no second verify call and no entitlement call within the TTL;
- unknown key → 401, JWT path untouched, cold cache with the registry down → 503;
- restricted tenant → read 200, write 402;
- the write audit entry has the expected shape.
## Not done here
- End-to-end on stage waits for tenant-registry #70 to be merged and deployed.
- Prod promotion needs your go, timed around the K-00001 pilot.
- No new endpoints; the MCP server does not accept keys.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
https://claude.ai/code/session_01EYKjxacrr6K88DruUmP48p
Step 5 of tenant-registry ADR 0001. A `Bearer bp_…` token is verified
with the registry (pruefwerk-svc) and becomes a tenant context carrying
the key; every other bearer goes through the JWT path unchanged.
- Verdicts, valid and invalid, are cached for 60 s by the key's SHA-256;
an unreachable registry with no fresh verdict means 503. The Werkbank
runner shares the same cached verify.
- Tenant, scopes and ladder status come only from the verify answer; the
support header is never read for a key, and a key holds no org role.
- role_gate gets a method + route -> scope table; every other route
answers 403 scope_missing. x.write implies x.read. Legacy keys (no
scopes) get the read scopes only.
- The entitlement check is skipped for keys (verify already intersects
scopes with entitlements). Key writes are audited like support writes,
as apikey:<key_id> with the holder and X-BP-Actor as on_behalf_of.
- Guide page docs/guide/api-keys.md.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EYKjxacrr6K88DruUmP48p
Callers choose the keys, so cached invalid verdicts must not grow without
end: at the cap expired verdicts are dropped first, and if none are, the
new verdict is answered but not kept.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EYKjxacrr6K88DruUmP48p
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
Implements step 5 of tenant-registry ADR 0001 (#70 there): the customer API accepts portal-issued API keys (
Authorization: Bearer bp_…) next to Keycloak JWTs. Spec:research/2026-10-13_pruefwerk-api-key-acceptance-spec.md(decision D4).How it works
api_key_gate::require_api_keyruns just outside the JWT middleware.bp_bearer is verified with the registry (POST /v1/internal/api-keys/verify, using the existingpruefwerk-svctoken) and becomes aTenantContextcarryingApiKeyAccess.require_runner_tokenuntouched.sha256(key); the key itself is never stored, cached or logged.503. An expired entry counts as no entry, so an outage never extends a revoked key's life.key_id,kind,holder,tenant_statusandlegacy.product_access()isNone, so any handler-level role check refuses it).user_idisapikey:<key_id>and the display name is the holder, so the finding history attributes changes to the key.role_gate::KEY_SCOPES):403 {"error":"scope_missing","required":…}, even for a key holding every scope.x.writeimpliesx.read;scans.runimplies nothing.pruefwerkor empty) get only the four read scopes.require_entitlementskips keys, because verify already intersects the key's scopes with the tenant's entitlements and returns the ladder status.restricted/frozen→ reads work and writes get402, through the existing status middleware.tenant_status(one from before #70) is treated as read-only.actor_type: "apikey").pruefwerk.api_key_write, withX-BP-Actorrecorded asmetadata.on_behalf_of. That header is cleaned and capped at 120 characters and never used to authorize.docs/guide/api-keys.md(in German).Tests
role_gate/router_tests.rs):registry.rs):api_key_gate.rs): the context a key gets, legacy handling, a registry answer from before #70, and theX-BP-Actorcleanup.tests/api_key_access.rs(needs Mongo, as CI provides; run locally against a temporarymongo:7), through the server's middleware order:Not done here
🤖 Generated with Claude Code
https://claude.ai/code/session_01EYKjxacrr6K88DruUmP48p
f7f56ab1ccto2254e1c8ff