Menu

Refresh token rotation with reuse detection in FastAPI (SQLAlchemy example)
📰

Refresh token rotation with reuse detection in FastAPI (SQLAlchemy example)

DEV Community: fastapi·LaunchPad Kits·3 days ago
#OBTg5NaE
#devtofastapi#devto#dev#refresh#token
Long article (637 words) — no AI summary yet.
Reading 0:00
15s threshold

Long-lived refresh tokens are the weak point of JWT auth: if one leaks, the attacker can mint access tokens until it expires. Rotation plus reuse detection limits the damage. Here is a compact implementation with FastAPI and async SQLAlchemy.

The idea

Every refresh token has a unique id ( jti ) and belongs to a family , which is created at login.

Each refresh token is single use . Calling /refresh revokes the old token and issues a new one in the same family.

If a token that was already revoked shows up again, someone replayed it (the legitimate client or the thief used it first). Revoke the whole family , which logs out both.

The model

The server keeps a record of every issued refresh token.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.