Skip to content

Credential revocation ​

What stops working when, for every kind of credential: admin sessions, API tokens and JWT sign-ins. Read it when you decide how to react to a leaked password or a departing user, or when you build a credential flow of your own.

What ends when ​

The columns are the affected user's credentials. "Signed out" means the next request with that credential is anonymous (sessions) or refused with 401 (tokens, JWT).

EventCurrent sessionOther sessionsAPI tokens (rcms_…)JWT sign-ins (access + refresh)
Sign outendedkeptkeptkept
Sign out everywhere (logout-others)kept (new id)signed outall revokedall ended
Password changekept (new id)signed outall revoked (revoke_tokens_on_password_change)all ended (always)
User disabled (RbacService::set_user_active)endedendedall revokedall ended
Email changed or password reset by an adminsigned outsigned outall revokedall ended
Password reset by link / invitation accepted—signed outall revoked (revoke_tokens_on_password_change)all ended
User deletedendedendeddeleteddeleted
One token revoked (DELETE /api/v1/auth/token)——that tokenkept
JWT logout (POST /api/v1/auth/jwt/logout)——keptthat sign-in
A spent refresh token used again——keptthat sign-in
APP_KEY rotatedendedendedkeptall ended
Roles or permissions changedkeptkeptkept; new permissions apply on the next requestkept; new permissions apply on the next request

With the default settings every row applies from the next request on every instance. The opt-in caches below trade that for throughput on API instances.

Every row is covered by the framework's tests (crates/rustcms-auth/tests/: auth_flow.rs, jwt.rs, recovery.rs, rbac.rs, instances.rs).

How it works ​

Account changes bump users.auth_version in the same transaction as the change. Sessions and JWT sign-ins store the version they were issued for, and every request checks it:

  • A session of a disabled or deleted user is ended by the request that finds it.
  • A session whose version is old makes the request anonymous and is frozen: never renewed, no cookie sent, left to expire. It is not deleted at that point, because the same browser may be re-issuing it for the new version in a concurrent request (the browser that changed the password keeps its session, with a new id).
  • An access token (JWT) also checks that its sign-in is not revoked or expired; the credential change marks every sign-in of the user revoked.

revoke_tokens_on_password_change (default true) applies to API tokens only: JWT sign-ins always end, like sessions.

Resolved permissions are cached for at most permission_cache_ttl_secs (30), keyed by a counter (auth_generations.rbac) that every RBAC change advances in its own transaction, and by a counter of the instance that every change made through it advances.

Several instances ​

By default, every request reads the RBAC counter, and a JWT's sign-in and user: a committed change applies on every instance from its next request. Two settings, off by default, let an instance reuse these reads for a short time so that a request does not pay for them every time (for example on API mode instances serving heavy traffic; 1000 is a reasonable value):

SettingDefaultWhat is reusedA change made on another instance applies
auth.permission_generation_cache_ms0 (off)the RBAC counter (auth_generations.rbac)within that time: roles, permissions, and through them every guard
auth.jwt.session_cache_ms0 (off)a JWT's sign-in and userwithin that time: sign-out, password change, user disabled or deleted

When they are on:

  • On the instance that made the change, it applies on the next request: RbacService, the sign-out, refresh, password, reset and invitation endpoints forget this instance's cached reads. Credential functions called directly from your code (credentials::logout_everywhere, …) leave them to their time.
  • Sessions and API tokens are read on every request: their revocation is never delayed. Permissions they carry follow the RBAC counter above.
  • Leave them at 0 when a revocation must apply everywhere at once; each costs one statement per request.

Session ids and refresh tokens are keyed from APP_KEY only, not from APP_PREVIOUS_KEYS: rotating the key ends them whatever the previous keys hold (Rotating APP_KEY).

Every admin response carries x-rustcms-user: <id> while signed in, so the panel can tell when the cookie changed accounts in another tab and never shows one user's page to another.

Races ​

  • Password changes, sign-out everywhere and token issuance re-check, inside a write transaction holding the user row, that the credentials verified earlier in the request are unchanged (password hash, auth_version, active). A token obtained with a password that is changed concurrently is refused, never issued after the revocation.
  • The permission cache is keyed by a generation advanced on every RBAC change: a lookup that started before a change cannot repopulate the cache with the old permissions.
  • JWT refresh tokens rotate under the locks of the user, the sign-in and the token, in that order (the same order as issuance and revocation). Two refreshes with one token: one wins, the other counts as a replay and ends the sign-in.
  • A request whose session was revoked can start a new one (CSRF, sign-in) in the same response.
  • A request already running when a credential is revoked finishes with the credentials checked when it started. Changes made through RbacService re-read the actor under lock and are refused (403) when the account's auth_version changed (password change, sign-out everywhere, admin reset), or the account was disabled or deleted. Revoking only one token, one JWT sign-in or one session does not change the account's version, so it does not stop an RBAC change that request already authenticated.

Building your own credential flow ​

rustcms::auth::credentials holds the primitives the built-in endpoints use. Use them when you add a flow of your own (an SSO callback that must end other sessions, an admin tool that revokes a user's tokens):

FunctionDoes
lock_unchanged(&txn, &snapshot)reloads the user under a row lock; 401 unless the password hash, auth_version and active flag still match snapshot
revoke_all(&txn, user_id, tokens)bumps auth_version, ends every JWT sign-in and, with tokens, revokes every API token; returns the new version
revoke_tokens(&txn, user_id)revokes every API token of the user
change_password(db, &snapshot, new_hash, revoke_api_tokens, &audit)sets the password and revokes the other credentials, in one transaction; returns the new version
logout_everywhere(db, &snapshot, &audit)ends every session, sign-in and token; returns the new version
issue_token(db, &snapshot, name, &abilities, expires_at, &request)issues an API token, unless the credentials changed since they were verified

Call revoke_all and revoke_tokens inside the transaction that changes the account, so the revocation commits or rolls back with it. After change_password or logout_everywhere in an admin request, keep the caller's session signed in with session.restamp(new_version) (the Session extractor); otherwise that request's own session is signed out too.

Revocations are recorded in the audit log: auth.password_changed, auth.signed_out_everywhere, token.revoked, token.created, and the user.* entries of RbacService.

RustCMS 0.17.0 · source cfb82fa33fab · MIT OR Apache-2.0