feat(keys): API keys — mint/list/rotate/revoke (task 8.2, SDK-03) #100

Merged
amiryahaya merged 1 commit from feat/1.5-api-keys into main 2026-11-07 15:40:44 +01:00
Owner

First PR of the M1.5 M2M early slice (dev plan v1.6 §5.5).

SDK-03 — API keys

  • ApiKey entity (api_keys + migration): tenant-scoped, sdid_-prefixed secret with only its SHA-256 stored, prefix for identification, space-separated Drive-side scopes (SDK-04 set, validated at mint), optional expiry, last-used tracking, immediate revocation
  • Endpoints (Owner/Admin, tenant-scoped):
    • POST /api/keys/api-keys — mint, 201 + secret shown once
    • GET /api/keys/api-keys — prefix + metadata; secret/hash never in payload
    • POST /api/keys/api-keys/{id}/rotate — new secret once; old valid 24 h grace
    • DELETE /api/keys/api-keys/{id} — immediate revoke; grace hashes cleared
  • ApiKeyAuthentication.ValidateAsync — acceptance core (revoked/expired/grace rules + LastUsedAt stamp) for the 8.3 /api/m2m/* auth middleware
  • Authz-matrix rows added (the SEC-03 harness caught the undeclared routes immediately)

Verification

TDD: 8 tests first (mint-once/hash-only, member-403, unknown-scope-400, list-no-secrets, rotate-grace, revoke-immediate, expiry-reject). Suite 888 / 0 / 39 (Release ×2) · gate 85.12% ≥ 80

First PR of the **M1.5 M2M early slice** (dev plan v1.6 §5.5). ## SDK-03 — API keys - `ApiKey` entity (api_keys + migration): tenant-scoped, `sdid_`-prefixed secret with **only its SHA-256 stored**, prefix for identification, space-separated Drive-side scopes (SDK-04 set, validated at mint), optional expiry, last-used tracking, immediate revocation - Endpoints (Owner/Admin, tenant-scoped): - `POST /api/keys/api-keys` — mint, 201 + **secret shown once** - `GET /api/keys/api-keys` — prefix + metadata; secret/hash never in payload - `POST /api/keys/api-keys/{id}/rotate` — new secret once; **old valid 24 h grace** - `DELETE /api/keys/api-keys/{id}` — immediate revoke; grace hashes cleared - `ApiKeyAuthentication.ValidateAsync` — acceptance core (revoked/expired/grace rules + LastUsedAt stamp) for the 8.3 `/api/m2m/*` auth middleware - Authz-matrix rows added (the SEC-03 harness caught the undeclared routes immediately) ## Verification TDD: 8 tests first (mint-once/hash-only, member-403, unknown-scope-400, list-no-secrets, rotate-grace, revoke-immediate, expiry-reject). Suite **888 / 0 / 39** (Release ×2) · gate **85.12% ≥ 80**
feat(keys): API keys — mint/list/rotate/revoke for machine principals (task 8.2, SDK-03, first M1.5 slice PR)
All checks were successful
Security CI / security-scan (pull_request) Successful in 3m6s
API CI / build-and-test (pull_request) Successful in 3m43s
647f035b89
ApiKey entity (api_keys): tenant-scoped, name, KeyPrefix (identification),
SecretHash only (SHA-256 hex — raw secret shown exactly once at
mint/rotate), PreviousSecretHash + PreviousExpiresAt (24 h rotation grace),
space-separated Drive-side scopes (SDK-04 known set validated at mint),
optional ExpiresAt, LastUsedAt (set on acceptance), RevokedAt (immediate).

Endpoints under /api/keys/api-keys (Owner/Admin only, tenant-scoped):
- POST /  mint — 201 + secret ONCE; validates name ≤200, scopes against
  the SDK-04 known set, future expiry
- GET /   list — prefix + metadata; raw secret and hash never in payload
- POST /{id}/rotate — new secret ONCE; old hash retained with 24 h grace
- DELETE /{id} — immediate revoke; grace hashes cleared (no grace after
  revoke); revoked keys vanish from the default list

ApiKeyAuthentication.ValidateAsync — SDK-03 acceptance core (rules:
not revoked, not expired, current hash always, previous hash only within
grace; stamps LastUsedAt). Wired for the 8.3 /api/m2m/* authentication
middleware; tests exercise it directly.

Authz-matrix rows added for the four routes (RouteClass.Account —
Owner/Admin enforced in-handler). Expiry-rejection test backdates via DB
(the endpoint correctly refuses past expiry at mint).

TDD: 8 tests first — RED (routes 404 pre-registration, bounds missing).
Suite 888 / 0 failed / 39 skipped (Release ×2); gate 85.12% ≥ 80.
Sign in to join this conversation.
No description provided.