Directory lifecycle sync
Suspend Tokamak access, API keys included, when an account is disabled or deleted in Keycloak, and restore it when the account comes back.
When someone leaves, the directory is where their account is switched off. Directory lifecycle sync makes Tokamak follow it. Auth regularly reads Keycloak for every account it issued:
- Disabled or deleted in Keycloak: the Tokamak account is suspended. API keys, CLI keys and sessions are refused from the next request, and refresh tokens are revoked.
- Enabled again: the suspension is lifted and the same API keys work again, without being re-issued.
Keycloak federates LDAP, Active Directory, Google and others, so disabling an account in any directory Keycloak mirrors works the same way.
Turn it on
Set TOKAMAK_AUTH_IDP_SYNC_ENABLED=true on tokamak-auth. It needs the Keycloak settings, KEYCLOAK_ISSUER and the service client with view-users, which password sign-in already uses. Auth refuses to start if the sync is on and any of these is missing. The first run is 20 seconds after start, then every TOKAMAK_AUTH_IDP_SYNC_INTERVAL (default 5 minutes). Access ends within about one interval of the change in the directory.

On a local test deployment with a 30-second interval, an API key of the account above behaved like this:
05:24:22 [email protected] disabled in Keycloak
05:24:23 API key GET /v1/models -> 200
05:24:29 API key GET /v1/models -> 401 # next sync run: suspended
...
05:26:06 [email protected] enabled in Keycloak
05:26:25 API key GET /v1/models -> 401
05:26:31 API key GET /v1/models -> 200 # next sync run: restored, same keyWhat it will and will not do
| Situation | What the sync does |
|---|---|
| Account disabled or deleted in Keycloak | Suspends it, recording the sync as the suspender |
| Account the sync suspended is enabled again | Lifts the suspension |
| Account suspended by a platform administrator | Never touches it, whatever Keycloak says |
| Administrator suspends an account the sync suspended | The administrator now owns the suspension; the sync will not lift it |
| Administrator re-activates an account that is still disabled in Keycloak | The next run suspends it again. Fix the account in the directory |
| Agent and service accounts, other issuers | Skipped |
An outage changes nothing. A network error, 5xx, 429, a 401 or 403 from the admin API, or a 404 that is not Keycloak's own "user not found" stops the run before any write, recorded as directory_unavailable.
A mass suspension waits for you. If one run would suspend more than TOKAMAK_AUTH_IDP_SYNC_MAX_SUSPENSIONS_PER_RUN (25) accounts, or more than TOKAMAK_AUTH_IDP_SYNC_MAX_SUSPENSION_PERCENT (10%) of the accounts checked, it suspends nobody. It records guard_tripped and logs an error; restorations still apply. After you have looked, run it once with the override:
# $SERVICE_TOKEN: a service token with audience tokamak-auth
curl -X POST https://<auth>/api/auth/internal/idp-sync/run \
-H "Authorization: Bearer $SERVICE_TOKEN" \
-H 'Content-Type: application/json' \
-d '{"allow_mass_suspension": true}'Only one replica runs the sync at a time, and Keycloak reads are paced by TOKAMAK_AUTH_IDP_SYNC_REQUESTS_PER_SECOND.
Watch it
GET /api/auth/internal/idp-sync/status (service token, audience tokamak-auth) returns the settings in effect, the last 20 runs with their outcome and counts, and the accounts the sync currently holds suspended. Each change is written to the auth audit log with the system as the actor, action user.idp_suspended or user.idp_restored, and request_id idp-sync-run:<id>.
Metrics: tokamak_auth_idp_sync_runs_total{outcome}, tokamak_auth_idp_sync_users_total{result}, tokamak_auth_idp_sync_last_run_timestamp_seconds{outcome}, tokamak_auth_idp_sync_last_success_timestamp_seconds and tokamak_auth_idp_sync_planned_suspensions. Alert on a stale last success and on any guard_tripped run.
Settings
| Variable | Default | Description |
|---|---|---|
TOKAMAK_AUTH_IDP_SYNC_ENABLED | false | Turn the sync on |
TOKAMAK_AUTH_IDP_SYNC_INTERVAL | 5m | Time between runs; minimum 30s |
TOKAMAK_AUTH_IDP_SYNC_BATCH_SIZE | 100 | Accounts read per page |
TOKAMAK_AUTH_IDP_SYNC_REQUESTS_PER_SECOND | 20 | Keycloak read rate; 0 is unthrottled |
TOKAMAK_AUTH_IDP_SYNC_MAX_SUSPENSIONS_PER_RUN | 25 | Absolute guard; 0 turns it off |
TOKAMAK_AUTH_IDP_SYNC_MAX_SUSPENSION_PERCENT | 10 | Percentage guard; 0 turns it off |
TOKAMAK_AUTH_IDP_SYNC_RUN_TIMEOUT | 2h | Time budget for one run. A run reads every account before writing, so this must cover accounts ÷ requests per second (144,000 at the defaults). A run that runs out changes nothing |
Limits
- Accounts that sign in with Google, Microsoft or an organization's single sign-on are suspended only when their Keycloak account is disabled or deleted. Keycloak does not learn when the upstream account is disabled; disable it in Keycloak too.
- A gateway delegation token already issued lives until it expires (about a minute); the next request is refused.
- An account deleted and recreated in the same realm becomes a new Tokamak account, with no memberships or keys, and the old one stays suspended. Add the person back to their organizations and revoke the old account's keys.
- Not shown in the platform console yet; use the status endpoint and metrics.
- SCIM provisioning (account and group changes pushed by the directory) is planned, not available.
Single sign-on (platform)
Prepare Keycloak for organization single sign-on, understand how Tokamak binds each organization's identity provider to its verified domains, and recover an organization locked out by a broken provider.
Connected applications
Let an application such as work-agents sign people in with their Tokamak account, read your organization's members and changes, and act for the people who allow it.