The refresh token grant
An 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. is meant to be short-lived, but some applications need to keep working for a person long after the sign-in page has closed. A refresh token lets a client get new access tokens for an 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. the person already gave, without sending them through the flow again.
Access that outlasts a visit
The printer now offers a yearly calendar. Ava connects her photo account once, and every December the printer picks twelve recent photos from her albums and sends her a proof. Ava won't be at her browser in December when the printer needs those photos.
The access token from the code exchange can't cover that gap. It lasted ten minutes, and short lifetimes are deliberate: if a token leaks, it is only useful briefly. The printer needs a way to get new access tokens months later without asking Ava to approve the connection again.
A refresh token is a credential a client uses to get new access tokens for an authorization it already has. It goes only to 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.'s 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., never to the photo 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.. The access token is what the photo API sees; the refresh token is what lets the printer ask for another one.
This time, when the printer exchanged its authorization codeA short-lived, single-use value that the authorization server sends back to a client through the browser. The client swaps it for tokens in a direct request to the authorization server; APIs never accept it., the photo service issued a refresh token alongside the access token:
{
"access_token": "demo-access-token-8",
"token_type": "Bearer",
"expires_in": 600,
"refresh_token": "demo-refresh-token-3",
"scope": "photos.read"
}All domains, credentials and tokens in these examples are fictional.
Whether a refresh token is issued is the authorization server's decision, not the client's. Some services issue one only when the client asks for continued access. A common way to ask is the offline_access scope, which OpenID ConnectAn identity layer on top of OAuth 2.0 that lets an application sign people in with an account at another service. It adds the ID token, standard claims, and discovery to the OAuth exchange. defines: "offline" means the person doesn't have to be present when the client uses the access. Because it changes what Ava is agreeing to, the consent screen usually says so, with a line such as "Keep access when you are not using the app." Other services decide by the kind of client or by their own policy.
The printer's backend stores the refresh token with Ava's connection record and protects it like any other long-lived secret, because it is the part that carries her approval forward. Each access token is used for a short while and then discarded.
Refreshing an access token
In December, the printer's backend sends the refresh token to the token endpoint with grant_type=refresh_token. There is no browser, no redirect, no authorization code and no PKCEProof Key for Code Exchange, pronounced pixie: the client creates a secret for each authorization attempt, sends only a hash of it at the start, and proves it holds the secret when redeeming the code, so a stolen authorization code is useless to anyone else. verifier this time. The printer is 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., so it authenticates with its client ID and secret in the Authorization header, as it did during the code exchange:
POST /token HTTP/1.1
Host: auth.photos.example
Authorization: Basic cGhvdG8tcHJpbnRlcjpkZW1vLW9ubHktbm90LWEtcmVhbC1zZWNyZXQ=
Content-Type: application/x-www-form-urlencoded
grant_type=refresh_token
&refresh_token=demo-refresh-token-3The authorization server checks four things before it answers. The client must authenticate. The refresh token must have been issued to this client. The refresh token must still be valid. And the authorization behind it must still exist: if Ava had disconnected the printer from her photo account, the token would no longer be honored.
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. matters here because it ties the refresh token to the printer. Someone who copied the refresh token out of the printer's database still couldn't use it without the printer's secret.
The request can include a scope parameter to ask for less than the original grant, for example a token for one purpose only. It can't ask for more. A refresh token carries forward what Ava approved, and broader access needs a new authorization request where she can say yes or no.
A successful response returns a new access token, which the printer uses at the photo API exactly as before.
When the refresh token is replaced
The response may also contain a different refresh token:
HTTP/1.1 200 OK
Content-Type: application/json
Cache-Control: no-store
Pragma: no-cache
{
"access_token": "demo-access-token-9",
"token_type": "Bearer",
"expires_in": 600,
"refresh_token": "demo-refresh-token-4",
"scope": "photos.read"
}As with the code exchange, Cache-Control: no-store and Pragma: no-cache tell anything between the server and the printer not to keep a copy of a response that holds tokens.
This is refresh token rotation: each refresh token works once, and each successful refresh hands the client its replacement. The printer stores demo-refresh-token-4 in place of demo-refresh-token-3 before it relies on the new access token. When a response carries no replacement, the printer keeps the refresh token it already has.
Rotation gives the server a way to notice theft. If an old refresh token comes back after it has been replaced, either the printer has a bug or someone else has a copy, and the server can't tell which. A cautious server refuses it and may revokeCancelling a credential, token, certificate, or authenticator before it would expire, so that it is no longer accepted. Whether every copy stops working at once depends on how services check it. the whole chain of refresh tokens that grew from Ava's authorization. That is why the printer refreshes a connection from one worker at a time, and why it treats a lost refresh response with the same caution as a lost code exchange: resending the old refresh token is not a safe retry.
Refresh tokens and rotation follows the server's side: what it keeps, and what it revokes when a used token returns.
When refresh stops working
Refresh tokens don't last forever. They can expire, go unused for too long, or be revoked because Ava disconnected the printer, changed a security setting, or the service detected reuse. The token endpoint then answers with invalid_grant, the same error a used or expired authorization code produces.
The printer can't repair that on its own. Retrying the same refresh token won't help, and it has no other way to get access Ava hasn't approved again. The right response is to mark the connection as needing attention, then ask Ava to reconnect the next time she visits, or send her a notice that December's calendar is waiting for her photos.
The printer's secret protects its refresh tokens, but 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., such as an application running entirely in the browser, has no secret to prove who is presenting a refresh token. Current security guidance requires servers to protect those refresh tokens another way: by rotating them, or by binding each one to a key the client holds, so a copy is useless without that key. Token revocation and Refresh token theft look at how stolen refresh tokens are used and contained.
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
- Keep the printer connected with a refresh token, and know when to ask againGive the printer a refresh token, let its access token expire, refresh and save the replacement, narrow but never widen, and see refresh stop when Ava disconnects.