跳到主内容

动态注册客户端

本指南介绍动态客户端注册 (DCR):软件通过一次 HTTP 请求将自己注册为 Casdoor 的 OAuth 2.0 客户端,无需管理员在控制台手动创建应用。DCR 适用于分发给最终用户的工具,例如 MCP 客户端、CLI 和桌面应用程序,这些工具在首次运行时即可获取其客户端凭据。Casdoor 实现了 RFC 7591 和 RFC 7592。


学习目标​

  • 为组织启用 DCR。
  • 查找注册端点并注册客户端。
  • 读取、更新和删除注册信息。
  • 了解已注册客户端可以执行的操作。
  • 接受通过元数据文档 URL 进行自我识别的客户端。

前提条件​

  • 拥有 Casdoor 管理控制台的超级管理员权限
  • 能够发送 HTTP 请求的客户端

为组织启用 DCR​

默认情况下,包括 built-in 在内的所有组织均未启用 DCR。由于注册端点不需要身份验证,因此只有在您开启该功能后,Casdoor 才会暴露此端点。

  1. 在 Casdoor 管理控制台中,打开组织的编辑页面。
  2. 开启 Enable dynamic client registration。
  3. 保存组织。

只有超级管理员可以更改此开关。对于组织管理员,此开关处于禁用状态,且 Casdoor 会忽略通过 API 发送的更改请求。

此开关设置组织的 dcrPolicy 字段:

dcrPolicyBehavior
disabled, or emptyCasdoor rejects registration requests
openAnyone can register an application in the organization, without authentication
注意事项

仅对需要 DCR 的组织启用此功能。许多场景使用管理员手动创建的应用即可满足需求,保持 DCR 关闭可以消除滥用您的 Casdoor 实例的一种途径。

查找注册端点​

注册端点为 /api/oauth/register。它接受一个可选的 organization 查询参数,指定 Casdoor 在其中创建客户端的组织。默认值为 built-in。

当客户端注册所在的组织开启了 Enable dynamic client registration 时,发现文档会公布该端点;否则,registration_endpoint 将不会出现在文档中,以便客户端请求客户端 ID 而不是因注册失败而报错。请求它:

curl https://your-casdoor.com/.well-known/openid-configuration

读取 registration_endpoint 的值:

{
"issuer": "https://your-casdoor.com",
"authorization_endpoint": "https://your-casdoor.com/login/oauth/authorize",
"token_endpoint": "https://your-casdoor.com/api/login/oauth/access_token",
"registration_endpoint": "https://your-casdoor.com/api/oauth/register",
...
}

全局发现文档没有 organization 参数,因此客户端在 built-in 中注册。另外两个发现文档返回 https://your-casdoor.com/api/oauth/register?organization=<organization>,以便客户端在该组织中注册:

  • 应用的发现文档 /.well-known/<application>/openid-configuration,其中包含应用所属的组织。
  • 启用了 Use organization issuer 的组织的发现文档,路径为 /.well-known/oauth-authorization-server/orgs/<organization> 或 /.well-known/openid-configuration/orgs/<organization>。其颁发者为 https://your-casdoor.com/orgs/<organization>。MCP 网关将此颁发者指定为该组织 MCP 服务器的授权服务器。

注册客户端​

向注册端点发送一个 POST 请求,请求体为包含客户端元数据的 JSON:

curl -X POST https://your-casdoor.com/api/oauth/register \
-H "Content-Type: application/json" \
-d '{
"client_name": "Claude Desktop",
"redirect_uris": ["http://localhost:3000/callback"],
"grant_types": ["authorization_code", "refresh_token"],
"token_endpoint_auth_method": "none",
"application_type": "native"
}'

响应中包含新客户端的凭据:

{
"client_id": "a1b2c3d4e5f6",
"client_secret": "secret_xyz789...",
"client_id_issued_at": 1737799294,
"client_secret_expires_at": 0,
"redirect_uris": ["http://localhost:3000/callback"],
"grant_types": ["authorization_code", "refresh_token"],
"token_endpoint_auth_method": "none",
"application_type": "native"
}

