Lays the platform-wide M7.1 multi-tenancy infrastructure on top of the existing Keycloak JWT signature validation.
JWT claims extraction.Claims struct now decodes tenant_id, tenant_slug, org_roles, products, plan, tenant_status, plus sub / name / preferred_username. require_jwt_auth builds a compliance_core::TenantContext from them and attaches it to request extensions. Missing tenant_id -> 401; missing tenant_status -> defaults to Trial with a warn-log.
TenantContext type in compliance-core. Single source of truth, re-exported alongside OrgRole and TenantStatus. The OrgRole enum is forward-compatible (#[serde(other)] Unknown variant) so a new realm role doesn't 401 the world.
TenantCtx Axum extractor. Handlers consume the context via TenantCtx(ctx): TenantCtx. Returns 401 if the upstream middleware didn't insert one.
require_tenant_status middleware. Enforces §5c of PLATFORM_ARCHITECTURE.md: Active/Trial/Demo pass, Frozen returns 402 on non-GET/HEAD/OPTIONS, Archived returns 410 on every method.
compliance_core::db::tenant_filter helper. Returns doc! { "tenant_id": ... }. tenant_filter_merge combines with extra conditions and refuses to let the caller override the tenant key.
Per-collection wiring (adding tenant_id to each model + threading the filter through every find / update_* / delete_* call across the 38 query call-sites) lands in a follow-up PR.
Ship Keycloak protocol mapper for tenant_status claim (separate ticket)
Per-collection scoping migration (M7.2)
## Summary
Lays the platform-wide M7.1 multi-tenancy infrastructure on top of the existing Keycloak JWT signature validation.
- **JWT claims extraction.** `Claims` struct now decodes `tenant_id`, `tenant_slug`, `org_roles`, `products`, `plan`, `tenant_status`, plus `sub` / `name` / `preferred_username`. `require_jwt_auth` builds a `compliance_core::TenantContext` from them and attaches it to request extensions. Missing `tenant_id` -> 401; missing `tenant_status` -> defaults to Trial with a warn-log.
- **`TenantContext` type in compliance-core.** Single source of truth, re-exported alongside `OrgRole` and `TenantStatus`. The `OrgRole` enum is forward-compatible (`#[serde(other)] Unknown` variant) so a new realm role doesn't 401 the world.
- **`TenantCtx` Axum extractor.** Handlers consume the context via `TenantCtx(ctx): TenantCtx`. Returns 401 if the upstream middleware didn't insert one.
- **`require_tenant_status` middleware.** Enforces §5c of PLATFORM_ARCHITECTURE.md: Active/Trial/Demo pass, Frozen returns 402 on non-GET/HEAD/OPTIONS, Archived returns 410 on every method.
- **`compliance_core::db::tenant_filter` helper.** Returns `doc! { "tenant_id": ... }`. `tenant_filter_merge` combines with extra conditions and refuses to let the caller override the tenant key.
Per-collection wiring (adding `tenant_id` to each model + threading the filter through every `find` / `update_*` / `delete_*` call across the 38 query call-sites) lands in a follow-up PR.
## Test plan
- [x] `cargo fmt --all -- --check` clean
- [x] `cargo clippy --workspace --exclude compliance-dashboard -- -D warnings` clean (matches baseline)
- [x] `cargo test -p compliance-core --lib` — 7 tests pass (4 tenant, 3 db)
- [x] `cargo test -p compliance-agent --lib api::` — 32 tests pass, including 6 new auth_middleware tests and 2 tenant_ctx tests
- [x] `cargo test -p compliance-agent --test tenant_status_middleware` — 6 integration tests pass (active / trial / demo / frozen / archived / no-context paths)
- [ ] Ship Keycloak protocol mapper for `tenant_status` claim (separate ticket)
- [ ] Per-collection scoping migration (M7.2)
Lays the platform-wide multi-tenancy infrastructure on top of the
existing Keycloak signature validation. JWTs now carry tenant_id,
tenant_slug, org_roles, products, plan, and tenant_status; the
middleware decodes them into a TenantContext and attaches it to the
request extensions. A TenantCtx Axum extractor exposes the context to
handlers, and a tenant_status middleware enforces the §5c lifecycle
(frozen tenants are 402 on writes; archived tenants are 410 on every
method).
A db::tenant_filter helper in compliance-core gives every future
collection a single grep-able pattern for tenant-scoped queries.
Per-collection wiring (adding tenant_id to each model + threading the
filter through every find/update/delete call) lands in a follow-up.
Tests: 6 inline unit tests for claims→context mapping, 2 for the
extractor, 6 integration tests for status middleware, 3 for db
filter.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Axum applies layers outermost-first. With the previous ordering
(`Extension(jwks_state)` first, `require_jwt_auth` last), the JWT
middleware ran before the Extension layer attached `JwksState` to
the request, so `request.extensions().get::<JwksState>()` always
returned None and the middleware silently passed through every
request as if Keycloak weren't configured.
Verified end-to-end against the local CERTifAI Keycloak realm:
- no token / bad token -> 401
- active / trial -> 200 read, write reaches handler
- frozen -> 200 read, 402 on writes
- archived -> 410 on every method
The bug was invisible to the unit + integration tests because they
construct the layer stack manually; only the live wiring exhibited it.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Superseded by #85 (squash-merged 2026-06-17). #85 ships the same M7.1 contract on top of the lifted compliance-core auth stack (post-#83 + #84), with the live-KC smoke harness green (15/15).
Superseded by #85 (squash-merged 2026-06-17). #85 ships the same M7.1 contract on top of the lifted compliance-core auth stack (post-#83 + #84), with the live-KC smoke harness green (15/15).
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.
Summary
Lays the platform-wide M7.1 multi-tenancy infrastructure on top of the existing Keycloak JWT signature validation.
Claimsstruct now decodestenant_id,tenant_slug,org_roles,products,plan,tenant_status, plussub/name/preferred_username.require_jwt_authbuilds acompliance_core::TenantContextfrom them and attaches it to request extensions. Missingtenant_id-> 401; missingtenant_status-> defaults to Trial with a warn-log.TenantContexttype in compliance-core. Single source of truth, re-exported alongsideOrgRoleandTenantStatus. TheOrgRoleenum is forward-compatible (#[serde(other)] Unknownvariant) so a new realm role doesn't 401 the world.TenantCtxAxum extractor. Handlers consume the context viaTenantCtx(ctx): TenantCtx. Returns 401 if the upstream middleware didn't insert one.require_tenant_statusmiddleware. Enforces §5c of PLATFORM_ARCHITECTURE.md: Active/Trial/Demo pass, Frozen returns 402 on non-GET/HEAD/OPTIONS, Archived returns 410 on every method.compliance_core::db::tenant_filterhelper. Returnsdoc! { "tenant_id": ... }.tenant_filter_mergecombines with extra conditions and refuses to let the caller override the tenant key.Per-collection wiring (adding
tenant_idto each model + threading the filter through everyfind/update_*/delete_*call across the 38 query call-sites) lands in a follow-up PR.Test plan
cargo fmt --all -- --checkcleancargo clippy --workspace --exclude compliance-dashboard -- -D warningsclean (matches baseline)cargo test -p compliance-core --lib— 7 tests pass (4 tenant, 3 db)cargo test -p compliance-agent --lib api::— 32 tests pass, including 6 new auth_middleware tests and 2 tenant_ctx testscargo test -p compliance-agent --test tenant_status_middleware— 6 integration tests pass (active / trial / demo / frozen / archived / no-context paths)tenant_statusclaim (separate ticket)Superseded by #85 (squash-merged 2026-06-17). #85 ships the same M7.1 contract on top of the lifted compliance-core auth stack (post-#83 + #84), with the live-KC smoke harness green (15/15).
Pull request closed