# Tell other apps about a sign-out

> When a user's session ends, the realm tells every app they signed in to through it, so each app can end its own session for them.

A user signs out in one app, or in the dashboard. Their tokens stop working at once, but an app that keeps its own
session, such as a server-side web app with its own cookie, would still show them signed in. With back-channel
logout, every other app they signed in to through the same session hears about it within seconds and ends its own
session for them.

**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:** have the app's client ID, and an `https` address on a public host name where the app receives the
  notice.

## Set it up

<Steps>

1. Register the app's back-channel address:

   ```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 '{ "backchannelLogoutUri": "https://wiki.example.com/backchannel-logout" }'
   ```

   `localhost`, single-word names and IP addresses are refused when you register it.

2. Have the app handle the notice. It's an HTTP POST carrying a **logout token**: a short-lived JWT signed by the
   realm, naming the realm, the app and the session (`sid`). The `sid` matches the one in the ID token the app received
   at sign-in, so the app finds its session by that `sid` and ends it.

3. Optionally, change how long a logout token is valid:

   ```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 '{ "logoutTokenLifetimeSecs": 120 }'
   ```

</Steps>

## Good to know

- **Each app is told once per session**, even if it signed in several times.
- **Only apps that received an ID token** in that session are told.
- **Delivery is best effort:** one retry after a timeout or a server error. The user's tokens don't wait for it; they
  end the moment the session ends.
- **A session that only times out isn't announced.**
- **The address must resolve to a public address** when it is called, or the call isn't made.

## Check it worked

Sign in to two apps with one session, and sign out from the first. The second app's back-channel address receives a
POST with a logout token whose `sid` matches its session.

The sign-out itself is on the user's trail as a `LOGOUT` record; the notices to each app are not, so look for them in
the app's own records.

## Troubleshooting

| Symptom | Cause | Fix |
|---|---|---|
| Registering the address is refused | It is `localhost`, a single-word name or an IP address | Use a public `https` host name |
| The app is never told | It received no ID token in that session, or the session only timed out | Ask for `openid` at sign-in; a timeout isn't announced |
| The app is told, but the user stays signed in to it | The app looks the session up by something other than `sid` | Store the ID token's `sid` at sign-in and end that session |
| The app rejects the logout token | It checked it after `logoutTokenLifetimeSecs`, or against the wrong issuer or audience | Check promptly; `iss` is the realm's issuer and `aud` the app's client ID |

## Undo it

`PATCH` the client with `{ "backchannelLogoutUri": null }`. The app is no longer told.

## Related

- [Logout](../../../applications/single-sign-on/logout/): back-channel logout and every sign-out path.
- [Sign users out from an app](../sign-out-from-an-app/): starting the sign-out.
