连接标准 OIDC 客户端
本指南解释了如何使用标准 OpenID Connect (OIDC) 客户端库将应用连接到 Casdoor,并描述了库所依赖的发现端点和 UserInfo 字段。
学习成果
- 找到您的 Casdoor 实例的发现 URL。
- 使用发现 URL 和应用凭据配置 OIDC 客户端库。
- 使用单个应用的发现端点。
- 将 UserInfo 字段映射到 Casdoor 用户的字段。
所需条件
- 一个正在运行的 Casdoor 实例。示例中使用演示站点
https://door.casdoor.com。 - Casdoor 中的一个应用,包含其客户端 ID、客户端密钥,以及你的应用的回调 URL(在 Redirect URLs 中配置)
- 适用于你所用编程语言的 OIDC 客户端库
配置客户端
Casdoor 是一个完整的 OIDC 提供商。如果你的应用已经使用 OIDC 库对接了其他身份提供商,你只需更改配置即可切换到 Casdoor。
-
选择一个库。例如:
OIDC 客户端库 语言 链接 go-oidc Go https://github.com/coreos/go-oidcpac4j-oidc Java https://www.pac4j.org/docs/clients/openid-connect.html更多库的信息,请参阅 oauth.net/code 和 认证的 OpenID 开发者工具。
-
向库提供以下值:
Setting Value Issuer or discovery URL https://<your-casdoor-host>. The library appends/.well-known/openid-configurationClient ID Client ID of the Casdoor application Client secret Client secret of the Casdoor application Redirect URL Callback URL of your application. It must be listed in Redirect URLs of the Casdoor application Scopes openid, plusprofileandemailas needed
该库从发现文档中读取 Casdoor 的端点和功能特性,因此你无需手动配置它们。
发现端点
Casdoor 在两个 URL 上发布其元数据。两者返回相同的文档。
| Standard | URL |
|---|---|
| OpenID Connect Discovery | https://<your-casdoor-host>/.well-known/openid-configuration |
| OAuth 2.0 授权服务器元数据 (RFC 8414) | https://<your-casdoor-host>/.well-known/oauth-authorization-server |
如果你的客户端仅支持 OAuth 2.0,请使用第二个 URL。
例如,https://door.casdoor.com/.well-known/openid-configuration 返回:
{
"issuer": "https://door.casdoor.com",
"authorization_endpoint": "https://door.casdoor.com/login/oauth/authorize",
"token_endpoint": "https://door.casdoor.com/api/login/oauth/access_token",
"token_endpoint_auth_methods_supported": [
"client_secret_basic",
"client_secret_post",
"private_key_jwt",
"none"
],
"userinfo_endpoint": "https://door.casdoor.com/api/userinfo",
"device_authorization_endpoint": "https://door.casdoor.com/api/device-auth",
"registration_endpoint": "https://door.casdoor.com/api/oauth/register",
"jwks_uri": "https://door.casdoor.com/.well-known/jwks",
"introspection_endpoint": "https://door.casdoor.com/api/login/oauth/introspect",
"response_types_supported": [
"code",
"token",
"id_token",
"code token",
"code id_token",
"token id_token",
"code token id_token"
],
"response_modes_supported": [
"query",
"fragment",
"form_post"
],
"grant_types_supported": [
"authorization_code",
"implicit",
"password",
"client_credentials",
"refresh_token",
"urn:ietf:params:oauth:grant-type:device_code",
"urn:ietf:params:oauth:grant-type:token-exchange",
"urn:openid:params:grant-type:ciba",
"urn:casdoor:params:oauth:grant-type:verification-code",
"urn:casdoor:params:oauth:grant-type:mobile-one-click"
],
"subject_types_supported": [
"public"
],
"id_token_signing_alg_values_supported": [
"RS256",
"RS512",
"ES256",
"ES384",
"ES512"
],
"scopes_supported": [
"openid",
"email",
"profile",
"address",
"phone",
"offline_access"
],
"code_challenge_methods_supported": [
"S256"
],
"claims_supported": [
"iss",
"ver",
"sub",
"aud",
"iat",
"exp",
"id",
"type",
"displayName",
"avatar",
"permanentAvatar",
"email",
"phone",
"location",
"affiliation",
"title",
"homepage",
"bio",
"tag",
"region",
"language",
"score",
"ranking",
"isOnline",
"isAdmin",
"isForbidden",
"signupApplication",
"ldap"
],
"request_parameter_supported": true,
"request_object_signing_alg_values_supported": [
"HS256",
"HS384",
"HS512"
],
"end_session_endpoint": "https://door.casdoor.com/api/logout",
"pushed_authorization_request_endpoint": "https://door.casdoor.com/api/login/oauth/par",
"require_pushed_authorization_requests": false,
"backchannel_authentication_endpoint": "https://door.casdoor.com/api/login/oauth/bc-authorize",
"backchannel_token_delivery_modes_supported": [
"poll"
],
"backchannel_user_code_parameter_supported": false,
"backchannel_logout_supported": true,
"backchannel_logout_session_supported": true,
"dpop_signing_alg_values_supported": [
"RS256",
"RS512",
"ES256",
"ES384",
"ES512",
"PS256",
"PS384",
"PS512"
],
"authorization_response_iss_parameter_supported": true,
"client_id_metadata_document_supported": false
}
该文档告知客户端以下内容:
- 授权类型: Casdoor 支持授权码模式、隐式模式、密码模式、客户端凭证模式和刷新令牌模式。它还支持用于输入受限设备(如智能电视和 CLI 工具)的设备代码模式 (
urn:ietf:params:oauth:grant-type:device_code)、令牌交换,以及其特有的验证码模式和移动一键登录模式。 - 响应类型和模式:
response_types_supported列出了授权码模式、隐式模式以及混合模式组合。response_modes_supported列出了三种响应模式。 - 客户端认证:
token_endpoint_auth_methods_supported表明客户端可以通过Authorization头或请求体中的密钥进行认证,可以使用签名的 JWT (private_key_jwt,参见联合凭证),或者作为使用 PKCE 的公共客户端完全不进行认证 (none)。 - PKCE:
code_challenge_methods_supported表明 Casdoor 支持使用S256方法的 PKCE(Proof Key for Code Exchange)。PKCE 保护公共客户端(如移动应用和单页应用)免受授权码拦截。支持 PKCE 的库会根据此元数据自动启用它。若要自行实现 PKCE,请参阅 OAuth 2.0。 - 授权响应中的 Issuer:
authorization_response_iss_parameter_supported为true,因为 Casdoor 会在每个授权响应中添加iss(RFC 9207)。 - 客户端 ID 元数据文档: 当
built-in组织接受指向元数据文档 URL 的客户端 ID 时,client_id_metadata_document_supported为true。参见动态客户端注册。
单个应用的发现端点
每个应用也有自己的发现端点。当应用使用自己的证书对令牌签名时(例如在多租户部署中),或者当你逐个迁移应用到新证书时,请使用这些端点。
| Endpoint | URL |
|---|---|
| OpenID Connect Discovery | https://<your-casdoor-host>/.well-known/<application-name>/openid-configuration |
| OAuth 2.0 授权服务器元数据 | https://<your-casdoor-host>/.well-known/<application-name>/oauth-authorization-server |
| JSON Web Key Set (JWKS) | https://<your-casdoor-host>/.well-known/<application-name>/jwks |
| WebFinger | https://<your-casdoor-host>/.well-known/<application-name>/webfinger |
例如,对于演示站点上的应用 app-example:
https://door.casdoor.com/.well-known/app-example/openid-configuration
https://door.casdoor.com/.well-known/app-example/oauth-authorization-server
应用文档与全局文档在这些字段上有所不同:
jwks_uri指向应用的 JWKS。registration_endpointcarries the organization of the application, as inhttps://door.casdoor.com/api/oauth/register?organization=<organization-name>, so that clients that register through dynamic client registration land in that organization. The global document has noorganizationparameter, and registers clients inbuilt-in. A document leavesregistration_endpointout when the organization doesn't allow dynamic client registration.scopes_supported添加了应用的自定义 Scopes。
全局端点返回:
{
"issuer": "https://door.casdoor.com",
"jwks_uri": "https://door.casdoor.com/.well-known/jwks",
"registration_endpoint": "https://door.casdoor.com/api/oauth/register",
...
}
casbin 组织下的应用 app-example 的端点返回:
{
"issuer": "https://door.casdoor.com",
"jwks_uri": "https://door.casdoor.com/.well-known/app-example/jwks",
"registration_endpoint": "https://door.casdoor.com/api/oauth/register?organization=casbin",
...
}
应用的 JWKS 包含应用自身证书的公钥。如果应用没有自己的证书,JWKS 将包含全局证书。
The issuer is the same in both documents. The iss claim of a token is the issuer of the application's organization, which is the host of the Casdoor backend unless the organization has its own issuer, and OIDC clients require the issuer of the discovery document to match it. Earlier versions returned an issuer that included the application name, such as https://door.casdoor.com/.well-known/app-example, which made strict clients reject the tokens.
应用文档的地址并不是在 issuer 后追加 /.well-known/openid-configuration,因此仅接收 issuer URL 并自行构建发现 URL 的客户端无法找到该地址。对于此类客户端,请为组织提供独立的 issuer。
组织的发现端点
在组织的编辑页面上开启 Use organization issuer,该组织将作为独立的 issuer 运行,其地址为 https://<your-casdoor-host>/orgs/<organization-name>。如果客户端在 issuer 后追加 /.well-known/openid-configuration,或者按照 RFC 8414 在其路径前插入 /.well-known/oauth-authorization-server,即可找到该组织的文档:
| Endpoint | URL |
|---|---|
| OpenID Connect Discovery | https://<your-casdoor-host>/orgs/<organization-name>/.well-known/openid-configuration |
| OpenID Connect Discovery | https://<your-casdoor-host>/.well-known/openid-configuration/orgs/<organization-name> |
| OAuth 2.0 Authorization Server Metadata | https://<your-casdoor-host>/orgs/<organization-name>/.well-known/oauth-authorization-server |
| OAuth 2.0 Authorization Server Metadata | https://<your-casdoor-host>/.well-known/oauth-authorization-server/orgs/<organization-name> |
组织的文档与全局文档在以下字段上有所不同:
issuer为https://<your-casdoor-host>/orgs/<organization-name>。组织下应用的令牌、UserInfo 响应以及授权响应中的iss也使用该 issuer。registration_endpoint包含组织信息,与应用文档中的情况一致。scopes_supported会添加该组织下所有应用的自定义 Scopes。
例如,像 Stalwart 这样的邮件服务器只有在 scopes_supported 列出了 urn:ietf:params:oauth:scope:mail 及其服务的其他 scope 时,才会向邮件客户端发布 issuer。请将这些 scope 添加到邮件服务器的应用的 Scopes 中,并在邮件服务器中设置组织的 issuer。
对于没有独立 issuer 的组织,这些端点会返回 404。已经针对组织的应用验证全局 issuer 的客户端,在你开启此功能后需要切换到新的 issuer。
UserInfo 字段
/api/userinfo 端点返回以下字段。表格显示了每个字段来源于 Casdoor 用户的哪个字段。
| Casdoor 用户字段 | OIDC用户信息字段 |
|---|---|
| Id | sub |
| originBackend | iss |
| Aud | aud |
| Name | preferred_username |
| DisplayName | name |
| Avatar | picture |
| Location | address |
| Phone | phone |
映射定义在 object/user.go 中。
/api/userinfo 将 address 声明作为来自用户 Location 字段的纯字符串返回,例如 "New York"。采用 JWT-Standard 格式的令牌将 address 返回为根据用户 Address 数组构建的结构化 OIDC 地址对象。参见 OIDC address claim。