Token Revocation and Blocklisting
What you'll learn: How to invalidate JWTs before their natural expiration to handle logout, security breaches, and permission changes.
The Challenge with JWTs
One of JWT's biggest strengths—statelessness—is also its Achilles' heel. Once issued, a JWT remains valid until it expires. But what if a user logs out, their account is compromised, or their permissions change? You need a way to revoke tokens before their time runs out.
Revocation Strategies
Blocklists (Blacklists)
Maintain a list of revoked token IDs (typically the jti claim) in fast storage like Redis. On each request, check if the token is blocklisted. Yes, this adds a lookup and reintroduces some statefulness—but only for revoked tokens, not all tokens.
Analogy: Like a "no-fly list" at an airport. Most travelers pass through quickly, but you check the list to catch the few who shouldn't be allowed.
Short Expiration Times
Issue tokens that expire quickly (e.g., 5-15 minutes). The damage window for a compromised token is minimal. Pair this with refresh tokens (which you learned about in Token Refresh and Rotation) to maintain user sessions smoothly.
Token Versioning
Include a version number in the token that matches a version stored with the user's account. When you need to invalidate all tokens for a user, simply increment their stored version. Tokens with old versions fail validation.
Key Takeaway: Because JWTs are stateless, revocation requires trade-offs—blocklists add state but provide precise control, short expiries limit exposure windows, and versioning enables bulk invalidation without tracking individual tokens.