Refresh tokens
Apps stay signed in without asking the user again, and, with rotation, a stolen refresh token is caught the moment it is used twice.
AvailableAPIRFC 6749 §6, RFC 9700 §4.14
An access token lasts minutes. A refresh token lets the app get a new one without sending the user back to sign in. Only the client it was issued to can use it, and it can narrow what it asks for but never widen it.
A refresh token comes with a browser or device sign-in, never
with client credentials. With OpenID Connect it comes only when the user allowed
offline_access. With rotation, each use returns a new refresh token, and a copy used a second time ends the
whole family, so a stolen token buys almost nothing. Reuse is caught only with rotation: public apps always rotate, but a
confidential app reuses its refresh token unless you set rotate.
Before you begin
Section titled “Before you begin”- Who can do it: a realm administrator (a group holding
RealmOwner) lets a client hold refresh tokens and sets the lifetimes;RealmViewercan read them. The app then refreshes with its own credentials. - Where: today this is done through the API (Call the API shows how to get
$CVYUH_SESSION). - First: the app signs users in with authorization code or device authorization.
How it works
Section titled “How it works”The app sends the refresh token to the token endpoint. A public app sends its client_id; a confidential one proves
itself as for any grant (client authentication):
curl https://foobar.cvyuh.com/oauth2/token \ -d grant_type=refresh_token \ -d client_id=team-wiki \ -d refresh_token=eyJhbGciOi…{ "access_token": "eyJhbGciOiJFUzI1NiIs…", "token_type": "Bearer", "expires_in": 300, "scope": "openid profile offline_access", "refresh_token": "eyJhbGciOi…", "id_token": "eyJhbGciOiJSUzI1NiIs…"}To ask for less, the app adds scope or resource naming part of what it was granted. While openid is still
granted, the answer carries a new ID token too.
Rotation, and catching a stolen token
Section titled “Rotation, and catching a stolen token”With rotation, each use spends the refresh token and returns a new one. If a spent token is ever used again, someone has a copy. The realm then ends the whole family: every refresh token descended from the same sign-in, and the access tokens they bought.
- Public apps always rotate, because they cannot keep a secret.
- A public app’s family is bound to its DPoP key when its first token was DPoP-bound (DPoP). Every refresh must then carry a proof by the same key.
- Confidential apps choose, and default to
reuse. They prove who they are on every use, so a copied token is useless without the client’s credentials. Withreuse, the same token works until its family ends and a second use is never caught; setrotateto have it caught. - A retry window lets an app that lost an answer present the same token again for a few seconds. It is off by default and capped at a minute, because inside it whoever presents the token is answered.
Set it up
Section titled “Set it up”These steps use the admin API; Call the API shows how to get a session.
- Allow the client to hold refresh tokens: add
refresh_tokento itsgrantTypesinhttps://foobar.cvyuh.com/api/v0/am/objects/v1/oauth2Client, and chooserefreshTokenRotation. Read the client first withGET, and send back the whole list with the new value: a list you send replaces the client’s list. - Set the realm’s lifetimes with
PATCH https://foobar.cvyuh.com/api/v0/am/objects/v1/oauth2Settings/oauth2Settings.
| Where | Setting | Default | What it does |
|---|---|---|---|
| Client | grantTypes includes refresh_token | The client may hold refresh tokens | |
| Client | refreshTokenRotation | reuse | rotate catches a reused token; public clients always rotate |
| Realm | refreshTokenIdleSecs | 3600 (1 hour) | Unused this long, the token stops working |
| Realm | refreshTokenLifetimeSecs | 86400 (1 day) | The family ends this long after the sign-in, however often it is used |
| Realm | refreshTokenRetrySecs | 0 (off) | The retry window |
| Realm | refreshTokenGrants | browser and device sign-in | The grants that come with a refresh token |
What the user sees
Section titled “What the user sees”Nothing, while refreshes succeed: the app stays signed in. When a family ends (its lifetime is up, it sat unused past the idle limit, the session ended for an online family, or a reuse was caught), the app’s next refresh is refused and the app sends the user to sign in again.
Limits and defaults
Section titled “Limits and defaults”| Setting or limit | Default | Range |
|---|---|---|
refreshTokenLifetimeSecs: the family’s life from sign-in | 86,400 (one day) | 1 to 31,536,000 (a year) |
refreshTokenIdleSecs: unused this long, it stops | 3,600 (one hour) | 0 (no idle limit) to 31,536,000 |
refreshTokenRetrySecs: the retry window | 0 (off) | 0 to 60 |
refreshTokenRotation | reuse; always rotate for a public client | rotate or reuse; a fapi2 client is refused rotate |
refreshTokenGrants | browser and device sign-in | any of the realm’s grants that sign a user in |
Troubleshooting
Section titled “Troubleshooting”| Symptom | Cause | Fix |
|---|---|---|
| Users are sent to sign in again every hour | The app doesn’t refresh within refreshTokenIdleSecs | Refresh on each app start, or raise the idle limit |
| A rotating app is signed out at random | Two tabs or threads refreshed with the same token: the second use looked like a theft and ended the family | Refresh from one place, or set a short refreshTokenRetrySecs |
| No refresh token comes back | The client lacks refresh_token in grantTypes, or the user didn’t allow offline_access with OpenID Connect | Add the grant, and ask for offline_access |
| A copied token wasn’t caught | The client uses reuse | Set refreshTokenRotation to rotate |
Audit record
Section titled “Audit record”Each refresh is a TOKEN_ISSUED record with grantType refresh_token. A caught reuse is a TOKEN_ISSUED record
with result FAILED, keyed by the client, with error invalid_grant and reason “the refresh token was already
used”; the family it ended is revoked with it. See them in Audit → Authentication, or read the user’s trail
(Read a user’s or agent’s trail).
Good to know
Section titled “Good to know”- A refresh never widens. A scope or resource the family wasn’t granted is refused.
- The user must still be there. A refresh is refused once the user can no longer be issued tokens, or once they took the app back on their Authorized Apps page (consent).
- An online family ends with the session. Unless the user allowed
offline_access, a refresh is refused once the session they signed in with has ended. See Choose online or offline access. - A refresh token is redeemed only at the issuer that gave it. With no base URL, each of the realm’s host names is its own issuer.
Reference
Section titled “Reference”Errors
Section titled “Errors”| Error | When |
|---|---|
invalid_request | No refresh_token is sent |
invalid_grant | The token is unknown, expired or another client’s; it was issued under another of the realm’s issuers (another host name, when no base URL is set); it was already used; its family was ended; the DPoP proof is by another key, or an attesting client’s instance is another; or the user, their session or their consent is gone |
invalid_scope | A scope the family wasn’t granted |
invalid_target | A resource the family wasn’t granted, or none the client may still name |
Related
Section titled “Related”- Keep apps signed in: lifetimes and rotation for an app.
- Choose online or offline access: which tokens end with the session.
- Logout: what a sign-out does to refresh tokens.
- Revoke tokens: revoke a refresh token and its family on purpose.