# 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.

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](/platform/realms/delegated-administration/)). `RealmViewer` can read the settings but
  not change them.
- **Where:** today this is done through the API; [Call the API](/develop/api/) shows how to get `$CVYUH_SESSION`.
- **First:** register the app, as in [Sign users in to a web app](../../sign-in-to-apps/sign-in-web-app/).

## Set it up

<Steps>

1. Let the app hold refresh tokens: its `grantTypes` include `refresh_token`. With OpenID Connect, a refresh token
   comes only when the user allows `offline_access`: see
   [Choose online or offline access](../online-and-offline-access/).

2. Choose rotation for a confidential app. A public app always rotates.

   ```sh
   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](../../services/fapi2-clients/).

3. Optionally, change the lifetimes for the realm. These are the defaults:

   ```sh
   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 }'
   ```

   `refreshTokenRetrySecs` lets 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.

</Steps>

## 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

| 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

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

| 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

`PATCH` the client with `{ "refreshTokenRotation": "reuse" }`, and the realm's lifetimes back to the values above.

## Related

- [Refresh tokens](../../../applications/single-sign-on/refresh-tokens/): families, rotation and every setting.
- [Choose online or offline access](../online-and-offline-access/): which access ends with the session.
- [Revoke a token](../../../../gateway/guides/revoke-a-token/): end a family by hand.
