Refresh tokens and rotation
A refresh tokenA longer-lived credential that a client sends to the authorization server, never to an API, to get new access tokens without asking the person again. carries a person's approval forward for months, so the authorization serverIn OAuth, the service that handles a person's sign-in and approval and decides which tokens to issue to a client. It may belong to the same company as the API, but its job is separate. has to decide what it keeps about each one, how long it may work, how to replace it exactly once, and when to end it. This lesson follows the photo service through each of those decisions for the printer's calendar connection to Ava's photos.
What the server keeps
In December, the printer sends demo-refresh-token-3 to the token endpointThe authorization server address where clients exchange an authorization code, refresh token, or other grant for tokens, in a direct request rather than through a browser redirect. and receives demo-access-token-9 and demo-refresh-token-4 in return. The refresh token grant followed that exchange from the printer's side: send the current refresh token, save its replacement, and never run two refreshes for one connection at once. On the photo service's side, the same exchange is a small database update with large consequences. All domains, accounts, and tokens in these examples are fictional.
When Ava approved the printer, the photo service recorded a grant: the standing authorizationDeciding whether a requested action is permitted, such as whether a signed-in account may delete a particular photo. Authentication says which account is asking; authorization decides what it can do. that Ava, whose account is user-2048, gave the client photo-printer for photos.read. The refresh tokens it issues hang from that record. After the December refresh, a simplified view of the server's records looks like this:
Grant
Subject: user-2048
Client: photo-printer
Scope: photos.read
Approved: 2026-10-01 09:00 UTC
Status: active
Refresh token family
Grant: the grant above
Started: 2026-10-01 09:00 UTC
Current token (SHA-256): _ipGaH26D00Ly1DMySWKcx37F7ecbArpfNZxIajZX9Y
Used tokens (SHA-256): STpHwz_fPJDreVsTgq-71T-jNb--CPNg4hjn_2T_RBg
Last used: 2026-12-01 09:00 UTC
Idle deadline: 2028-01-05 09:00 UTC
Absolute deadline: 2028-10-01 09:00 UTC
Status: activeThe refresh tokens themselves are not in the record. OAuthA framework that lets a person give an application limited access to their resources at another service, using tokens instead of sharing their password. OAuth is about access; OpenID Connect adds sign-in. leaves token storage to each authorization server, and the photo service keeps only a SHA-256 hashTurning data into a short, fixed-size value called a hash, in a way that cannot be reversed and where changing one character changes the result completely. Services store password hashes instead of passwords, and signatures are made over a hash. of each one, written here in base64urlA way of encoding data as ordinary text characters so it can travel safely in headers, URLs, and JSON. Anyone can decode it, so it hides nothing; Base64url is a variant safe for URLs, used in JWTs. as in Encoding and hashing. The current entry is the hash of demo-refresh-token-4, and the used entry is the hash of demo-refresh-token-3. When a token arrives, the server hashes it and looks for that hash, so a stolen copy of the database holds nothing that can be presented at the token endpoint: a stored hash sent as a token hashes to something else and matches nothing.
Passwords need slow, salted hashing because people choose guessable ones. A refresh token is a long random value the server generated, so nobody can recover it by guessing, and a single fast hash is enough.
The token family is the chain of refresh tokens descended from one approval, each replacing the one before. Keeping the hashes of used tokens lets the server recognize one if it ever comes back. The client binding matters as much as the hash: the server looks a refresh token up only among those issued to the client presenting it, so a token issued to photo-printer is refused when any other client sends it.
Lifetimes with limits
A refresh token that never expired would carry Ava's approval forward long after she had forgotten giving it. The two deadlines in the family record bound it in different ways.
The idle lifetime ends a family that has gone unused for too long. Current security guidance says refresh tokens should expire after a period without use, since a connection nobody uses is also one nobody is watching, and a token copied from an old phone or a forgotten backup should already be dead by the time anyone finds it. Each successful refresh moves the deadline forward, so this is a sliding lifetime. The photo service allows 400 days, so the December refresh pushed the deadline to 5 January 2028.
A sliding lifetime on its own never ends for a client that keeps refreshing, and that includes a thief who holds the newest token while the real printer sits idle. The absolute lifetime caps the family whatever its activity. This one ends on 1 October 2028, two years after Ava approved, and no refresh token in the family outlives it. Then the printer has to ask Ava to connect again, which is also her moment to decide whether it should still have access.
Refreshes once a year
- Idle deadline
- 2028-01-05 09:00 UTC
- Absolute deadline
- 2028-10-01 09:00 UTC
Then the printer has to ask Ava to connect again
From an old phone or a forgotten backup
- Idle deadline
- Passes unused
- Absolute deadline
- 2028-10-01 09:00 UTC
Already dead by the time anyone finds it
Holds the newest token
- Idle deadline
- Moves forward with each refresh
- Absolute deadline
- 2028-10-01 09:00 UTC
No refresh token in the family outlives it
The numbers are policy, and they can differ by client and by scope.
The numbers are policy, and they can differ by client and by scope. The calendar refreshes once a year, so its idle lifetime must be longer than a year, or the connection would lapse every time. A refresh token that can obtain photos.delete might deserve days rather than months.
Rotating in one step
When the December request arrives, the server works through it in order:
- Authenticate the client, here
photo-printerwith its client secretA shared secret that a confidential client uses to prove itself to the authorization server, much like a password for an application. It belongs on a backend, never in code sent to browsers or phones.. - Hash the presented token and look for it among this client's refresh tokens.
- Check that its family and grant are still active and that neither deadline has passed.
- If the hash matches a used token rather than the current one, stop: this is reuse, which ends the family.
- If the request includes a
scope, check that it falls within the grant, and refuse anything broader withinvalid_scope. The new access tokenA token that a client presents to an API to use access it has been granted, usually limited in what it allows and how long it lasts. It authorizes API calls; it does not tell the client who signed in. gets the narrower scope, but the replacement refresh token keeps the grant's full scope, so a narrow request today does not shrink what the printer can ask for next time. - Replace the refresh token and issue the access token as a single step.
The last step is where implementations go wrong. If the server checks the current token and writes its replacement as separate operations, two requests arriving together can both pass the check. Both receive new refresh tokens, and the family splits into two branches that reuse detection cannot make sense of.
The fix is to make the replacement conditional on the token still being current, inside a database transaction: a group of changes the database applies all together or not at all, so no other request can see or change the rows halfway through. Written in SQL, the language most databases use for queries, the replacement reads:
UPDATE refresh_token_families
SET current_hash = :new_hash,
last_used_at = :now
WHERE family_id = :family_id
AND current_hash = :presented_hash
AND status = 'active';In plain terms: set the family's current hash to the new token's hash, but only if the current hash is still the one this request presented and the family is still active. The values starting with a colon are filled in by the server for each request. If the statement changes one row, this request won. In the same transaction, the server adds the old hash to the family's used tokens, and only after the transaction is saved does it send the new tokens. If the statement changes no rows, another request replaced the token first, and this one is handled as presenting a used token.
A lost response is the hard case. The server saved the rotation and sent demo-refresh-token-4, but the connection dropped and the printer never saw it. The printer still holds demo-refresh-token-3, which is now a used token. If it tries again, strict reuse detection ends the family, and Ava has to reconnect the calendar because of a network fault.
Some servers soften this by accepting the token just replaced for a short grace period, a matter of seconds, instead of treating it as reuse at once. No standard defines such a period, so it is a design choice, and its cost is real: a thief who presents a stolen token inside that window is not detected either. Servers that make this choice keep the period short, apply it only to the token presented by the same client, and still treat anything after the window as reuse.
Ending a family
When a used token comes back outside any grace period, the server cannot tell whether the printer made a mistake or someone else holds a copy, so it revokes the family. The current token stops working too, and the server records a security event against the grant. Where it can, it also invalidates the access tokens the family produced, although JWTA JSON Web Token: a compact token made of Base64url-encoded parts that carries claims and is usually signed. Anyone holding it can decode and read the claims, so the signature shows who issued it but hides nothing. access tokens already issued stay usable until they expire at APIs that validate them locally, as Token formats and validation explained.
Reuse is not the only reason a family should end. These events should end it too, and some should end the grant itself:
| Event | What ends |
|---|---|
| Ava removes the printer from her connected applications at the photo service | The grant and every family under it. Any new connection needs her approval again. |
| Ava's password is reset, her account is recovered, or she signs out everywhere | Families for her grants, as the service's policy decides. Current guidance allows revocation on security events like these. |
The photo service suspends photo-printer | Every family issued to that client. |
| The printer's client secret leaks | Depending on what the logs show, every family issued to that client, since a stolen refresh token plus the secret would keep working. |
| A used refresh token is presented again | That family. |
Each of these reaches the printer the same way: its next refresh fails with invalid_grant, which the refresh token grant lesson explained means asking Ava to reconnect. The server owes the printer no further explanation, but it does owe Ava one: a list of connected applications that shows when each was last used, and a notice when one was cut off because of suspected misuse.
Refresh token theft, in Security and failure cases, follows a thief through the race between a stolen refresh token and the real one, and shows where these limits stop it.
Check your understanding
Try these questions before moving on. If an answer isn't right, use the feedback and try again.
Try it in the Lab
- Rotate a refresh token family, tune its reuse grace, and end itRotate Rhea's refresh token, narrow and come back, let a family pass its idle lifetime, allow a few seconds of grace for a lost response, then end a family from an account event.
Try a Capture the Flag challenge
Apply this lesson against an isolated, intentionally vulnerable application. Capture a flag, explain the mistake, then repair and retest it.