Short-Lived Access and Refresh Tokens
Split auth into a short access token for API calls and a longer refresh in a cookie, so a stolen access token expires fast and refresh is bound to one origin.
On this page
Introduction
This guide covers short access plus refresh token split, a recurring concern in the auth-jwt scenario. One long token is both a forgeable credential and a theft liability. Splitting into a short access token for API calls and a longer refresh token in an httpOnly cookie means a stolen access token dies in minutes, and the refresh token never crosses JavaScript where XSS could grab it.
Short access token
Issue an access token at five to fifteen minutes carrying the user id and role, so a stolen one expires before it does much damage and the API stays stateless.
The trade-off worth naming is that issue an access token at five to fifteen minutes carrying the user id and role, so a stolen one expires before it does much damage and the api stays stateless adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
const access = await new SignJWT({ sub: user.id, role: user.role }).setProtectedHeader({ alg: 'HS256' }).setExpirationTime('15m').sign(secret);
Refresh in a cookie
Put the refresh token in an httpOnly, Secure, SameSite=Strict cookie so JavaScript cannot read it, which closes the XSS path to the long-lived credential.
The trade-off worth naming is that put the refresh token in an httponly, secure, samesite=strict cookie so javascript cannot read it, which closes the xss path to the long-lived credential adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
res.cookie('refresh', refresh, { httpOnly: true, secure: true, sameSite: 'strict', maxAge: 30*864e5 });
Refresh on expiry
When the access token expires, call the refresh endpoint which reads the cookie and mints a new access token, so the user stays logged in without re-entering a password.
The trade-off worth naming is that when the access token expires, call the refresh endpoint which reads the cookie and mints a new access token, so the user stays logged in without re-entering a password adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
Revoke on logout
On logout, revoke the refresh token and clear the cookie, so the long-lived credential is dead and the client has nothing to refresh with.
The trade-off worth naming is that on logout, revoke the refresh token and clear the cookie, so the long-lived credential is dead and the client has nothing to refresh with adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
CSRF on refresh
Because the refresh cookie is sent automatically, protect the refresh endpoint with a CSRF token or SameSite, so a cross-site form cannot trigger a refresh.
The trade-off worth naming is that because the refresh cookie is sent automatically, protect the refresh endpoint with a csrf token or samesite, so a cross-site form cannot trigger a refresh adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
Test the split
Assert the access token expires and a refresh yields a new one, that the cookie is httpOnly, and that logout revokes the refresh, so the split holds.
The trade-off worth naming is that assert the access token expires and a refresh yields a new one, that the cookie is httponly, and that logout revokes the refresh, so the split holds adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
Sources
Sources: OAuth 2.0 BCP, Cookie attributes.
FAQ
- Why not put the access token in a cookie too?
- You can, but it is sent on every request including CSRF-able ones; keeping it in memory and the refresh token in a cookie limits what an attacker can grab with XSS versus CSRF.
- How short is short?
- Five to fifteen minutes for the access token; long enough that you are not refreshing on every call, short enough that a stolen one is useless quickly.
Report a problem with this articleOpens a pre-filled issue on GitHub.