Access Tokens vs Refresh Tokens: Lifetimes and Rotation

Why OAuth splits access and refresh tokens, how to pick lifetimes, how rotation and reuse detection catch a stolen refresh token, and how logout should revoke them.

By · · Updated · 10 min read

Illustration for Access Tokens vs Refresh Tokens: Lifetimes and Rotation

Your single-page app starts getting 401 Unauthorized from the API about fifteen minutes after login, and someone suggests making the access token last a week. Don't. An access token is the short-lived credential the API checks on every request; a refresh token is a longer-lived credential the client sends only to the authorization server, used to get a new access token when the old one expires. Keep the access token short, rotate the refresh token on every use, and the 401 becomes a silent refresh instead of a security hole.

What follows is based on RFC 6749 (OAuth 2.0), RFC 9700 (the OAuth 2.0 Security Best Current Practice) and RFC 10017 (OAuth 2.0 for Browser-Based Applications).

Why there are two tokens

RFC 6749 defines both. Access tokens are "credentials used to access protected resources": the client sends one to the resource server (your API) on every call, normally in an Authorization: Bearer header as RFC 6750 describes. Refresh tokens are "credentials used to obtain access tokens", and the spec is blunt about where they go: they are "intended for use only with authorization servers and are never sent to resource servers."

That split is the point. An access token passes through your front end, proxies, logs and every service behind the gateway, and a bearer token works for whoever holds it. So it should expire quickly and carry only the scope and audience it needs; RFC 9700 says access token privileges "SHOULD be restricted to the minimum required" and tokens "SHOULD be audience-restricted to a specific resource server."

A refresh token travels only between the client and the token endpoint, so it can live longer, and because the authorization server checks it on every use, it can be revoked. RFC 9700 sums up the trade: refresh tokens let the server "issue access tokens with a short lifetime and reduced scope."

Access tokenRefresh token
Sent toResource server (your API), every requestAuthorization server token endpoint only
FormatOften a signed JWT (RFC 9068), sometimes opaqueUsually opaque to the client
LifetimeMinutesHours to weeks, bounded by the login session
Checked byThe API, often locally from the signatureThe authorization server, against its own records
RevocationHard if self-contained: it stays valid until expEasy: the server deletes or flags it

Worked example: read a real access token

Here is an access token in the JWT format from RFC 9068, signed with the ES256 example key published in RFC 7515, Appendix A.3. It is safe to paste anywhere because that key is public; never sign real tokens with it.

eyJhbGciOiJFUzI1NiIsInR5cCI6ImF0K2p3dCIsImtpZCI6Im9LSXl3dkdVcFRWVHl4TVEzYndJSWVRVXVkZnJfQ2tMTWpDRTE5RUNELVUifQ.eyJpc3MiOiJodHRwczovL2F1dGguZXhhbXBsZS5jb20iLCJzdWIiOiJ1c2VyLTQ4MjEiLCJhdWQiOiJodHRwczovL2FwaS5leGFtcGxlLmNvbSIsImNsaWVudF9pZCI6Im9yZGVycy13ZWIiLCJzY29wZSI6Im9yZGVyczpyZWFkIiwiaWF0IjoxNzkzNzgyODAwLCJleHAiOjE3OTM3ODM3MDAsImp0aSI6ImIxZTdjM2QyLTlmNGEtNGM2MS04YTJlLTVkMGY3ZTlhMWMzNCJ9.d3U4YxXS57QYO2A3Z-eSh6mpUyyk9RC19zyBdKY2B_I4UMnVlVsSFe40F8qRN2MNB7qzpF4XjvRrz-t5eth1og

Step 1: decode it. Open the JWT Decoder, paste the token (a leading Bearer copied from the network tab is fine) and click Decode JWT. The Header box shows:

{ "alg": "ES256", "typ": "at+jwt", "kid": "oKIywvGUpTVTyxMQ3bwIIeQUudfr_CkLMjCE19ECD-U" }

