Keep apps signed in
Let apps renew their access without asking users to sign in again, choose how refresh tokens rotate, and catch a stolen one.
APIRFC 6749, RFC 9700
An access token lives five minutes by default, and most users stay longer. With a refresh token, the app swaps it for a fresh access token in the background, and the user keeps working without being sent back to sign in. You choose whether a confidential app’s refresh tokens rotate, and how long they last.
Before you start:
- Who can do it: a realm administrator, in a group that holds
RealmOwner(Delegated administration).RealmViewercan read the settings but not change them. - Where: today this is done through the API; Call the API shows how to get
$CVYUH_SESSION. - First: register the app, as in Sign users in to a web app.
Set it up
Section titled “Set it up”-
Let the app hold refresh tokens: its
grantTypesincluderefresh_token. With OpenID Connect, a refresh token comes only when the user allowsoffline_access: see Choose online or offline access. -
Choose rotation for a confidential app. A public app always rotates.
Terminal window curl -X PATCH 'https://foobar.cvyuh.com/api/v0/am/objects/v1/oauth2Client/<client-id>' \-H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \-H "Content-Type: application/json" \-d '{ "refreshTokenRotation": "rotate" }'A client held to FAPI 2.0 is refused
rotate: its refresh tokens are not rotated. See Hold a client to FAPI 2.0. -
Optionally, change the lifetimes for the realm. These are the defaults:
Terminal window curl -X PATCH https://foobar.cvyuh.com/api/v0/am/objects/v1/oauth2Settings/oauth2Settings \-H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \-H "Content-Type: application/json" \-d '{ "refreshTokenIdleSecs": 3600, "refreshTokenLifetimeSecs": 86400,"refreshTokenRetrySecs": 0 }'refreshTokenRetrySecslets an app that lost an answer use the same token again within that many seconds, up to 60. Anyone holding the token is answered inside the window, so leave it at 0 unless an app needs it.
What the app does
Section titled “What the app does”It posts grant_type=refresh_token to https://foobar.cvyuh.com/oauth2/token, authenticating as it did for the code,
and gets a new access token. With rotation, it also gets a new refresh token, and must keep only the newest. It may
ask for fewer scopes or APIs than it was granted, never more.
A refresh token unused for an hour stops working. However much it is used, the whole chain ends a day after the sign-in, and the user signs in again. If the user removed the app, the account was deactivated, or the client was turned off, the refresh is refused.
When a refresh token is stolen
Section titled “When a refresh token is stolen”| The app… | Rotation | What a thief with a copy can do |
|---|---|---|
| is public (browser, mobile) | Always on | Use it once at most. The next use of either copy ends the whole family, the thief’s and the app’s |
is confidential, set to rotate | On | The same, and the thief also needs the client’s own credentials |
is confidential, set to reuse | Off | Nothing without the client’s credentials. End the family by revoking the token |
When the family ends, the app’s next refresh fails and the user signs in again. That is the signal that something is
wrong. Reuse is caught only where tokens rotate: a confidential app left at reuse, the default, never trips it.
Check it worked
Section titled “Check it worked”Sign in to the app, wait for its access token to expire, and use it again: the app should refresh without sending you
to sign in. Under rotation, the token response carries a new refresh_token each time.
Every refresh is on the user’s trail as a TOKEN_ISSUED record with grantType refresh_token. A reuse that ended a
family is a TOKEN_ISSUED record with result FAILED, under the client’s ID, with error invalid_grant and
reason “the refresh token was already used”. Find them in Audit → Authentication in the dashboard.
Troubleshooting
Section titled “Troubleshooting”| Symptom | Cause | Fix |
|---|---|---|
| Users are sent to sign in after an hour away | The refresh token was idle past refreshTokenIdleSecs | Raise it, up to the family’s lifetime |
| Users are sent to sign in once a day, however active | The family reached refreshTokenLifetimeSecs | Raise it, or accept a daily sign-in |
invalid_grant, “already used”, with no theft | Two tabs or workers refreshed with the same rotating token at once | Have the app share one refresh at a time, or set refreshTokenRetrySecs to a few seconds |
| No refresh token comes back | The app lacks refresh_token in grantTypes, or the user didn’t allow offline_access | See step 1 |
Setting rotate is refused | The client is held to FAPI 2.0 | Leave it at reuse |
Undo it
Section titled “Undo it”PATCH the client with { "refreshTokenRotation": "reuse" }, and the realm’s lifetimes back to the values above.
Related
Section titled “Related”- Refresh tokens: families, rotation and every setting.
- Choose online or offline access: which access ends with the session.
- Revoke a token: end a family by hand.