authz
Role: the single authorization decision point. It answers Check, CheckMany, LookupResources,
EffectivePermissions, SubjectRoles, LookupSubjects and AuthenticateAPIKey over internal RPC, and exposes a REST API
for roles, grants, assignments, API keys, login history and organization / site settings. MongoDB is the source
of truth; Permify evaluates relationship checks from tuples that authz derives from MongoDB. See the
authorization model.
Limits: it never sees resource data. Callers pass the target ARN and its parents (site, project) and authz turns them into contextual tuples, so other services never write tuples. It does not authenticate users (the gateway does, see identity); it only validates raw API keys for the gateway.
Decisions
| Step | Behaviour |
|---|---|
| Unknown permission | REASON_UNKNOWN_PERMISSION (not in pkg/catalog) |
| Service principal | kind=service with an ID is always allowed (Lookup returns all, EffectivePermissions the whole catalogue) |
| Target resolution | ARN + parents → Permify entity, permission and contextual tuples; an unresolvable target is denied |
| Holders | user → the user; API key → the key, plus its owner when user-owned (both must be allowed: key grants ∩ owner permissions). Revoked, expired or unknown keys hold nothing |
| Default role | DEFAULT_ROLE_FOR_USERS (global role name) added as a contextual membership for every user holder; lookup cached 30 s |
| Gate | allowed but Elevated without admin mode → REASON_ADMIN_MODE_REQUIRED; Sensitive without step-up (ACR == STEP_UP_ACR and auth_time within STEP_UP_MAX_AGE) → REASON_STEP_UP_REQUIRED |
LookupResources and EffectivePermissions are computed from the MongoDB grants (role grants, default
role, key grants), not from Permify. For API keys the key scope is intersected with the owner scope.
LookupResources returns an empty scope when the gate would refuse the permission. SubjectRoles lists the
global and project roles of a user (assignments, Keycloak-synced roles, default role) or of an API key (its own
assignments only); events uses it for role invitations. LookupSubjects (service principals only) lists the users
whose roles grant a permission on a resource, from the same grants; notifications uses it to resolve recipients.
The REST handlers use the same authorizer in process (no RPC round trip).
Permify synchronisation
- At startup: create the tenant (10 retries, 1 s apart), write the schema generated from the catalogue, seed, then reconcile.
- Incremental writes failing are logged and schedule a reconciliation (MongoDB already holds the change).
- Reconciliation reads every tuple of every schema entity type and applies the diff with the desired state (all non-deleted roles' grants, active assignments, active keys' grants). A mutex serialises it with writes.
- Checks pass the latest snap token and the schema version (depth 20).
Background jobs
- Seed (idempotent, never overwrites): organization settings, sites
parisandstrasbourg, system rolesBureau,Trésorier,Coordinateur,Lab Manager,Membre,Invité(French data).Bureaumaps to Keycloak roleintranet-admin,Trésoriertointranet-treasurer. In development the dev admin user (00000000-0000-4000-8000-000000000001) is assignedBureau. - Keycloak sync (only when
KEYCLOAK_URLis set), everyKEYCLOAK_SYNC_INTERVAL: for each role with akeycloak_role, assigns missing members and removes stalekeycloak-sourced assignments (manual ones are kept). WhenKEYCLOAK_LOGIN_EVENTSis on, importsLOGIN/LOGIN_ERRORadmin events since a watermark (first run: last 24 h). The service account needsview-usersandview-events.
Dependencies
- Services: none called. Callers: gateway and every service through
authzclient(see internal RPC). - Infrastructure: MongoDB (replica set, transactions), Permify (gRPC), Kafka, Keycloak admin API (optional).
- Libraries:
catalog,arn,principal,eventstore,outbox,audit,cloudevent,kafkax,mongox,httpx,problem,ids,config,logging,telemetry(see libraries).
Inputs and outputs
| Kind | Name | Notes |
|---|---|---|
| HTTP | GET /v1/public/settings | anonymous; organization and sites, contact email as image path and spoken FR/EN labels |
| HTTP | GET /v1/public/settings/contact-email.svg | anonymous; configured contact address as SVG outlines, cached five minutes |
| HTTP | /v1/authz/* | permissions catalogue, me/*, roles, project roles, role directory, API keys, logins, assignments, settings: API reference |
| RPC in | authz.v1.AuthzService/* | same port as REST, see internal RPC |
| Kafka out | authz.role.v1 | created, updated, grants_replaced, deleted (ID, name, scope, project, Keycloak role, grants) |
| Kafka out | authz.assignment.v1 | created, removed (ID, role, subject kind / ID, source); consumed by members (history) |
| Kafka out | authz.apikey.v1 | created, rotated, revoked (ID, key_id, prefix, owner, grants, CIDRs, expiry; never the hash); consumed by the gateway |
| Kafka out | audit.records.v1 | authz.role.create/update/delete/assign/unassign, authz.apikey.create/rotate/revoke, settings.organization.update, settings.site.manage, settings.site.delete |
| Kafka in | gateway.session.v1 | created, failed → login history (source gateway, user agent ≤ 200 chars) |
| Kafka in | members.member.v1 | erased → revoke the user's API keys, remove their assignments, delete their login history |
Events and audit records go through the transactional outbox (see Kafka and
CQRS). The consumer group is authz; malformed events are logged and skipped.
Permissions checked by the REST API
| Permission | Used for |
|---|---|
authz.role.read / create / update / delete / assign | global roles, grants, assignments |
projects.project.manage_roles | every /projects/{id}/roles route |
projects.apikey.manage | /projects/{id}/api-keys |
authz.apikey.read, authz.apikey.revoke (elevated) | all keys, admin revocation |
authz.login.read (sensitive) | /v1/authz/logins |
settings.organization.update, settings.site.manage, settings.site.delete | settings writes |
/v1/authz/me/* routes need an authenticated user principal only.
GET /v1/authz/directory/roles?scope=global|project&project_id= lists role cards (id, name, scope,
project_id; no grants, no assignments) for pickers such as event invitations. Global roles need an
authenticated user only (API keys and services need authz.role.read). Project roles are returned only when
the caller holds projects.project.read on the project or is assigned one of its roles; otherwise 403.
Data (MongoDB, database authz)
| Collection | Key fields | Indexes |
|---|---|---|
roles | name, description, scope (global/project), project_id, keycloak_role, system, grants[] (permission, resource pattern), version, deleted_at (soft delete) | (name, project_id), (scope, project_id, _id), keycloak_role |
assignments | role_id, subject_kind (user/api_key), subject_id, source (manual/keycloak), active, removed_at | unique (role_id, subject_kind, subject_id) where active; (subject_kind, subject_id, active); (role_id, active, _id) |
api_keys | prefix, hash (HMAC-SHA256 with the pepper), owner_kind, owner_id, name, grants[], allowed_cidrs, expires_at, last_used_at (updated at most once a minute), revoked_at | unique prefix; (owner_kind, owner_id, _id) |
logins | event_id, user_id, time, ip, user_agent, outcome, reason, source (gateway/keycloak) | unique event_id; (user_id, _id) |
settings | organization document, watermark:keycloak_events | |
sites | _id, name, address | |
events | event store (roles, assignments, keys) | unique (aggregate_id, version); (type, _id) |
outbox | pending Kafka messages |
API keys have the form atl_<prefix>_<secret> (8-character prefix, 32 random bytes in base32); only the hash
and the prefix are stored, the raw key is returned once at creation or rotation.
Owner kinds: user, project and device. Device keys are issued, rotated and revoked only by the devices
service principal through /internal/v1/devices/{id}/api-keys (never routed by the gateway) and cannot hold grants:
they authenticate device agents, which the devices service authorizes by key owner.
Permify schema
Generated from the catalogue at startup (internal/schema):
| Entity | Relations / permissions |
|---|---|
user, api_key | none |
role, project_role | member @user @api_key |
organization (ID main) | per permission: <relation>_grant, permission = grant |
site | organization; permission = grant or organization.<relation> |
project | organization, site; project-defined permissions = grant or site or organization |
<service>_<type> | organization, site, project (project resource types); per action: <action>_grant, permission = grant or site or organization (or project for project permissions) |
Grant subjects are role#member and api_key, plus project_role#member for project-scoped permissions. The
catalogue types settings_organization, settings_site and projects_project map to the structural
organization, site and project entities.
Configuration
| Variable | Default | Description |
|---|---|---|
HTTP_ADDR | :8081 | Listen address (REST and ConnectRPC) |
PRINCIPAL_PUBLIC_KEY | required | Base64 Ed25519 key verifying principal tokens |
MONGO_URI, MONGO_DATABASE | mongodb://localhost:27017/?directConnection=true, authz | Source of truth |
KAFKA_BROKERS | localhost:19092 | Events, audit records, consumed topics |
PERMIFY_ADDR | localhost:3478 | Permify gRPC endpoint |
PERMIFY_TENANT | t1 | Permify tenant (created if missing) |
PERMIFY_API_KEY | empty | Bearer token sent to Permify |
AUTHZ_APIKEY_PEPPER | dev pepper outside production | HMAC pepper for API key hashes; required in production |
DEFAULT_ROLE_FOR_USERS | empty | Name of a global role implicitly held by every user |
STEP_UP_ACR, STEP_UP_MAX_AGE | passkey, 15m | Step-up requirement for sensitive permissions |
AUTHZ_RECONCILE_INTERVAL | 5m | Full Permify reconciliation period |
KEYCLOAK_URL | empty | Enables the Keycloak sync |
KEYCLOAK_REALM | atelier | Realm |
KEYCLOAK_CLIENT_ID, KEYCLOAK_CLIENT_SECRET | empty | Service account; required with KEYCLOAK_URL |
KEYCLOAK_SYNC_INTERVAL | 5m | Role sync and login import period |
KEYCLOAK_LOGIN_EVENTS | true | Import Keycloak login events |
Intervals must be positive. Plus the common APP_ENV, LOG_LEVEL, METRICS_ADDR, OTEL_EXPORTER_OTLP_ENDPOINT
(see environment variables).