Topic 162 / Core
JWT and session management: expiry, refresh token rotation, revocation
Chapter 10.2 · Identity and Access
What and why
The flow in topic 161 hands back tokens; this topic is how those tokens — and server-side sessions, the older alternative — are managed safely once issued. A JWT (JSON Web Token) is self-contained: header, claims, and a signature, verifiable by any service holding the verification key (a public key for asymmetric signing, the shared secret for HMAC signing), with no database lookup. A server-side session is the opposite: a reference (usually a cookie) to state held entirely on the server. The trade-off is the same one every stateless-vs-stateful choice makes: JWTs scale verification without a shared store, at the cost of making revocation hard; sessions make revocation trivial, at the cost of a lookup on every request. Builds on: → 161 (OAuth 2.0 and OpenID Connect).
Diagram
| JWT (stateless) | Server-side session (stateful) | |
|---|---|---|
| Verification | Signature + expiry check, no lookup | A lookup against the session store, every request |
| Revocation | Hard — token is valid until it expires, unless a denylist is kept | Trivial — delete the session record |
| Scaling verification | Any service with the verification key, independently | Requires a shared, reachable session store |
How it works
- Issue a short-lived access token (minutes, not days) — the shorter its life, the smaller the window a stolen one is useful for.
- Issue a longer-lived refresh token, used only to obtain a new access token when the current one expires.
- Refresh token rotation: every time a refresh token is used, issue a new one and invalidate the one just used. A refresh token presented a second time (reuse of an already-rotated-out token) is a theft signal, not a normal retry.
- On detecting reuse, revoke the entire refresh-token chain for that session, not just the one token — the attacker and the legitimate user may both be holding copies at that point.
- Revocation for stateless JWTs needs an explicit mechanism — a short-lived access token plus a revocable refresh token is the usual answer; a pure-JWT system with no denylist cannot revoke an access token before it naturally expires.
- Server-side sessions revoke instantly by design, at the cost of a store lookup per request — the simpler choice when that cost is acceptable.
Practical example
(illustrative). Access token expiry: 15 minutes. Refresh token expiry: 30 days, rotated on every use. If a refresh token already rotated out is replayed, the system revokes the whole chain immediately — the legitimate user is forced to log in again, but the stolen token stops working within, at most, the 15-minute access-token window that was already issued from it.
When to use it
- Use JWTs when: many services must verify tokens independently, without a shared, always-reachable session store — the stateless scaling is the actual requirement, not a default.
- Use server-side sessions when: instant revocation matters more than stateless scaling, and a shared store (e.g. Redis) is already part of the architecture.
- Avoid: long-lived JWTs with no rotation or revocation plan — a stolen one is valid for its full lifetime with nothing to stop it.
- Failure scenarios: a stolen refresh token reused undetected because rotation isn't implemented; sensitive data placed in JWT claims, which are readable (though not modifiable) by anyone holding the token; a "logout" that only deletes the client-side copy and never revokes the server-side session or refresh chain.
- Simpler alternative: a server-side session store (Redis) when revocation and simplicity matter more than avoiding a shared store.
Practice
(Mechanism kind — implement minimal, spot the misuse; 20–30 min, lab: Redis). Implement an access/refresh token pair with rotation: issuing a refresh token stores its identifier in Redis; using it issues a new one and deletes the old identifier; presenting an already-deleted identifier revokes every token issued from that session. Reproduce the theft scenario by replaying an old refresh token and confirm the whole chain is revoked, not just the replayed one. Done when replay is detected and the full chain revocation is visible in Redis.
Shared lab setupKey takeaway
Short-lived access tokens plus rotating refresh tokens give most of statelessness's scaling with a real revocation story; a plain long-lived JWT does not. Done when you can: state, for a token scheme, exactly how long a stolen token remains useful and what detects reuse. Used next in → 163 (claims drive authorization decisions); Project 3.
References
None — this topic makes no external claims.
Source directory