0001: Stateless JWTs with per-user token versioning¶
Status: Accepted
Context¶
Pure stateless JWTs cannot be revoked before they expire. That makes logout, password changes and deactivation ineffective until the token times out. A server-side session store would fix this, but it adds infrastructure (for example Redis) and a lookup on every request.
Decision¶
- Issue short-lived access tokens and longer-lived refresh tokens, both HS256 JWTs carrying
typ,iss,aud,jtiandver. vermust equaluser.token_version, which is checked on every request (the user row is loaded anyway).- Incrementing
token_versionrevokes every token for that user. This happens on logout, password change or reset, deactivation and role change. - Password-reset tokens are a separate
typand become invalid after one use (the reset incrementstoken_version).
Consequences¶
- Revocation is per user ("sign out everywhere"), not per device. Per-device sessions would need a session table keyed by
jti. - Refresh tokens rotate (each refresh returns a new pair), but old refresh tokens stay valid until expiry or the next version bump; there is no reuse detection.
- Rotating
SECRET_KEYinvalidates every token. Asymmetric keys (RS256/EdDSA with a JWKS endpoint) are the upgrade path if other services need to verify tokens.