Refresh token theft
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 whole grant and lasts for months, so a copy is worth far more than a copied 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.. This lesson follows copies of the printer's refresh tokens for Ava's photos, what client authenticationChecking evidence that someone, or some application, controls an account, such as a password or a passkey. It establishes which account is being used, not what that account may do. and rotation do about them, where rotation leaves a gap, and how a theft is contained.
Why refresh tokens are worth stealing
The copied access tokenA piece of data issued by a trusted service that a client presents as a credential, for example to call an API or to report a sign-in. Some tokens carry readable claims; others are random strings that only the issuer can interpret. in Token theft and replay stopped working within minutes. A refresh token is built to last. demo-refresh-token-4 is what lets the printer make Ava Archer's December calendar without her, and anyone else holding it could draw on the same 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..
A refresh token represents the whole authorization a person gave a client, not one request to one APIAn application programming interface: a defined way for software to interact with a service. A web API offers endpoints where other programs send HTTP requests.. It often lasts months, it needs no person to be present, and every use can produce a fresh access token with the grant's full scope. Current guidance calls refresh tokens an attractive target for exactly these reasons.
They are also kept for a long time, which gives an attacker time to find them. The printer's website keeps them, encrypted, in connection records in its database, as Server-rendered web applications described. The phone app keeps them on the device, where a careless backup or a compromised phone can expose them. A browser application that holds one puts it within reach of injected script.
What a thief can do with a copy depends on whether the client it belongs to can authenticate.
When the thief also needs the client
All domains, credentials, and tokens in these examples are fictional. Suppose an attacker gets far enough into the printer's systems to copy and decryptTransforming data so that only someone holding the right key can read it. It protects confidentiality and, unlike hashing, is designed to be reversed by the key holder. its connection records, and tries one of the refresh tokens at 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.:
POST /token HTTP/1.1
Host: auth.photos.example
Content-Type: application/x-www-form-urlencoded
grant_type=refresh_token
&refresh_token=demo-refresh-token-4
&client_id=photo-printerThe photo service issued that token to photo-printer, a confidential clientAn OAuth client, typically running on a backend, that can keep a credential such as a client secret or private key protected, and so can authenticate itself to the authorization server.. The original specification requires 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. to keep that binding and to check it, with client authentication, whenever the client can authenticate. This request carries no client credential, so the token endpoint answers invalid_client, and the refresh token alone is useless.
That is why the printer keeps its client credential somewhere other than its connection records, such as a secrets store, and why a private keyThe half of a key pair that its owner keeps to themselves and uses to create signatures or decrypt data. Anyone who obtains it can act as its owner. held in a key management service helps further: it cannot be copied along with the database at all. A breach that reaches both turns into the incident Rotating client credentials described. The credential is revoked first, then the tokens issued with it are reviewed.
Racing under rotation
The printer's phone app, photo-printer-app, is a public clientAn OAuth client, such as code running in a browser or an app on a phone, that cannot keep a credential secret because the people using it can inspect it. It relies on protections such as PKCE instead of a client secret.. It has no credential, so a copied refresh token is all a thief needs. For public clients, current guidance requires the authorization server either to bind refresh tokens to the client instance or to rotate them. With rotation, which The refresh token grant introduced, the server watches for one thing: a refresh token that has already been used, presented again.
Say the app on Ava's phone holds demo-app-refresh-token-1, and a thief copies it from a phone backup. What happens next depends on who uses it first.
| Who refreshes first | What the server sees | Outcome |
|---|---|---|
| The thief | The thief receives demo-app-refresh-token-2. Later, the app presents demo-app-refresh-token-1, which was already used. | Reuse detected. The server revokes the family, including the thief's replacement. The thief keeps at most an access token until it expires, and Ava reconnects the app. |
| The app | The app receives demo-app-refresh-token-2. The thief then presents demo-app-refresh-token-1. | Reuse detected. The family is revoked, so the thief gets nothing, and the app must reconnect too. |
| The thief, while the app stays unused | An unbroken chain: token 1, then 2, then 3, each used once. | Nothing looks wrong. The thief keeps rotating until the app returns, the family reaches its absolute lifetime, or something else ends the grant. |
In the first two rows, the server cannot tell who presented the old token. It revokes everything, which stops the thief at the cost of one reconnection for Ava. A server that allows a short grace period after each rotation misses a reuse inside those few seconds, the trade-off Refresh tokens and rotation weighed.
The third row is the gap. Rotation notices theft only when both parties use the token, and an idle app uses nothing. What ends that chain is the absolute lifetime on every family, which arrives on a fixed date however busily the thief rotates.
Sender-constraint closes the gap more directly. If the app's refresh tokens are bound to a key kept in the phone's secure hardware, a copy taken from a backup cannot be used at all, because the key was never in the backup. Advanced OAuth's DPoP lessons show how a public client binds its tokens this way.
Containing a theft
When the server detects reuse, or anyone suspects a theft, the response follows the same steps:
- Revoke the family, and the access tokens issued from the same grant where the server can reach them, as Token revocation described.
- Record a security event naming the client, the grant, the time, and the networks involved, but never the token values.
- Tell the person plainly, for example: "We disconnected Photo Printer from your photo account because one of its credentials was used in two places. Reconnect it if you still use it."
- Require reconnection. The client's next refresh fails with
invalid_grant, and, as The refresh token grant described, it marks the connection as needing attention and asks Ava to connect again.
The other events that end a family, from a password reset to a leaked client credential, also contain thefts that nobody has noticed yet: a person who resets their password because something felt wrong should not leave a thief's refresh token working behind them.
Reuse detection depends on legitimate clients never causing it. A client that runs two refreshes for the same connection at once presents a used token just as a thief would, and the server cannot tell the difference. The one-refresh-at-a-time rule from The refresh token grant keeps the signal meaningful, so that when reuse is reported, it can be treated as the security event it usually is.
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
- Race two copies of a refresh token and see what rotation catchesShow a confidential client's refresh token is useless alone, play the lesson's three rotation rows with two copies of a public client's token, and contain the theft through the alert it raises.