Skip to content

Lesson 8 of 14 · 15m

Refresh Tokens and Revocation

Short-lived access plus rotating, reuse-detected refresh tokens — and the revocation state that 'stateless JWT' can't avoid.

The two-token model

The design that works is asymmetric: a short-lived access token (JWT, 5–15 minutes, verified locally against a JWKS — fast, no lookups) and a long-lived refresh token (opaque, stored server-side, single-use, rotated). The access token dies fast enough that theft has a shelf life; the refresh token does the real session work behind a HttpOnly cookie.

The asymmetry is the point. Access tokens are checked per-request everywhere — keeping them self-verifying keeps that check cheap; making them long-lived would spread revocation-sensitive credentials across every API you run. Refresh tokens are checked once per refresh at one endpoint — making them opaque and server-side puts the state where it's cheap to enforce and easy to kill.

Rotation and reuse detection

Every refresh call exchanges the current refresh token for a new one and invalidates the old. Now picture theft: the attacker uses the stolen refresh token first, then the real user presents the same token — a replay of an invalidated credential. That replay is a theft signal, and the correct response is to kill the entire token family: the whole session chain derived from it. RFC 9700 (the OAuth 2.0 Security BCP, published January 2025) spells out the pattern; Supabase's GoTrue implements it natively — refresh rotation is on by default in supabase-js (autoRefreshToken) and reuse detection can invalidate the session family on replay.

One nuance: legitimate replays happen — a race between two tabs refreshing at once. Implementations grant a short reuse grace window (GoTrue's is configurable) before declaring theft. Tighten the window, don't remove the detection. Also cap the family itself: rotation bounds each individual token, but a family that can rotate forever is a session that never dies — give the chain an absolute expiry too, and require re-authentication when it ends.

Logout deserves the same precision: it must invalidate the refresh token and family server-side. Deleting the cookie without killing the family leaves a live ticket for anyone who copied it — the same dead-cookie/live-session distinction as the session lesson, one layer up.

Revocation is where "stateless" goes to die

"Stateless JWT" advocates promise logout-by-deleting-a-cookie. Then ops asks to kill a compromised user's access right now and you discover the truth: JWTs cannot be revoked; they only expire. Your options:

  • Short TTLs — bound the exposure to minutes. The standard answer.
  • jti denylist — a Redis set of revoked token IDs, checked per request. It works; it's also server-side state, which is what you were avoiding.
  • Refresh-gated access — access tokens live for the lifetime of one refresh window; revocation happens at the refresh endpoint where state already exists.

The design that survives production is short-lived access + rotated refresh + a revocation path that lives at the refresh layer, because that's where the state legitimately exists anyway.

Keys have privilege: the Supabase shape

On a GoTrue/Supabase stack the credential hierarchy matters more than the token format: the anon/publishable key identifies a project and is meant to be public — RLS is the enforcement layer. The service_role key is also just a JWT — but it's signed to bypass RLS entirely. Ship it client-side once — bundled env, leaked NEXT_PUBLIC_ variable, committed .env — and every row-level policy in the database is decorative. Server-side only, rotated on suspicion, and grep your client bundles for it.

Claims worth checking beyond exp: iss (which project signed it), aud (authenticated vs service_role), and aal (aal2 for MFA-verified operations). Supabase's own MFA model treats aal as the step-up boundary — a lesson in why claims are checks, not facts.

What to actually do

Access tokens ≤15 minutes. Refresh tokens rotated, reuse-detected, family-killed on replay, HttpOnly. Revocation implemented at the refresh layer — accept the state. service_role never leaves the backend. Verify iss/aud/exp/aal on every protected route.

Keep reading

Related articles from the SudoSecurity blog.