and the Payload box shows iss https://auth.example.com, sub user-4821, aud https://api.example.com, client_id orders-web, scope orders:read, iat 1793782800, exp 1793783700 and a jti.

Step 2: read the lifetime. exp minus iat is 1793783700 minus 1793782800, which is 900 seconds: a 15-minute token. The token response's expires_in (seconds, RFC 6749) tells the client the same thing, so it can refresh a little early. In UTC, iat is 09:00:00 on 4 November 2026 and exp is 09:15:00 (the Epoch Converter turns each into a date). A token like this explains the 401 in the opening: after 09:15 the API rejects it, and the client should already have refreshed. RFC 7519 lets servers allow "some small leeway, usually no more than a few minutes" for clock skew, so a token can work for a few seconds past exp on one server and not another.

Step 3: try the refresh token. Paste the refresh token from the same login, for example 8xLOxBtZp8kq2T3mWQyR0Jv5nHc7aEdUfgIsPo1VbN4, into the decoder and it says Invalid JWT token. That is expected. RFC 6749 says the refresh token string is "usually opaque to the client": it is a random handle the authorization server looks up, and there is nothing inside for the client to read.

Step 4: match the signing key. The API verifies the token with the authorization server's public keys, published as a JWK Set at its jwks_uri. Paste this one into the JWK Tool and click Convert to PEM:

{"keys":[{"kty":"EC","crv":"P-256","x":"f83OJ3D2xF1Bg8vub9tLe1gHMzV76e8Tus9uPHvRVEU","y":"x_FEzRu9m36HLN_tue659LNpXW6pCyStikYjKIWI5a0","use":"sig","alg":"ES256"}]}

The tool shows the PEM public key and an RFC 7638 thumbprint (SHA-256) of oKIywvGUpTVTyxMQ3bwIIeQUudfr_CkLMjCE19ECD-U, the same value as the token's kid. That is how the API picks the right key when the JWK Set holds several during a key rotation. The decoder never checks the signature; verification belongs in your API's JWT library, using this key and checking iss, aud, exp and typ as RFC 9068 requires.

Choosing lifetimes

The RFCs give no numbers; the right lifetime depends on how bad a stolen token would be. Ask two questions.

Access token: how long can you live with a stolen token you cannot revoke? A self-contained JWT works until exp, so keep it to minutes; our example uses 15. The cost is refresh traffic: an 8-hour working session needs 32 access tokens, so 31 refresh calls after the login, which is trivial load for a token endpoint. If you truly need instant revocation, use opaque access tokens that the API checks with the authorization server (token introspection, RFC 7662) and pay a lookup per request instead.

Refresh token: how long should a user stay signed in without logging in again? RFC 9700 says refresh tokens "SHOULD expire if the client has been inactive for some time." For browser apps RFC 10017 goes further: the server must either set a maximum lifetime or expire refresh tokens that go unused, must not extend that lifetime when it issues a rotated token, and should tie the refresh token to the user's authenticated session. In practice: an idle timeout plus an absolute limit counted from login.

Short is not free: a refresh token that dies after an hour idle logs people out at lunch. Pick lifetimes from the risk, then make sure the refresh path is fast.

Refresh token rotation and reuse detection

Rotation means every refresh returns a new refresh token and retires the old one. RFC 6749 already allows it (the server "MAY issue a new refresh token, in which case the client MUST discard the old refresh token"), and RFC 9700 makes it one of two required options for public clients such as single-page and mobile apps: their refresh tokens "MUST be sender-constrained or use refresh token rotation."

The useful part is what happens when an old token comes back. The server keeps the retired tokens linked to the live one. If a retired token is presented, either an attacker or the real client is replaying it, and the server cannot tell which. So, per RFC 9700 section 4.14.2, it "will revoke the active refresh token", ending the whole chain and forcing a new login.

