JWT Token Lifecycle and Revocation

The structural weakness of stateless JWTs: a signed token is valid until it expires regardless of logout, password reset, or theft — so lifetime, storage, and revocation design decide the blast radius.

The core problem

A JWT is a bearer token (RFC 6750): possession is the whole proof, and a resource server that verifies a signature and exp asks the auth server nothing. So the server has nothing to “forget” on logout. Deleting the client’s copy, ending the auth-server session, or resetting the password does not reach a token already issued — it keeps working until exp. With a 24h token, a secret copied at 09:00 and noticed at 09:05 still works until ~08:55 next day. The right question is not “does logout work?” but “how long can a token I no longer trust keep working?”

Expiration and refresh

  • Short access tokens (5–15 min) cap the window; Keycloak defaults access tokens to 5 minutes. The cost is refresh traffic: a 15-min token = 96 refreshes/user/day (~111 req/s at 100k always-on users).
  • Absolute vs sliding expiry: absolute ends the session at a fixed point; sliding extends on activity but needs an upper bound (Keycloak: SSO Session Idle sliding window, SSO Session Max absolute ceiling; refresh-token life is derived from the session, there is no standalone refresh-lifetime knob).
  • Refresh-token rotation (RFC 9700 §4.14.2): each use returns a new refresh token and invalidates the old one. Seeing an invalidated refresh token return means two parties hold the family — revoke the whole family and force re-auth. This gives theft detection “for free”. Rotation is a realm-level control in Keycloak (Realm settings > Tokens), not per-client Advanced.
  • Rotation must be atomic: invalidating token A and issuing token B belong in one database transaction, otherwise two concurrent refreshes with A both succeed (a race that defeats reuse detection). Refresh tokens need not be JWTs; opaque random values validated against a server-side record are enough (Moorthy).
  • Failure to invalidate on reset: JWT Auth WordPress plugin ≤ 3.0.2 (CVE-2026-106466) kept refresh tokens valid after password reset or device removal, so a previously stolen refresh token still minted access tokens.

Revocation faster than expiry

Three ways, each reintroducing some state (the line from “no state, slow revocation” to “some state, fast revocation”):

  • Denylist of jti until its exp passes, checked in a fast cache; stays small because access tokens are short-lived.
  • Per-user token version / nbf timestamp: store a counter or timestamp on the user, embed/compare against iat, bump on “log out everywhere” / password reset to invalidate all outstanding tokens at once.
  • Opaque tokens + introspection (RFC 7662): random string, resource server asks the auth server if it is active — instant revocation, but restores the lookup JWTs removed. scip.ch notes revocation is the inherent JWT trade-off; many teams use opaque access tokens externally and JWTs internally.
  • Key the denylist on a canonical value: PyJWT accepted non-canonical encodings of the signature segment, which Ubuntu notes could let an attacker bypass token revocation checks (USN-9177-1) (USN-9177-1). The advisory gives no mechanism; the likely one is a denylist keyed on the raw token string, which a re-encoded but still-valid signature no longer matches. Revoking by jti avoids depending on encoding. See Python JWT Library Vulnerabilities.

Theft, storage and binding

  • Stolen tokens: the signature proves genuineness, not that the sender is the subject. A copy replays exactly. Vectors: token in URL (leaks via logs/history/Referer), XSS reading localStorage, MITM on plaintext, device malware, debug logs/crash reports/support tickets.
  • Storage: localStorage is XSS-reachable — “sign in once, get hacked everywhere” across micro-frontends sharing a token. Prefer access token in memory + refresh token in HttpOnly, Secure, SameSite cookie. Never read/trust claims client-side for access control, and never paste real tokens into third-party decoder sites.
  • Refresh cookie scoping: Set-Cookie: refresh_token=…; HttpOnly; Secure; SameSite=Lax; Path=/auth/refresh keeps the refresh token away from JS and from every other request; because the browser attaches it automatically, the refresh endpoint still needs CSRF protection (Moorthy).
  • Reducing reuse: short expiry + rotation; sender-constrained tokens (DPoP / mTLS) bind the token to a client key — with DPoP the client also proves possession of a private key, so a stolen token alone does not replay, at the cost of implementation complexity and without replacing basic validation (Moorthy); IP binding helps against a remote thief but logs out roaming users and misses same-device XSS/malware — use coarse signals (country/ASN) as a risk cue rather than hard logout.
  • AI agents are now heavy bearer-token holders (MCP authorization builds on OAuth); an agent-reachable token sits where injection or tool misuse can exfiltrate it, sharpening the lifetime argument.

Operational pitfalls

  • A signing key regenerated per process start (os.urandom(32) each boot) invalidates every session on restart — logs everyone out.
  • Key rotation with JWKS: publish the new key before signing with it and keep the old key until its tokens expire — that overlap is why kid exists; verifiers cache the set and refresh once on an unknown kid (JWKS and kid).
  • Rotation after compromise must also invalidate live sessions: Intel/CBSE rotated a cracked HS256 key and killed all active sessions so old forged tokens stopped working (Intel Inside).
  • Client-side JWT minting leaks the signing secret (see JWT Secret Cracking).
  • Simple-JWT-Login WordPress CSRF (plugin action) — token-issuing endpoints need CSRF defence like any state-changing endpoint; see Cross-Site Request Forgery.

Defenses (summary)

  • Short-lived access tokens; rotating, revocable refresh tokens; invalidate refresh tokens on password reset and device removal.
  • A revocation mechanism sized to what a stolen token can do (denylist, token-version, or introspection).
  • HttpOnly/Secure/SameSite cookie or in-memory storage; no tokens in URLs; TLS always.
  • Stable signing keys with rotation; consider sender-constrained tokens for high-value sessions.

Sources