跳到主内容

RP 发起的登出(OIDC end-session 端点)

RP 发起的登出让依赖方(RP,即您的客户端应用)将用户从 Casdoor 登出,然后将浏览器重定向回应用。Casdoor 实现了 OpenID Connect RP-Initiated Logout 1.0。OpenID Connect (OIDC) 客户端库将此端点称为 end-session 端点。

什么是 RP 发起的登出​

从您的应用登出只会结束应用自身的会话。用户仍然登录在身份提供商上,因此下次登录时无需输入密码即可直接通过。RP 发起的登出填补了这一缺口:当用户在您的应用中点击 Sign out 时,应用清除自身会话并将浏览器重定向到身份提供商的 end-session 端点,该端点结束提供商的会话并重定向回来。

OpenID Connect 有三个协同工作的登出规范:

SpecificationWho starts itWhat it does
RP-initiated logoutYour application, through the browserEnds the session at the identity provider
Back-channel logoutThe identity provider, server to serverTells the other applications of the session to end their sessions
Front-channel logoutThe identity provider, through the browserThe same, through hidden iframes in the browser

Casdoor 实现了 RP 发起的登出和后端通道登出:在此端点执行登出后,它会向同一会话中的其他应用发送后端通道登出通知。其在 /.well-known/openid-configuration 的发现文档通过 end_session_endpoint 宣布该端点,因此大多数 OIDC 库无需配置即可找到它。

典型请求如下:

https://door.example.com/api/logout?id_token_hint=<ID token>&post_logout_redirect_uri=https%3A%2F%2Fapp.example.com%2F&state=af0ifjsldkj

若要一次性结束组织中某个用户的所有会话,请使用单点登出。

端点​

GET  /api/logout
POST /api/logout

参数​

参数必需映射的工具
id_token_hint推荐Casdoor 之前签发给用户的 ID 令牌(id_token)。提供时,Casdoor 用它来定位并使对应的令牌 / 会话过期。
post_logout_redirect_uri可选登出后浏览器跳转到哪里。必须登记在应用的 Redirect URLs 列表中,否则请求会被拒绝。
client_id可选应用的客户端 ID。在没有 id_token_hint、且无法从当前会话确定应用时,用来定位应用。
州/省可选一个不透明的值,会作为 state 查询参数追加到 post_logout_redirect_uri 后返回。
信息

OIDC 规范建议提供 id_token_hint 但不强制要求。Casdoor 接受不含该参数的请求,此时会登出当前浏览器会话。某些客户端(如 Gitea)仅发送 post_logout_redirect_uri,可能还附带 client_id。

行为​

Casdoor 的行为取决于请求是否包含 id_token_hint。

带有 ID 令牌提示时​

  1. Casdoor 查找 ID 令牌为 id_token_hint 的令牌。对于旧版本 Casdoor 颁发的提示(当时访问令牌和 ID 令牌相同),Casdoor 会将提示作为访问令牌进行查找。如果找不到令牌,Casdoor 返回错误 Token not found, invalid accessToken。
  2. Casdoor 会使该令牌过期。如果请求中没有会话 cookie,Casdoor 会从令牌中提取用户信息。
  3. Casdoor 清除当前浏览器会话,并向其他应用发送后端通道注销通知,同时向该会话中的 SAML 应用发送 SAML 注销请求。如果应用通过带有注销 URL 的自定义 OAuth 提供商让用户登录,Casdoor 会使用该令牌的访问令牌调用该 URL。
  4. If post_logout_redirect_uri is present and valid for the application, Casdoor redirects the browser to it and appends state if the request contains it. Otherwise, Casdoor returns HTTP 200.

无 ID 令牌提示​

  1. Casdoor 从会话中获取应用信息,如果不可用,则回退到由 client_id 标识的应用。
  2. 如果浏览器没有活动会话,说明用户已经登出,可能是因为会话过期或在其他标签页中结束。如果请求包含 post_logout_redirect_uri 且 Casdoor 能识别该应用,Casdoor 仍会检查 URL,将浏览器重定向到该地址,并附加 state。否则,Casdoor 返回 HTTP 200 且不执行其他操作。
  3. 否则,Casdoor 会注销当前会话。
  4. Casdoor 清除会话及其令牌,并发送后端通道登出通知。
  5. 如果存在 post_logout_redirect_uri 且对该应用有效,Casdoor 会将浏览器重定向到该地址,并在请求包含 state 时附加它。
  6. 如果请求中没有 post_logout_redirect_uri,Casdoor 返回 HTTP 200。响应中包含应用的主页 URL(如果已设置),但内置应用除外。

审计记录​

Casdoor 记录每次登出操作,即使对于 GET 请求,当 logPostOnly 为 true 时也会记录,并触发登出的 Webhooks。如果请求中没有会话 Cookie,记录中将显示 id_token_hint 对应的用户。Casdoor 会从存储在记录中的请求 URI 中移除 id_token_hint。请参阅 配置参考。

重定向 URL 验证​

Casdoor 始终根据应用的 Redirect URLs 检查 post_logout_redirect_uri。如果 URL 不在列表中,或者 Casdoor 无法确定应用,Casdoor 将拒绝请求并报错,不进行重定向。这可以防止开放重定向漏洞。

请将你的登出后跳转 URL 添加到应用的 Redirect URLs 中。

示例​

使用 ID 令牌登出并返回应用:

GET /api/logout?id_token_hint=<ID_TOKEN>&post_logout_redirect_uri=https://myapp.example.com/logged-out&state=xyz

不使用 ID 令牌登出当前会话,并通过客户端 ID 标识应用:

GET /api/logout?client_id=<CLIENT_ID>&post_logout_redirect_uri=https://myapp.example.com/logged-out

另请参阅​