Timeline from 09:00 to 10:00: access tokens AT1, AT2 and AT3 each last 15 minutes; refresh tokens RT1, RT2 and RT3 rotate at 09:15 and 09:30; at 09:40 an attacker replays the retired RT1, the server revokes RT3, and the user's refresh at 09:45 fails so they sign in again
Timeline from 09:00 to 10:00: access tokens AT1, AT2 and AT3 each last 15 minutes; refresh tokens RT1, RT2 and RT3 rotate at 09:15 and 09:30; at 09:40 an attacker replays the retired RT1, the server revokes RT3, and the user's refresh at 09:45 fails so they sign in again

Two details matter. AT3 keeps working until 09:45 after the chain is revoked, because it is a self-contained JWT: that is the window your access token lifetime buys. And rotation needs care on the client: if two browser tabs refresh at the same moment with the same token, the second request looks exactly like a replay. Serialize refreshes in one place (a single worker, a lock, or the backend) so only one request uses each refresh token.

The other option is sender-constraining: the token is bound to a key the client holds, using DPoP (RFC 9449) or mutual TLS (RFC 8705), so a stolen copy is useless without that key. RFC 9700 recommends it for access tokens too, which closes the AT3 window for a thief.

Where to store each token

The rule is to keep refresh tokens out of reach of page JavaScript. Server-side apps keep both tokens on the server, tied to the session cookie. For single-page apps, RFC 10017 strongly recommends a Backend for Frontend (BFF) for business and sensitive applications: a small server component holds the tokens and the browser gets only an HttpOnly, Secure session cookie. Mobile apps use the platform's protected storage. RFC 10017 walks through the other browser storage choices and their trade-offs in full.

Wherever they live, never put an access token in a URL. RFC 9700 says clients "MUST NOT pass access tokens in a URI query parameter", because URLs end up in browser history, server logs and Referer headers.

Revocation and logout

Logging out should revoke the refresh token at the authorization server (the RFC 7009 revocation endpoint), not just delete it from the client; servers that can should then invalidate access tokens from the same grant too. RFC 9700 adds that servers may revoke refresh tokens automatically on security events such as a password change or logout at the authorization server. Remember the gap: a self-contained access token already in flight stays valid until exp unless your APIs introspect or check a deny-list keyed by jti.

This post covers the OAuth 2.0 specifications and general practice. If you run an identity provider, handle payments or health data, or must meet a profile such as FAPI, follow that profile and have the design reviewed by a security engineer before launch.

Tools for this job

  • JWT Decoder: paste an access token from the network tab to read its kid, aud, scope, iat and exp and see the lifetime the server is really issuing. It decodes only; signature checks belong in your API.
  • JWK Tool: paste the authorization server's JWK Set to get each key's RFC 7638 thumbprint and PEM, so you can match a token's kid to a key and debug "no matching key" errors during signing-key rotation.
  • Epoch Converter: turn iat, exp and auth_time values into readable dates.

For every registered claim and the classic JWT attacks, read How JWTs Work: Decoding Headers, Payloads and Claims. For JWK Sets, PEM conversion and thumbprints in depth, see JSON Web Keys (JWK) Explained: Formats, JWKS and Thumbprints, and for the other half of a secure login, How TOTP 2FA Codes Work: RFC 6238 Step by Step.

Frequently asked questions

Can I use a refresh token to call my API directly?

No. RFC 6749 says refresh tokens are never sent to resource servers. The API should only ever accept access tokens; a refresh token goes to the authorization server's token endpoint with grant_type=refresh_token and comes back as a new access token (and, with rotation, a new refresh token).

Should refresh tokens be JWTs?

They can be, but there is little to gain. The client is not meant to read them, and the authorization server has to look them up anyway to check rotation and revocation. If a server encodes the grant in the token itself, RFC 9700 says it must protect the token's integrity, for example with a signature.

Why does my user get logged out when they open two tabs?

With rotation, two tabs refreshing at the same time both send the same refresh token. The first succeeds and retires it; the second presents a retired token, which looks like theft, so the server revokes the chain. Run refreshes through a single shared place, such as one worker or your backend, so each refresh token is used once.

Sources and further reading

Browse all tools