添加 OpenID Connect 提供商
本指南介绍如何将 Casdoor 连接到支持 OpenID Connect (OIDC) 的身份提供商,例如 Keycloak、Authentik、Zitadel 或另一个 Casdoor 实例。OIDC 提供商类型从发行者的发现文档中读取端点,检查 ID 令牌,并根据标准 OIDC 声明填充用户信息。
学习成果
- 添加一个
OIDC提供商,并根据发行者 URL 填写其端点。 - 了解 Casdoor 如何检查 ID 令牌以及它读取哪些声明。
所需条件
- An identity provider that publishes an OIDC discovery document at
<issuer>/.well-known/openid-configuration - A client at the identity provider with the redirect URI
https://<your-casdoor-host>/callback, and its client ID and client secret - Casdoor 管理控制台的 Administrator 访问权限
关于 OIDC 类型
OIDC 是与从 Custom 到 Custom10 不同的独立类型。这两种类型显示相同的端点字段,但 OIDC 增加了 Issuer URL,从发现文档填充端点,并且在设置了 Issuer URL 时要求提供有效的 ID 令牌。对于标准的 OpenID Connect 提供商,请使用 OIDC;对于不支持 OIDC 的 OAuth 2.0 提供商,请使用 自定义 OAuth 提供商。
创建提供商
-
在 Casdoor 管理控制台中,前往 Identity \u003e Providers 并添加一个提供商。
-
将 Category 设置为
OAuth,将 Type 设置为OIDC。Casdoor 会将 Scope 设置为openid profile email。 -
输入来自身份提供商的 Client ID 和 Client secret。
-
In Issuer URL, enter the issuer of the identity provider, for example
https://idp.example.com/realms/<realm-name>, and click Request. Casdoor reads the discovery document and fills in the fields:Field Value from the discovery document Issuer URL issuerAuth URL authorization_endpointToken URL token_endpointUserInfo URL userinfo_endpointLogout URL end_session_endpoint -
检查其他字段并根据需要进行修改:
Field Description Scope Scopes to request. Keep openidin the list, because the identity provider returns an ID token only for itEnable PKCE Adds Proof Key for Code Exchange (PKCE) to the flow User mapping 每个用户字段要读取的 Claims。行以默认名称开始,例如 ID 对应 id,Username 对应username。请将 ID 设置为sub,因为 OIDC 提供商没有idclaim。ID、Username 和 Display name 行不能为空。请参阅 Casdoor 读取的 ClaimsFavicon URL of the logo that the sign-in page shows for the provider -
保存提供商.
Casdoor fetches the discovery document from <issuer>/.well-known/openid-configuration. It removes a trailing / from the issuer, adds https:// if the issuer has no scheme, and doesn't append the path again if the issuer already ends with it. The request fails if the document can't be read within 10 seconds, or if it has no authorization_endpoint or token_endpoint.
浏览器在登录时会跳转到 Auth URL,因此必须设置它。如果 Token URL 或 UserInfo URL 为空,Casdoor 会在每次登录时从 issuer 的发现文档中读取它们。
The Request button calls GET /api/get-idp-discovery?issuer=<issuer>, which requires an administrator. For an organization administrator, Casdoor fetches the document only from Internet addresses, and refuses private, loopback, and link-local addresses, unless the administrator's organization is a trusted organization. See Which addresses a provider can reach.
Casdoor 如何检查 ID 令牌
在 Token URL 处交换授权码后,Casdoor 读取令牌响应中的 id_token。
- 如果设置了 Issuer URL,则必须提供
id_token,否则登录将失败。 sub必须存在。- 如果设置了 Issuer URL,
iss必须与之相等,忽略末尾的/。 exp(如果存在)必须在未来时间。aud必须是 Client ID,或者是包含它的列表。
Casdoor 通过它打开的连接直接从令牌端点接收 ID 令牌,不检查令牌的签名。
如果设置了 UserInfo URL,Casdoor 还会使用访问令牌调用它。其 claims 会覆盖同名的 ID 令牌 claims。
Casdoor 读取的 Claims
For each user field, Casdoor uses the claim that User mapping names. If the mapping names no claim, or the provider doesn't return the claim, or the claim is empty, Casdoor uses the first standard claim that has a value. The exception is ID: if the provider doesn't return the claim that ID names, the sign-in fails with cannot find <claim> in user from custom provider.
| User field | Standard claims, in order |
|---|---|
| ID | sub |
| Username | preferred_username, name, sub |
| Display name | name, preferred_username |
email | |
| Email verified | email_verified |
| Phone | phone_number |
| Phone verified | phone_number_verified |
| Avatar | picture |
如果提供商未返回 ID,登录将失败。
Casdoor 将其他 claims 以 JSON 格式存储在用户属性 oauth_OIDC_extra 中。它省略了仅对协议重要的 claims:aud、at_hash、auth_time、azp、c_hash、exp、iat、jti、nbf、nonce、s_hash 和 sid。要将这些 claims 之一复制到用户字段(如职位),请参阅 将 OAuth claims 映射到用户字段。
email_verified 和 phone_number_verified 决定应用的 Binding rule 是否可以将登录链接到具有相同电子邮件地址或电话号码的现有用户。请参阅 Casdoor 如何关联账户。
注销
当用户从使用该提供商的应用程序注销时,如果设置了 Logout URL,Casdoor 会向 Logout URL 发送一个 POST 请求,其中包含表单字段 token、client_id 和 client_secret。它在后台发送请求,仅记录失败信息。如果身份提供商不接受此请求,请清除 Logout URL。
验证结果
- 将提供商添加到应用程序。请参阅 向应用程序添加提供商。
- 打开应用程序的登录页面并点击提供商按钮。
- 在身份提供商处登录。Casdoor 为您登录,或者如果应用程序允许注册,则创建一个用户。
故障排除
| Error | Cause |
|---|---|
the OIDC discovery from: <url> doesn't contain the required endpoints | The document has no authorization_endpoint or token_endpoint. Check the issuer URL |
cannot find id in user from custom provider | ID in User mapping is id. Set it to sub |
id_token not found in token response | The Scope has no openid, or the provider doesn't return an ID token |
id_token iss: <iss> doesn't match the issuer: <issuer> | The Issuer URL differs from the iss claim. Use the exact value of issuer in the discovery document |
id_token aud doesn't contain the client ID | The ID token was issued for another client. Check the Client ID |