How to Implement JWT Revocation and Blocklisting
Add revocation to stateless JWTs: keep a short lifetime, use a blocklist or a token version, and revoke efficiently without a lookup on every request.
On this page
Introduction
JWTs are popular because they are stateless: the server verifies a signature and trusts the claims without a database lookup or a session store. That same property makes them hard to revoke. Once issued, a JWT is valid until it expires, and the server has no built-in way to say ‘this one is no longer good’. A stolen token remains usable for its whole lifetime, which is why revocation is a design decision, not an afterthought.
This tutorial covers the stateless trade-off, how a short lifetime bounds the damage, how to add a blocklist or a token version when immediate revocation is required, and how to test the revocation paths. The examples use standard JWT claims, and the techniques apply to any stateless token.
Accept the stateless trade-off
A JWT is verified by checking its signature and its standard claims, not by consulting a store. That is what makes it fast and horizontally scalable, and also what makes revocation hard: there is nothing to delete or update to invalidate it. Any revocation scheme adds back some state, so the design question is how much state you are willing to add for how much revocation power.
Decide the requirement first. If a bounded delay in revocation is acceptable, a short lifetime and a refresh mechanism may be enough, because the token stops working soon after the refresh is denied. If immediate revocation is required, you need a stateful check, a blocklist or a version, and you accept the lookup or a cache. Choose with the requirement in hand rather than claiming statelessness while needing immediate revocation.
Bound the damage with a short lifetime
The simplest control is a short access-token lifetime, often minutes. A stolen token is then useful only for that window, and the client renews with a refresh token. Revocation is enforced at the refresh step: deny the refresh and the client cannot obtain a new access token, so access ends within one access-token lifetime. No blocklist is needed for the common case of a logout or a session ending.
Pair the short access token with a refresh token that is checked against state, so the refresh is where revocation takes effect. Keep the access token lifetime short enough that the residual window is acceptable, and long enough that the refresh traffic is manageable. This is the trade-off: a shorter lifetime narrows the window but increases refreshes, and the right value depends on how sensitive the resource is.
import { SignJWT } from 'jose';
async function issueAccessToken(userId, key) {
return new SignJWT({ sub: String(userId), ver: userTokenVersion(userId) })
.setProtectedHeader({ alg: 'ES256' })
.setIssuedAt()
.setExpirationTime('5m') // short: bounds how long a stolen token works
.sign(key);
}
Add a blocklist for immediate revocation
When immediate revocation is required, keep a blocklist of revoked tokens, keyed by the token’s unique identifier, the jti claim, and set an entry’s lifetime to the token’s remaining lifetime. A blocklist entry needs to live only until the token would have expired, because after that the expiry check rejects it anyway. Storing it with a TTL equal to the remaining lifetime keeps the list small and self-cleaning.
The verification step then checks the signature, the standard claims, and the blocklist, in that order, so an invalid signature never reaches the blocklist lookup. Cache the blocklist in memory or a fast store to avoid a database round trip on every request, and accept that the cache introduces a small delay in propagating a revocation. The lookup is the cost of immediate revocation, and a short lifetime reduces how much you need it.
async function verifyToken(token: string, key: unknown, isRevoked: (jti: string) => Promise<boolean>) {
const { payload } = await import('jose').then((m) => m.jwtVerify(token, key));
if (await isRevoked(payload.jti as string)) throw new Error('revoked');
return payload;
}
async function revoke(jti: string, exp: number) {
const ttl = exp * 1000 - Date.now();
if (ttl > 0) await blocklist.set(jti, '1', ttl); // expires with the token
}
Use a token version for mass revocation
For revoking all of a user’s tokens at once, such as after a password change or a suspected compromise, a per-user token version is simpler than blocklisting each token. Store a version on the user, include it as a claim when issuing tokens, and reject any token whose version is below the user’s current version. Bumping the version invalidates every outstanding token for that user in one write.
Version checking needs the user’s current version on each request, which is a lookup unless it is cached; cache it with a short TTL, and accept the small propagation delay. Combine the version for mass revocation with a blocklist for single-token revocation where both are needed, and always keep a short lifetime so the stateful check is a backstop rather than the only protection.
ALTER TABLE users ADD COLUMN token_version int NOT NULL DEFAULT 0;
-- revoke every outstanding token for this user in one write
UPDATE users SET token_version = token_version + 1 WHERE id = $1;
Test the revocation paths
Test that a valid token is accepted before revocation and rejected after each revocation mechanism: a blocklist entry, a version bump, and the expiry of a short lifetime. Test that a token with a bad signature is rejected before the stateful check, so the blocklist is not consulted for garbage. Test that a blocklist entry disappears after the token’s expiry, so the list does not grow without bound.
T=$(node scripts/issue-token.mjs user=1)
curl -s -o /dev/null -w '%{http_code}\n' -H "Authorization: Bearer $T" http://localhost:3000/me # 200
node scripts/bump-version.mjs 1
curl -s -o /dev/null -w '%{http_code}\n' -H "Authorization: Bearer $T" http://localhost:3000/me # 401
Common pitfalls
Claiming statelessness while needing immediate revocation, a long access-token lifetime that widens the theft window, a blocklist entry without a TTL so the list grows forever, checking the blocklist before the signature, and no refresh-checked path so a revoked user keeps renewing are the recurring mistakes. A short lifetime, a TTL-scoped blocklist or a token version, signature-first verification, and refresh enforcement address them.
Conclusion
JWTs cannot be un-issued, so revocation is a trade-off between state and immediacy. Keep access tokens short and enforce revocation at the refresh, add a blocklist keyed by jti with a TTL for single-token revocation, and bump a per-user token version for mass revocation. Self-check by asserting a token is rejected after each mechanism and that a blocklist entry expires with the token. Then a stolen token is useful for minutes, not months.
Sources: RFC 7519: JSON Web Token, OWASP: JSON Web Token cheat sheet, RFC 7009: OAuth 2.0 Token Revocation.
FAQ
- Why can't I revoke a JWT directly?
- A JWT is self-contained and verified with a signature, so the server does not consult anything to accept it. Revocation requires either a stateful check — a blocklist or a version — or letting the token expire, which bounds how long a stolen token is useful.
- Blocklist the token or bump a version?
- A token version per user is easier to manage for mass revocation such as a password change: reject any token whose version is below the current one. A blocklist keyed by token id is finer-grained for revoking a single token. Many systems use both.
Report a problem with this articleOpens a pre-filled issue on GitHub.