What a refresh token is for
Access tokens are meant to be short-lived. A stolen access token is useful to an attacker only until it expires, so servers keep that window small. The catch is that the user should not have to sign in every few minutes.
A refresh token solves this. It is a longer-lived credential that the client keeps and sends only to the token endpoint, never to an API. When the access token runs out, the client trades the refresh token for a new one. RFC 6749 defines the refresh grant in section 6.
The refresh token is not shown to resource servers, and the user does not see the exchange. To the user, the session simply keeps working.
How the refresh grant works
The client sends a POST to the token endpoint with grant_type=refresh_token and the token. A confidential client authenticates the same way it did for the original grant. A public client sends its client_id.
POST /realms/acme/protocol/openid-connect/token HTTP/1.1
Host: auth.example.com
Content-Type: application/x-www-form-urlencoded
Authorization: Basic d2ViLWFwcDpzYW1wbGUtc2VjcmV0LW5vdC1yZWFs
grant_type=refresh_token&refresh_token=SAMPLE_REFRESH_TOKENThe response has the same shape as the first token response: a new access_token, its expires_in, and often a new refresh_token. If the original request had the openid scope, a new ID token may come back as well.
{
"access_token": "SAMPLE_NEW_ACCESS_TOKEN",
"token_type": "Bearer",
"expires_in": 300,
"refresh_token": "SAMPLE_NEW_REFRESH_TOKEN",
"scope": "openid profile email"
}The optional scope parameter
You may send scope to ask for less than the original grant allowed. RFC 6749 says the requested scope must not include anything the user did not grant before. It can narrow a token for one API call; it cannot widen it.
What this tool does and does not do
The builder writes the refresh request for you as a curl command and as raw HTTP. It handles the endpoint, the grant parameters and client authentication.
- It builds the Keycloak token endpoint from a server URL and realm, or takes a custom URL.
- It supports public clients,
client_secret_basic,client_secret_postand a pasted signed JWT. - It adds
scopeonly when you fill it in.
It does not send the request and cannot tell whether your refresh token is still valid. It does not decode the token either. Keycloak refresh tokens are JWTs you can read with the JWT decoder, but other servers may issue opaque strings.
How to use the builder
- Enter your Keycloak server URL and realm, or a custom token endpoint.
- Paste the refresh token in place of the sample.
- Narrow the scope if you need to; leave it empty otherwise.
- Pick the same client authentication your app used for the original grant.
- Copy the curl command, run it, and keep the new refresh token from the response.
Where refresh tokens fit
Server-side web apps
A confidential client keeps the refresh token on the server, next to the user’s session. When an API call fails because the access token expired, the server refreshes and retries. The browser never sees either token.
Single-page apps
A browser app is a public client. RFC 9700 says refresh tokens for public clients must be sender-constrained or rotated, so a stolen one cannot be reused quietly. Keep the token in memory, and consider a back end for front end that holds tokens on a server instead.
Mobile and desktop apps
Native apps keep the refresh token in the platform’s secure storage, such as the iOS Keychain or Android Keystore-backed storage. Refreshing lets the app stay signed in across launches without asking for the password again.
Offline access
Some apps need to act for the user while the user is away, such as a sync job. The offline_access scope asks for a refresh token that keeps working after the user’s session ends. Request it only when you truly need it.
How Keycloak handles refresh tokens
In Keycloak the refresh request goes to https://<host>/realms/<realm>/protocol/openid-connect/token, the same endpoint as the first token request.
- Keycloak returns a new refresh token with each refresh. Keep the newest one.
- A refresh token is tied to the user’s session. When the session ends, by idle timeout, maximum lifetime or logout, the refresh token stops working.
- With the realm setting Revoke refresh token on, each refresh token can be used only once, unless Refresh token max reuse allows more.
- An offline token, from the
offline_accessscope, survives the end of the session. The user needs theoffline_accessrole, which new users get by default. - Keycloak refresh tokens are JWTs with
typset toRefresh. Treat them as secrets all the same.
When to refresh
There are two common strategies, and many apps use both.
Before the token expires
The token response says how long the access token lasts in expires_in. Record the time you received it and refresh a little before it runs out, for example when less than a tenth of the lifetime is left. This avoids failed API calls, but it refreshes even when the user is idle.
When an API says the token expired
An API that rejects an expired token answers 401 with error="invalid_token" in the WWW-Authenticate header (RFC 6750). Refresh once, then retry the call once. If the retry also fails, the problem is not expiry, so stop and surface the error.
Clock differences
Do not compare the token’s exp claim with your own clock alone. A few seconds of drift between machines is normal. Working from expires_in and a margin is more reliable.
Rotation and reuse
Refresh token rotation means every refresh returns a new refresh token and the old one stops working. If an attacker steals a token and uses it, the real client’s next refresh fails, or the other way around. Either way the theft shows up.
With rotation on, two refreshes in parallel can break each other: both send the same old token, and one of them gets invalid_grant. Serialize refreshes in your app, for example behind a single in-flight promise or a lock.
Common errors and how to fix them
| Error | Usual cause | Fix |
|---|---|---|
invalid_grant, Token is not active | The refresh token expired. | Send the user through sign-in again. |
invalid_grant, Session not active | The session behind the token ended: idle timeout, maximum lifetime or logout. | Sign in again. Raise session timeouts if users hit this too often. |
invalid_grant after a retry | Rotation is on and the token was already used. | Store the new token from every response and avoid parallel refreshes. |
invalid_client | Wrong client id or secret, or no client authentication sent. | Refresh with the client the token was issued to, using its current secret. |
invalid_scope | The scope asks for more than the original grant. | Leave scope out or ask only for a subset. |
The OAuth error lookup covers the rest of the codes.
Security notes
RFC 6749 section 6 says a refresh token belongs to the client it was issued to, and the server must check that. A confidential client authenticates on every refresh.
RFC 9700 asks servers to protect refresh tokens for public clients with sender-constraining, such as DPoP, or with rotation. It also suggests that refresh tokens can expire when they have not been used for some time.
Never send a refresh token to an API, never log it, and never put it in a URL.
Best practices
- Refresh shortly before the access token expires, or when an API answers 401.
- Replace the stored refresh token with each new one.
- Make one refresh at a time per session.
- Ask for
offline_accessonly for real background work. - On
invalid_grant, stop retrying and start a fresh sign-in.