# Logout

> Signing out from one app ends the user's session, revokes the tokens it gave, and tells your other apps.

When a user signs out of one of your apps, the realm ends their session, and every access token given in that
session without offline access stops working at once. Your other apps that signed the user in through the same
session are told, so each can end its own session for that user too.

Use it in every app that signs users in with [OpenID Connect](../openid-connect/). The app starts the sign-out by
sending the browser to the realm; apps that registered a back-channel address hear about it without the browser.

## Before you begin

- **Who can do it:** a realm administrator (a group holding `RealmOwner`) registers the client's sign-out addresses;
  `RealmViewer` can read them. The app starts each sign-out itself.
- **Where:** today this is done through the API ([Call the API](/develop/api/) shows how to get
  `$CVYUH_SESSION`).
- **First:** the app signs users in with [OpenID Connect](../openid-connect/), so it holds an ID token to send as the
  hint.

## How it works

**1. The app sends the browser to the realm**, with the ID token it holds as `id_token_hint`, where to return, and a
`state` of its own:

```http
GET /oauth2/logout?id_token_hint=eyJhbGciOiJSUzI1NiIs…
    &post_logout_redirect_uri=https%3A%2F%2Fwiki.example.com%2Fsigned-out&state=x7f3k
Host: foobar.cvyuh.com
```

**2. The realm ends the session** when the hint names the browser's current session, and returns the browser:

```http
HTTP/1.1 303 See Other
Location: https://wiki.example.com/signed-out?state=x7f3k
```

If the hint names another session, or there is no hint, the user is first asked on the realm's sign-out page.
Without a hint, the app sends its `client_id` beside `post_logout_redirect_uri`, and the browser returns there once
the user confirms. If the user chooses to stay signed in, nothing ends, and the browser goes to the realm's home
page.

**3. Other apps are told.** Every app that received an ID token in that session and registered a
`backchannelLogoutUri` receives a `POST` with a form field `logout_token`, and answers `200` or `204`. Each app is
told once per session. The token is a JWT of type `logout+jwt`, with these claims:

```json
{
  "iss": "https://foobar.cvyuh.com/oauth2",
  "aud": ["team-wiki"],
  "iat": <unix time>,
  "exp": <iat + 120>,
  "jti": "0c9a2f4e-71d3-4b8e-a5f6-3e2d1c0b9a87",
  "sid": "6f1e2d3c-4b5a-4968-8776-a5b4c3d2e1f0",
  "events": { "http://schemas.openid.net/event/backchannel-logout": {} }
}
```

It names the session by `sid`, the same `sid` as in the app's ID tokens. It carries no `sub` and no `nonce`, so it
can never pass for an ID token. It's signed with the same algorithm as the app's ID tokens.

### What ends with the session

| Access | After sign-out |
|---|---|
| Tokens given without `offline_access` | End with the session, everywhere at once |
| Tokens given with `offline_access` | Keep working: the user allowed access while away |
| A session that simply times out | Ends its tokens the same way, but no app is told |

A token given without `offline_access` never outlives the session's own end, even if the session isn't signed out:
its lifetime is cut to fit. Signing out of the realm's own pages has the same effect as an app-initiated sign-out.

## Set it up

Set these on the client (`https://foobar.cvyuh.com/api/v0/am/objects/v1/oauth2Client`; see [Call the API](../../../../develop/api/)) and the
realm (`PATCH https://foobar.cvyuh.com/api/v0/am/objects/v1/oauth2Settings/oauth2Settings`):

| Where | Setting | What it does |
|---|---|---|
| Client | `postLogoutRedirectUris` | Where the browser may return after logout |
| Client | `backchannelLogoutUri` | Where the realm posts the logout token |
| Client | `idTokenSignedResponseAlg` | The algorithm its ID tokens and logout tokens are signed with ([OpenID Connect](../openid-connect/)) |
| Realm | `logoutChallengeLifetimeSecs` | How long the confirm page stays answerable (default 600) |
| Realm | `logoutTokenLifetimeSecs` | How long a logout token is valid (default 120) |

The app sends the browser to `https://foobar.cvyuh.com/oauth2/logout`. Discovery publishes it as
`end_session_endpoint`.

## What the user sees

With a hint naming their current session, nothing: the browser goes straight back to the app's signed-out address.
Otherwise the realm's sign-out page asks the user to confirm. If they choose to stay signed in, nothing ends and the
browser goes to the realm's home page. A bad hint shows the realm's signed-out page with the error, and never returns
to the app.

## Limits and defaults

| Setting or limit | Default | Range |
|---|---|---|
| `logoutChallengeLifetimeSecs`: how long the confirm page stays answerable | 600 | 10 to 3,600 |
| `logoutTokenLifetimeSecs`: how long a logout token is valid | 120 | 1 to 300 |
| Back-channel delivery | one try and one retry | fixed |
| `backchannelLogoutUri` | none | `https`, at a public host name |

## Troubleshooting

| Symptom | Cause | Fix |
|---|---|---|
| The user lands on the realm's signed-out page, not the app's | `post_logout_redirect_uri` isn't exactly one of `postLogoutRedirectUris` | Register the exact address |
| The user is asked to confirm every time | The app sends no `id_token_hint`, or one for another session | Send the ID token from the current sign-in |
| Another app stays signed in | It registered no `backchannelLogoutUri`, didn't answer `200`/`204`, or keeps its own session without checking `sid` | Register the address, answer quickly, and end the local session whose `sid` matches |

## Audit record

A sign-out writes a `LOGOUT` record for the session, on the user's trail. Whether each app was told isn't recorded
on the trail today; check the app's own records for the `logout_token` it received. See the sign-out in
**Audit → Authentication**.

## Good to know

- **The browser returns only to a registered address**, matched exactly against `postLogoutRedirectUris`. Any other
  is ignored, and the browser goes to the realm's own signed-out page. `state`, when sent, is returned on that
  address.
- **An expired ID token is still a good hint**: a session can outlive its token.
- **A bad hint ends nothing.** A hint the realm didn't issue, or a `client_id` that isn't the hint's audience, is an
  error. The browser is shown the realm's signed-out page with the error, never sent back to the app.
- **A browser with no session isn't an error**: it's simply returned.
- **Only the session being ended can answer its confirm page.**
- **Back-channel addresses must be `https`, at a public host name.** Addresses such as `localhost` or an IP address
  are refused.
- **Delivery is best effort, with one retry.** The session's tokens never wait on it: they end the moment the
  session ends.

## Related

- [Sign users out from an app](../../../guides/sessions/sign-out-from-an-app/): the app's side, start to finish.
- [Tell other apps about a sign-out](../../../guides/sessions/tell-apps-about-sign-out/): receiving logout
  tokens.
- [Choose online or offline access](../../../guides/sessions/online-and-offline-access/): which tokens outlive the
  session.
- [Refresh tokens](../refresh-tokens/): what keeps an app signed in until then.
- [Revoke tokens](../../revoke-access/revocation/): every other way access is revoked.
