# Sessions

> What a sign-in gives the user, how long it lasts, how many one user may hold, and how a stronger sign-in upgrades it without starting over.

A successful sign-in gives the user a session: the realm's record that they are signed in, how, and since when.
You decide how long a session lasts, how long it may sit unused, and how many one user may hold at once. When an
action needs more than the user's current sign-in, a stronger chain upgrades the same session in place.

## Before you begin

- **Who can do it:** a realm administrator, in a group that holds `RealmOwner`
  ([Delegated administration](/platform/realms/delegated-administration/)). `RealmViewer` can read the settings.
- **Where:** session lifetimes and the quota, today through the API ([Call the API](/develop/api/) shows how to get
  `$CVYUH_SESSION`); the sign-in timeouts, in the dashboard under **Authentication → Settings** or through the API.
  Live sessions are listed and ended through the API ([Session management](../session-management/)).

## How it works

### Signing in

A sign-in is a short conversation at `POST https://foobar.cvyuh.com/api/v0/am/auth/v1/authenticate`. An empty body
starts it, and the realm answers with a page of prompts and an `authId`. Each later request sends the `authId` and the
answers, until the realm answers success or failure. [Call the API](../../../develop/api/) walks through one.

- **The `authId` names one sign-in in progress.** It ends on success, on any failure, and when the sign-in or the
  page times out. To try again, start again.
- **On success the realm gives a new session id.** It never reuses the `authId`. Its cookies are `HttpOnly`,
  `Secure` and `SameSite=Lax`.
- `POST https://foobar.cvyuh.com/api/v0/am/auth/v1/logout` ends the session.

### The session

A session records who signed in, the chain and modules they passed, the level reached, when, and from which address.
It ends `maxSessionMinutes` after sign-in, or after `maxIdleMinutes` without use, whichever comes first.

One user may hold up to `sessionQuota` sessions at once, 10 by default. A sign-in past that ends the user's oldest
sessions until it fits; the new one is never the one ended.

Tokens read the session: `acr` from the chain, `amr` from the modules, and `auth_time` from the sign-in. See
[Step-up and re-authentication](../../applications/single-sign-on/step-up/).

### Making a session stronger

A chain named when a sign-in starts (`?service=<chain>`) runs **only for a user who is already signed in** to the
realm. It knows who they are and when they signed in. When it succeeds, it **upgrades** the session:

- The new session continues the earlier one's sign-in lineage (its `sid`), so apps and tokens tied to it stay valid.
  The lineage ends when its latest session does.
- The audit trail records `SESSION_UPGRADED`, naming both sessions.
- Only the user's own live session can be continued.

This is how an app asks for a stronger sign-in before a sensitive action, and how adding a passkey runs after
sign-in.

## Set it up

Set how long sessions last and how many one user holds:

```sh
curl -X PATCH https://foobar.cvyuh.com/api/v0/am/objects/v1/sessionSettings/sessionSettings \
  -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
  -H "Content-Type: application/json" \
  -d '{ "maxSessionMinutes": 480, "maxIdleMinutes": 30, "sessionQuota": 5 }'
```

## What the user sees

- **The session lives in a browser cookie that ends when the browser closes.** There's no "stay signed in" choice:
  a user who closes their browser signs in again next time, unless the browser restores its cookies on restart.
- **A session that ends asks for a new sign-in.** After `maxSessionMinutes`, or `maxIdleMinutes` without use, the next
  page that needs the session sends the user to sign in again.
- **Over the quota, the oldest session ends.** A user signed in on many devices finds their oldest one signed out.

### How a session, an app's session and a refresh token end: an example

With the defaults (two hours, 30 idle minutes) and the realm's default token lifetimes, Maya signs in at 09:00 and
opens an app that signs her in with OpenID Connect:

