---
title: Logout
summary: Signing out has two parts: ending your app's own session, and ending the person's Tessera session so their other apps sign out too.
section: connect
order: 4
---
# Logout

Signing out has two parts: ending your app's own session, and ending the person's Tessera session so their other apps sign out too.

## What ending a Tessera session does

Tessera keeps one login session per browser per realm. Every app the person signed in to through it is a participant. When the session ends, Tessera does three things together:

1. Deletes the session, so the next `/authorize` shows the sign-in page.
2. Revokes every refresh token issued through it. The next refresh fails with `invalid_grant`.
3. Sends a back-channel logout to every participating app that registered a `backchannel_logout_uri`.

A session ends when the person signs out at the end-session endpoint, revokes it on their account page, an administrator revokes it, the user is disabled or deleted, their password is changed or reset, or they turn off their second factor (which ends their other sessions). It does not end just because it expired.

## RP-initiated logout

Clear your own session first, then send the browser to the `end_session_endpoint` from discovery.

```http
GET /realms/acme/logout
  ?id_token_hint=eyJ...
  &post_logout_redirect_uri=https%3A%2F%2Fapp.example.com%2Fsigned-out
  &state=Rk2p9 HTTP/1.1
Host: login.kurthlabs.com
```

| Parameter | Notes |
| --- | --- |
| `id_token_hint` | The ID token you hold. It tells Tessera which client is signing out. Tessera checks its signature against the realm's keys and its `iss`; it does not check expiry, so an old token still works. A hint that does not verify is ignored. |
| `client_id` | Use this instead if you do not keep the ID token. It selects the client whose registered post-logout redirect URIs apply, but unlike a verified `id_token_hint` it does not brand the signed-out page. |
| `post_logout_redirect_uri` | Where to send the browser afterwards. It must exactly equal one of the client's registered post-logout redirect URIs. |
| `state` | Returned on the redirect as `state=...`. |

The endpoint accepts `GET` only, with the parameters in the query string, because it is a browser redirect. The browser's Tessera session ends whenever the request arrives, whatever the parameters say; they only decide where the browser goes next. Answers:

- With a registered `post_logout_redirect_uri`: `303 See Other` to that URI, with `state` added.
- Without one, or with a URI that is not registered: Tessera's own "You are signed out" page. It never follows an unregistered URL.
- If another app of the session registered a `frontchannel_logout_uri`, the response is a page that loads those URIs in hidden frames and then continues to the redirect after a moment.

> **Planned.** Today the end-session endpoint ends the browser's session as soon as it is requested, even without an `id_token_hint`. Because it is a `GET`, a link on any web page can sign someone out of Tessera. A planned change makes Tessera ask the person to confirm before it ends a session when the request carries no valid `id_token_hint`. A request with a valid hint, as in the example above, is meant to behave as it does now. Send the hint whenever you have it.

Register the post-logout redirect URIs when you create the client (`--post-logout-uri` in [the CLI](/docs/reference/cli.md)). `tessera init` registers one for the dev origin and each `--url`.

## Back-channel logout

Back-channel logout tells your server that a session ended, even if the browser never comes back. You need it if your app keeps its own sessions and the person can sign out somewhere else.

Register an endpoint on the client:

```sh
tessera clients ensure acme-web --backchannel-logout-uri https://app.example.com/auth/backchannel-logout
```

Tessera sends a server-to-server `POST`:

```http
POST /auth/backchannel-logout HTTP/1.1
Host: app.example.com
Content-Type: application/x-www-form-urlencoded
Cache-Control: no-store

logout_token=eyJ...
```

### The logout token

A signed JWT with header `typ: logout+jwt` and these claims:

| Claim | Meaning |
| --- | --- |
| `iss` | The issuer your app signs in with |
| `aud` | Your client ID |
| `iat`, `exp` | Issued at, and two minutes later |
| `jti` | Unique ID of this logout; a retry reuses it |
| `events` | `{"http://schemas.openid.net/event/backchannel-logout": {}}` |
| `sub` | The user |
| `sid` | The login session, as your client sees it |

There is never a `nonce`. The `sid` equals the `sid` claim in the ID tokens your app received for that session, so store `sid` and `sub` with your app session when the person signs in.

### Verify it

Before you act on the token, check all of these:

1. The signature, using the key in the realm's `jwks_uri` that matches the header `kid`.
2. `typ` is `logout+jwt`. Reject anything else, so an ID token or access token cannot be replayed here.
3. `iss` is your issuer and `aud` is your client ID.
4. `iat` is recent (a few minutes at most) and `exp` has not passed.
5. `events` contains the back-channel logout key.
6. `sub` or `sid` is present, and `nonce` is absent.
7. The `jti` has not been seen before. Remember each one until its token expires.

Then end the matching app session, by `sid` when present, otherwise by `sub`. Answer `200` (any `2xx` works). Answer `400` if the token is invalid. Do not cache the response.

### Retries

Tessera treats `2xx` as delivered. A `4xx` answer (except `408` and `429`) is permanent and is not retried. A network error, a timeout, `408`, `429` or `5xx` is retried with backoff, up to 8 attempts. Each attempt signs a fresh token with the same `jti`, so make ending the session idempotent. Redirects are not followed, so register the final URL. The URL must be a public `https` address: a workspace's logout endpoint cannot point at `localhost` or a private or internal address, and Tessera has no allow list for workspaces. Registering one is refused with [`destination_not_public`](/errors/destination_not_public.md). While you develop, use a deployed URL or a public tunnel.

### Example

With [`jose`](https://github.com/panva/jose), which does the signature, `typ`, `iss`, `aud`, `iat` and `exp` checks for you. Create the key set once so it keeps its cache:

```ts
import { createRemoteJWKSet, jwtVerify } from "jose";

const issuer = "https://login.kurthlabs.com/realms/acme";
const jwks = createRemoteJWKSet(new URL(`${issuer}/jwks`));
const seen = new Map<string, number>(); // jti -> expiry; use a shared store if you run several copies

export async function backchannelLogout(request: Request): Promise<Response> {
  const form = await request.formData();
  try {
    const { payload } = await jwtVerify(String(form.get("logout_token") ?? ""), jwks, {
      issuer,
      audience: "acme-web", // your client ID
      typ: "logout+jwt",
      algorithms: ["EdDSA", "RS256"],
      maxTokenAge: "5m",
    });
    const events = payload.events as Record<string, unknown> | undefined;
    if (!events || !("http://schemas.openid.net/event/backchannel-logout" in events)) throw new Error("not a logout token");
    if ((!payload.sid && !payload.sub) || "nonce" in payload) throw new Error("invalid logout token");
    if (!payload.jti || seen.has(payload.jti)) throw new Error("replayed or missing jti");
    seen.set(payload.jti, (payload.exp ?? Date.now() / 1000) * 1000);
    await (payload.sid ? sessions.deleteBySid(String(payload.sid)) : sessions.deleteBySubject(String(payload.sub)));
    return new Response(null, { status: 200 });
  } catch {
    return new Response('{"error":"invalid_request"}', { status: 400 });
  }
}
```

The in-memory `seen` map lives in one process and grows until you prune the entries whose expiry has passed. If you run several copies of your app, keep the used `jti` values in a shared store (Redis, or a database table with a unique key).

If your realm has its own login domain, use that domain as the issuer and for the JWKS URL.

## Front-channel logout

A client can also register `frontchannel_logout_uri`. Tessera loads it in a hidden frame on its sign-out page, with `iss` and (if the client asks for it) `sid` added as query parameters. It only runs when the person signs out in their browser through Tessera's end-session endpoint, so use back-channel logout for anything that must be reliable.
