|
| 1 | +--- |
| 2 | +title: RP-initiated logout |
| 3 | +description: Log a user out of Casdoor from a relying party using the OIDC RP-Initiated Logout endpoint. |
| 4 | +keywords: [OIDC, RP-initiated logout, end session, logout, id_token_hint, post_logout_redirect_uri] |
| 5 | +authors: [hsluoyz] |
| 6 | +--- |
| 7 | + |
| 8 | +## Overview |
| 9 | + |
| 10 | +**RP-Initiated Logout** lets a relying party (RP, i.e. your client application) log the user out of Casdoor and, optionally, redirect the browser back to the application afterward. It follows the [OpenID Connect RP-Initiated Logout 1.0](https://openid.net/specs/openid-connect-rpinitiated-1_0-final.html) specification. |
| 11 | + |
| 12 | +This is the endpoint an OIDC client calls as its "end session" endpoint. It differs from [single sign-out](/docs/session/single-sign-out), which terminates every session in the organization at once. |
| 13 | + |
| 14 | +## Endpoint |
| 15 | + |
| 16 | +```text |
| 17 | +GET /api/logout |
| 18 | +POST /api/logout |
| 19 | +``` |
| 20 | + |
| 21 | +| Parameter | Required | Description | |
| 22 | +|---|---|---| |
| 23 | +| `id_token_hint` | Recommended | The ID token (`id_token`) that Casdoor previously issued to the user. When present, Casdoor uses it to identify and expire the exact token/session. | |
| 24 | +| `post_logout_redirect_uri` | Optional | Where to send the browser after logout. Must be registered in the application's **Redirect URLs** list, otherwise the request is rejected. | |
| 25 | +| `client_id` | Optional | The client ID of the application. Used to resolve the application when `id_token_hint` is omitted and the application cannot be determined from the current session. | |
| 26 | +| `state` | Optional | An opaque value echoed back as a `state` query parameter appended to `post_logout_redirect_uri`. | |
| 27 | + |
| 28 | +:::info |
| 29 | + |
| 30 | +Per the OIDC spec, `id_token_hint` is **RECOMMENDED, not REQUIRED**. Casdoor therefore accepts logout requests without it and falls back to the user's current browser session. Some clients (for example, [Gitea](https://github.com/casdoor/casdoor/issues/5607)) only send `post_logout_redirect_uri` (optionally with `client_id`) — these requests are supported. |
| 31 | + |
| 32 | +::: |
| 33 | + |
| 34 | +## Behavior |
| 35 | + |
| 36 | +Casdoor selects one of two paths depending on whether `id_token_hint` is provided. |
| 37 | + |
| 38 | +### With `id_token_hint` |
| 39 | + |
| 40 | +1. The token identified by `id_token_hint` is expired. |
| 41 | +2. The current browser session is cleared and a back-channel logout notification is sent to other applications. |
| 42 | +3. If `post_logout_redirect_uri` is present and valid for the application, the browser is redirected there (with `state` appended when supplied). Otherwise the endpoint returns `200 OK`. |
| 43 | + |
| 44 | +### Without `id_token_hint` |
| 45 | + |
| 46 | +1. If there is no active session, the endpoint returns `200 OK` and does nothing (the user is already logged out). |
| 47 | +2. Otherwise Casdoor logs out the current session. The application is resolved from the session first, and falls back to the one identified by `client_id` when provided. |
| 48 | +3. The session and its token are cleared and a back-channel logout notification is sent. |
| 49 | +4. If `post_logout_redirect_uri` is present and valid for the application, the browser is redirected there (with `state` appended when supplied). |
| 50 | +5. If no `post_logout_redirect_uri` is given, Casdoor returns `200 OK`, including the application's homepage URL when one is configured (except for the built-in application). |
| 51 | + |
| 52 | +## Redirect URI validation |
| 53 | + |
| 54 | +`post_logout_redirect_uri` is always validated against the target application's registered **Redirect URLs**. If the URI is not in that list — or the application cannot be resolved — Casdoor rejects the request with an error instead of redirecting. This prevents open-redirect abuse. Make sure your post-logout URL is added to the application's Redirect URLs. |
| 55 | + |
| 56 | +## Examples |
| 57 | + |
| 58 | +Log out using the ID token and return to the app: |
| 59 | + |
| 60 | +```text |
| 61 | +GET /api/logout?id_token_hint=<ID_TOKEN>&post_logout_redirect_uri=https://myapp.example.com/logged-out&state=xyz |
| 62 | +``` |
| 63 | + |
| 64 | +Log out based on the current session, resolving the app by `client_id` (no `id_token_hint`): |
| 65 | + |
| 66 | +```text |
| 67 | +GET /api/logout?client_id=<CLIENT_ID>&post_logout_redirect_uri=https://myapp.example.com/logged-out |
| 68 | +``` |
| 69 | + |
| 70 | +## See also |
| 71 | + |
| 72 | +- [Single sign-out (SSO logout)](/docs/session/single-sign-out) — terminate every session in the organization at once. |
| 73 | +- [Session management](/docs/session/management) |
0 commit comments