| Time | What happens |
|---|---|
| 09:00 | Maya signs in. The realm's session will end at 11:00 at the latest, or sooner after 30 minutes unused. The app gets an access token (5 minutes) and a refresh token. |
| 09:05 to 09:40 | Maya works in the app, which refreshes its access token every few minutes. Those refreshes use the app's refresh token, not the realm's session, so they don't keep the session from idling. |
| 09:20 | Maya opens a second app. Its sign-in uses the realm's session without asking her anything, which counts as use: the idle clock restarts. |
| 09:50 | 30 minutes without use: the realm's session ends. The app's access is online (no `offline_access`), so its refresh tokens stop working at the same moment, and the app is sent a back-channel logout if it asked for one. |
| 09:52 | The app's next refresh is refused. Its own session may still say Maya is signed in, but it has to send her to sign in again. |

If users spend long stretches in apps without returning to the realm, raise `maxIdleMinutes`, or give those apps
offline access. With `offline_access`, the refresh tokens outlive the session until their own lifetime ends (a day by default); see
[Choose online or offline access](../../guides/sessions/online-and-offline-access/).

## Limits and defaults

| Setting or limit | Default | Range |
|---|---|---|
| `maxSessionMinutes`: how long a session lives | `120` (two hours) | up to `43200` (30 days) |
| `maxIdleMinutes`: how long it may sit unused | `30` | up to `1440` (a day) |
| `sessionQuota`: sessions one user holds at once | `10` | `1` to `20` |
| `loginTimeoutSecs`: the whole sign-in, however many pages | `120` | `1` to `86400` |
| `pageTimeoutSecs`: one page of a sign-in | `120` | |

The last two are on `authSettings`, in **Authentication → Settings**.

## Troubleshooting

| Symptom | Cause | Fix |
|---|---|---|
| A script's calls start failing with `401` | Each run signs in again, and past the quota its oldest sessions, or the user's, are ended | Sign in once per run and reuse the session |
| A user is signed out of one device when they sign in on another | They hold `sessionQuota` sessions already | Raise `sessionQuota` (up to 20), or have them sign out where they no longer work |
| A sign-in page says it has expired | The user took longer than `pageTimeoutSecs` on a page, or `loginTimeoutSecs` overall | Start again; raise the timeouts if users often need longer |

## Audit record

Each sign-in is recorded step by step (`LOGIN_STARTED`, `MODULE_COMPLETED`, then `LOGIN` or `LOGIN_FAILED`), an
upgrade as `SESSION_UPGRADED`, each session ended as `LOGOUT` (including one ended by the quota), and expired
sessions swept as `SESSIONS_REAPED`. In the dashboard, open **Audit → Authentication**; see
[What is recorded](/audit-reports/audit-trail/what-is-recorded/).

## Good to know

- **A page that can't be answered isn't shown.** If the sign-in timed out, or another request already moved it on,
  the client is told so rather than shown a page whose answer would be lost.
- **A success that created nothing isn't reported.** If the sign-in timed out at its last step, no session id is
  handed back.
- **Each module leaves its own audit row,** and the final row carries the whole sign-in.
- **Scripts share the quota.** A script that signs in on every call ends its own earlier sessions, and the user's;
  sign in once per run.

## Reference

| Where | Setting | What it does |
|---|---|---|
| `sessionSettings` | `maxSessionMinutes` (`120`; at most 43200, 30 days) | How long a session lives |
| `sessionSettings` | `maxIdleMinutes` (`30`; at most 1440, a day) | How long it may sit unused |
| `sessionSettings` | `sessionQuota` (`10`; 1 to 20) | How many sessions one user holds at once |
| `oauth2Settings` | `acrValues`, `amrValues` | Which chain earns each `acr`, and which `amr` each module adds |

Values in brackets are the defaults.

## Related

- [Session management](../session-management/): find a user's sessions and end them.
- [Lockout](../lockout/): how the realm stops password and code guessing.
- [Step up for sensitive actions](../../guides/stronger-sign-in/step-up-for-sensitive-actions/): ask for a
  stronger sign-in from an app.
- [Keep apps signed in](../../guides/sessions/keep-apps-signed-in/): how apps stay signed in after the
  session.
