Track 07 · Lesson 5 of 18Identity and login

Track 07 · Identity and login

Access and refresh tokens

Lesson 5 of 18· 7 min

A short-lived access token (minutes) is sent with every API call, so a leak is only useful briefly. A long-lived refresh token (days) is used only to get new access tokens, so users aren’t logged out every 15 minutes.

Let Mallory steal the refresh token

Use the API, let time pass, then attack

Minute 0

Access token

A1

Valid for 15 more minutes. Kept in memory, sent as Authorization: Bearer A1.

Refresh token

R1

Valid for 30 days. Lives in an HttpOnly cookie, only sent to /auth/refresh.

  1. POST /login → access token A1 (15 minutes) and refresh token R1 (30 days).

Two tokens, two jobs#

The access token is a bearer token: it works for whoever holds it, sent as Authorization: Bearer …. Keeping it to minutes limits what a leak is worth. The refresh token is only ever sent to one endpoint, POST /auth/refresh, and the server tracks it, so it can be revoked like a session.

Rotation and reuse detection#

With rotation, every refresh returns a new refresh token and retires the old one. If a retired token ever shows up again, two parties hold copies, so one of them stole it. The server can’t tell which, so it revokes the whole session family. The thief is locked out, and the real user just logs in again.

The window that stays open#

Logging out revokes the refresh token on the server, but any access token already issued keeps working until it expires. That window is the price of stateless tokens, and the reason to keep them short.

Check yourself

With rotation on, an old refresh token that was already used shows up at the refresh endpoint. What should the server do?