Identity infrastructure · · 7 min read
The refresh token is the session
By the Argenium team
Teams spend a lot of time tuning access-token lifetimes and very little thinking about refresh tokens, which often live for weeks. But whoever holds the refresh token holds the session. Design that token first.
Two tokens, two jobs
An access token is presented to your APIs on every request. To keep that fast it is usually a signed JWT that each service verifies locally, without calling anyone. The price is that you cannot cheaply take it back: once issued, it works until it expires. Its lifetime is your window of irrevocable access, which is why it should be measured in minutes.
A refresh token is presented only to your authorization server, which can check it against stored state on every use. That makes it the right place for everything that needs memory: rotation, revocation, idle timeouts and the user’s list of active sessions.
Rotate, and notice reuse
With rotation, every refresh returns a new refresh token and retires the old one. On its own that only limits how long a stolen token is useful. The valuable part is what rotation lets you detect: if a retired token is presented again, two parties hold copies of it, and you cannot tell which one is legitimate. The safe answer is to revoke the entire chain, the token family, and send the user back to sign in.
async function refresh(presented: string) {
const token = await tokens.findByHash(hash(presented));
if (!token || token.revoked) throw new InvalidGrant();
// Atomic compare-and-set: of two concurrent requests, only one may win.
const won = await tokens.markUsedIfUnused(token.id);
if (!won) {
// A rotated token came back. Someone else has a copy.
await tokens.revokeFamily(token.familyId);
throw new InvalidGrant();
}
return issuePair({ subject: token.subject, familyId: token.familyId });
}Two details matter. Store refresh tokens hashed, like passwords, so a database leak does not hand out live sessions. And make the used-check atomic: a read followed by a separate write lets two parallel requests both succeed. If flaky mobile networks cause false alarms, allow a grace window of a few seconds that returns the same new token, rather than weakening detection.
Bind tokens to the client
Bearer tokens work for anyone who has them. Sender-constrained tokens only work for the client they were issued to. For server-to-server traffic, mutual TLS can bind tokens to a client certificate (RFC 8705). For browsers and mobile apps, DPoP (RFC 9449) has the client keep a private key and sign a short proof for each request; the token is bound to that key, so a token copied out of a log or a proxy is useless on its own.
The OAuth 2.0 Security Best Current Practice (RFC 9700) asks that refresh tokens issued to public clients be either sender-constrained or rotated. Doing both is better.
Where a browser should keep tokens
Anything in localStorage or a JavaScript variable can be read by any script that runs on your page, including the one injected through an XSS bug you have not found yet. The sturdier pattern for single-page apps is a backend-for-frontend: a thin server component completes the OAuth flow, keeps the tokens, and gives the browser only an HttpOnly, Secure, SameSite session cookie. Script can no longer exfiltrate the tokens; at worst it can act within the page while the user is there.
Lifetimes and the moments that should end them
- Two clocks, not one. An idle timeout ends sessions nobody is using; an absolute lifetime ends sessions eventually, however active.
- Revoke on credential change. A password reset, a removed passkey or “sign out everywhere” should revoke every token family for that user.
- Step up for sensitive actions. Changing an email address or payout details deserves fresh authentication, whatever the session age.
- Show users their sessions. A list of devices with the option to end each one turns users into part of your detection.