请安全地存储 client_id 和 client_secret。客户端将在后续的所有 OAuth 2.0 流程中使用它们。响应中还包含 registration_client_uri 和 registration_access_token,客户端需要使用这些信息来管理其注册。

请求参数​

ParameterRequiredDescription
redirect_urisYesArray of callback URLs that Casdoor may redirect to after sign-in
client_nameNoDisplay name of the client. Casdoor generates a name if you omit it
grant_typesNoGrant types that the client uses: authorization_code, refresh_token, urn:ietf:params:oauth:grant-type:device_code, or urn:ietf:params:oauth:grant-type:jwt-bearer, which a registered client can use only to redeem an ID-JAG. Casdoor drops any other value without an error. The default is ["authorization_code"]
token_endpoint_auth_methodNoHow the client authenticates at the token endpoint: none, client_secret_post, client_secret_basic, or private_key_jwt
jwks_uri使用 private_key_jwt 时用于签署客户端 client_assertion 的密钥集的 HTTPS URL(RFC 7523)
application_typeNoweb for server-side clients, or native for desktop and mobile clients
logo_uriNoURL of the logo of the client
client_uriNoURL of the home page of the client
policy_uriNoURL of the privacy policy of the client
tos_uriNoURL of the terms of service of the client. Casdoor sets it as the Terms of Use of the application
scopeNoSpace-separated list of the scopes that the client requests

错误​

如果失败,Casdoor 将按照 RFC 7591 的定义返回错误,包含 error 代码和可读的 error_description:

ErrorCause
invalid_redirect_uriredirect_uris is missing, or an entry uses the javascript:, data:, or vbscript: scheme
invalid_client_metadata该组织未启用 DCR,组织不存在,或参数格式错误。logo_uri、client_uri、policy_uri 和 tos_uri 必须是绝对的 http 或 https URL

管理注册​

客户端可以在 /api/oauth/register/<client-id> 处读取、更新和删除其自身的注册信息。每个请求都需在 Authorization: Bearer <registration-access-token> 标头中发送注册响应中的 registration_access_token:

RequestDescription
GET /api/oauth/register/<client-id>Returns the current metadata of the client
PUT /api/oauth/register/<client-id>Updates the metadata
DELETE /api/oauth/register/<client-id>Deletes the client

已注册客户端的身份与权限​

