Beta

Create a tenant

A new tenant starts with its own users, OAuth settings, audit history and logs. You are its first Tenant Admin.

Refresh tokens and rotation

A refresh token carries a person's approval forward for months, so the authorization server 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 endpoint 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 authorization 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: active

The refresh tokens themselves are not in the record. OAuth leaves token storage to each authorization server, and the photo service keeps only a SHA-256 hash of each one, written here in base64url 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.

An idle family ends at its sliding deadline. A busy one, however often it refreshes, still ends at the absolute deadline.

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:

  1. Authenticate the client, here photo-printer with its client secret.
  2. Hash the presented token and look for it among this client's refresh tokens.
  3. Check that its family and grant are still active and that neither deadline has passed.
  4. If the hash matches a used token rather than the current one, stop: this is reuse, which ends the family.
  5. If the request includes a scope, check that it falls within the grant, and refuse anything broader with invalid_scope. The new access token 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.
  6. 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 JWT access tokens already issued stay usable until they expire at APIs that validate them locally, as Token formats and validation explained.

Each refresh replaces the token in one step and moves the idle deadline forward. When a replaced token comes back, the whole family ends, and the printer has to ask Ava to reconnect.

Reuse is not the only reason a family should end. These events should end it too, and some should end the grant itself:

EventWhat ends
Ava removes the printer from her connected applications at the photo serviceThe 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 everywhereFamilies for her grants, as the service's policy decides. Current guidance allows revocation on security events like these.
The photo service suspends photo-printerEvery family issued to that client.
The printer's client secret leaksDepending 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 againThat 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.

A refresh token family has a 90-day idle lifetime that moves forward with each refresh, and no other limit. What is the risk? 1 of 3

Choose one answer.

Two refresh requests carrying the same current refresh token arrive at the same moment. What stops both from receiving replacement tokens? 2 of 3

Choose one answer.

The photo service stores only a SHA-256 hash of each refresh token. An attacker copies the whole table of token families. What can they present at the token endpoint? 3 of 3

Choose one answer.

Try it in the Lab

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.

  1. The Replayed Refresh Token Difficulty 3 of 5 · About 35 minutes · API target

We value your privacy

We use cookies and similar technologies to enhance your browsing experience, and analytics to understand our traffic. By clicking "Allow all", you consent to optional analytics. Cookie Policy

Learn identity