Casdoor 会为每个注册的客户端创建一个应用。该应用具有以下属性:

  • 它属于注册端点所在的组织,并在应用列表中显示标签 dcr、类别 Agent 和类型 MCP。
  • 其客户端密钥不会过期。管理员可以随时删除该应用。
  • 它在受限角色 app-dcr 下运行。使用其客户端凭据,它只能调用登录流程的端点:/api/login/oauth/*、/api/get-oauth-token、/api/userinfo 和 /api/get-application。它无法调用管理 API。
  • 用户通过客户端获取的令牌以及伴随的会话只能调用 /api/userinfo、/api/user、/api/mcp、/api/login/oauth 和 /api/server/*。Casdoor 会拒绝使用该令牌发出的其他请求。如果浏览器仅发送此类请求留下的会话 cookie,Casdoor 会将该会话注销并将请求视为匿名处理。任何人都可以注册客户端并请求用户授权,因此客户端不能使用用户的令牌来管理 Casdoor。

为了让用户能够立即登录到客户端,该应用启用了密码登录,并从组织的默认应用中继承了以下设置:

SettingInherited
Providers and sign-in methodsYes, so that at least one sign-in method works
BrandingThe theme, the footer HTML, and the form CSS. The logo too, unless the request contains logo_uri
Sign-in itemsThe layout of the sign-in form
Session and WebAuthn settingsEnableSigninSession and EnableWebAuthn
Token lifetimesToken expire, Refresh token expire, and Cookie expire, when they are above 0. Otherwise, the access token lasts 168 hours, and the refresh token and the cookie last 720 hours

令牌格式和令牌字段并非来自默认应用,而是来自组织的 Default token format(默认令牌格式)和 Default token fields(默认令牌字段)。参见 组织。

接受由元数据文档 URL 标识的客户端​

客户端可以跳过注册,并使用指向客户端 ID 元数据文档的 HTTPS URL 作为其 client_id,就像 MCP 客户端所做的那样。该文档是一个 JSON 文件,包含与注册请求相同的元数据。Casdoor 遵循 draft-ietf-oauth-client-id-metadata-document。

哪个组织​

同一客户端(如 ChatGPT、Claude 或 VS Code)对于其连接的所有 Casdoor 实例和组织只有一个客户端 ID URL。因此,Casdoor 为每个 URL 保留一个由所有组织共享的应用,并将其作为用户登录所在组织的应用提供服务:

  • 组织来自授权请求的 organization 参数。应用程序的发现文档,以及启用了 Use organization issuer 的组织的发现文档,会将组织放在 authorization_endpoint 的路径中,例如 https://your-casdoor.com/orgs/acme/login/oauth/authorize,它会重定向到带有 organization=acme 的登录页面。如果没有该参数,组织则为 built-in。
  • Casdoor accepts the URL only when Enable dynamic client registration is on for that organization. Otherwise, the authorization request fails with "client ID metadata documents are not accepted, enable dynamic client registration of the organization: first". The discovery document shows the setting in client_id_metadata_document_supported.
  • 用户在组织的登录页面登录,令牌携带用户所在组织的颁发者信息,刷新后依然如此。

该应用属于 built-in,启用了 Is shared,以 URL 作为其客户端 ID,名称以 cimd_ 开头,并带有标签 cimd。早期版本 Casdoor 添加到其他组织中的应用,在下一次获取时会变为这样的共享应用。

文档​

client_id URL 必须使用 https,路径不能仅为 /,且不得包含用户信息、# 片段或 . / .. 段。

在 /login/oauth/authorize 处,Casdoor 通过 GET 请求获取文档。该请求超时时间为 5 秒,不跟随重定向,并且仅访问公共互联网地址。文档必须满足以下规则:

  • 响应必须是 HTTP 200,有效的 JSON,且大小不超过 16 KB。
  • client_id 等于该 URL,且不存在 client_secret。
  • token_endpoint_auth_method 为空或 none(适用于使用 PKCE 的公共客户端),或者为 private_key_jwt 且配有 HTTPS 的 jwks_uri(适用于签署 client_assertion 的客户端,如 ChatGPT)。
  • redirect_uris 至少包含一个完整的 URL。
  • 其他元数据遵循注册请求的规则。grant_types 默认为 ["authorization_code"]。

该应用使用与已注册客户端相同的默认值和继承设置,这些设置取自 built-in 的默认应用。显示名称为 <client_name> (<host>),或者仅为主机名。如果主页与主机在同一域名下,则主页为 client_uri,否则为 https://<host>。

On later authorization requests, Casdoor fetches the document again at most once an hour and updates the name, the home page, the redirect URLs, the grant types, the terms of use, the logo, and the key set. If an application without the tag cimd already has the URL as its client ID, Casdoor uses that application as it is.

示例:MCP 客户端​

以下 JavaScript 代码发现注册端点并注册客户端:

// Discover the registration endpoint
const discovery = await fetch('https://your-casdoor.com/.well-known/openid-configuration')
.then(r => r.json());

// Register the application
const registration = await fetch(discovery.registration_endpoint, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
client_name: 'MCP Client',
redirect_uris: ['http://127.0.0.1:6437/callback'],
grant_types: ['authorization_code', 'refresh_token'],
token_endpoint_auth_method: 'none',
application_type: 'native'
})
}).then(r => r.json());

// Store credentials for OAuth flows
const { client_id, client_secret } = registration;

拥有凭据后,客户端运行授权码流程:用户在浏览器中登录,Casdoor 带着授权码重定向到回调 URL,客户端用该码交换令牌。参见 OAuth 2.0。

另请参阅​