クライアントの動的登録
このガイドでは、動的クライアント登録 (DCR) について説明します。ソフトウェアが HTTP リクエスト 1 つで Casdoor の OAuth 2.0 クライアントとして自己登録するため、管理コンソールでアプリケーションを作成する必要はありません。DCR は、MCP クライアント、CLI、デスクトップアプリケーションなど、エンドユーザーに出荷するツールに適しています。これらのツールは初回実行時にクライアント認証情報を取得します。Casdoor は RFC 7591 および RFC 7592 を実装しています。
学習目標
- 組織に対して DCR を許可します。
- 登録エンドポイントを見つけ、クライアントを登録します。
- 登録内容を読み取り、更新、削除します。
- 登録済みクライアントができることを理解します。
- メタデータドキュメント URL で自身を識別するクライアントを受け入れます。
必要なもの
- Casdoor 管理コンソールへのグローバル管理者アクセス権
- HTTP リクエストを送信できるクライアント
組織に対して DCR を許可する
DCR はデフォルトでオフです。built-in を含むすべての組織が対象です。登録エンドポイントには認証が必要ないため、Casdoor は有効化されるまでこれを公開しません。
- Casdoor 管理コンソールで、組織の編集ページを開きます。
- Enable dynamic client registration をオンにします。
- 組織を保存します。
このスイッチを変更できるのはグローバル管理者のみです。組織管理者の場合、スイッチは無効になり、API 経由で送信された変更は Casdoor によって無視されます。
このスイッチは組織の dcrPolicy フィールドを設定します:
dcrPolicy | Behavior |
|---|---|
disabled, or empty | Casdoor rejects registration requests |
open | Anyone can register an application in the organization, without authentication |
DCR が必要な組織でのみ DCR を有効にしてください。多くのセットアップでは、管理者が手動で作成したアプリケーションで動作し、DCR をオフにしておくことで Casdoor インスタンスへの不正利用を防ぐことができます。
登録エンドポイントを見つける
登録エンドポイントは /api/oauth/register です。オプションの organization クエリパラメータを受け取ります。これは Casdoor がクライアントを作成する組織です。デフォルトは built-in です。
ディスカバリドキュメントはこのエンドポイントを通知しています。リクエストしてください:
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 で登録されます。他の2つの発見ドキュメントは 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 サーバーの認可サーバーとして指定します。
クライアントの登録
クライアントのメタデータを JSON として登録エンドポイントに POST リクエストを送信します:
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 も含まれており、これらはクライアントが 登録を管理するために必要です。
リクエストパラメータ
| Parameter | Required | Description |
|---|---|---|
redirect_uris | Yes | Array of callback URLs that Casdoor may redirect to after sign-in |
client_name | No | Display name of the client. Casdoor generates a name if you omit it |
grant_types | No | Grant 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_method | No | How 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_type | No | web for server-side clients, or native for desktop and mobile clients |
logo_uri | No | URL of the logo of the client |
client_uri | No | URL of the home page of the client |
policy_uri | No | URL of the privacy policy of the client |
tos_uri | No | URL of the terms of service of the client. Casdoor sets it as the Terms of Use of the application |
scope | No | Space-separated list of the scopes that the client requests |
エラー
失敗した場合、Casdoor は RFC 7591 で定義されているように、error コードと読み取り可能な error_description を含むエラーを返します:
| Error | Cause |
|---|---|
invalid_redirect_uri | redirect_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> で読み取り、更新、削除できます。各リクエストでは、登録レスポンスに含まれる registration_access_token を Authorization: Bearer <registration-access-token> ヘッダーで送信します:
| Request | Description |
|---|---|
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 はこのようなトークンでの他のリクエストを拒否します。ブラウザがそのようなリクエストが残したセッションクッキーのみを送信する場合、Casdoor はそのセッションをログアウトし、リクエストを匿名として処理します。誰でもクライアントを登録してユーザーに認可を求めることができるため、クライアントはユーザーのトークンを使用して Casdoor を管理することはできません。
ユーザーがすぐにクライアントにサインインできるようにするため、アプリケーションではパスワードによるサインインが有効になっており、組織のデフォルトアプリケーションから以下の設定を継承します:
| Setting | Inherited |
|---|---|
| Providers and sign-in methods | Yes, so that at least one sign-in method works |
| Branding | The theme, the footer HTML, and the form CSS. The logo too, unless the request contains logo_uri |
| Sign-in items | The layout of the sign-in form |
| Session and WebAuthn settings | EnableSigninSession and EnableWebAuthn |
| Token lifetimes | Token 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 から取得されます。Organizations を参照してください。
メタデータドキュメント URL で識別されるクライアントを受け入れる
クライアントは登録をスキップし、MCP クライアントが行うように、クライアント ID メタデータドキュメントを指す HTTPS URL を client_id として使用できます。このドキュメントは、登録リクエストと同じメタデータを持つ JSON ファイルです。Casdoor は draft-ietf-oauth-client-id-metadata-document に準拠します。
どの組織か
ChatGPT、Claude、VS Code などの同一のクライアントは、接続するすべての Casdoor インスタンスと組織に対して単一のクライアント ID URL を持っています。したがって、Casdoor は URL ごとに 1 つのアプリケーションを保持し、すべての組織で共有されるようにして、ユーザーがサインインする組織のアプリケーションとして提供します:
- 組織は、認可リクエストの
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は、PKCE を使用する公開クライアントの場合は空またはnone、ChatGPT のようにclient_assertionに署名するクライアントの場合は HTTPS のjwks_uriとともにprivate_key_jwtです。redirect_urisは少なくとも 1 つの完全な 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 を参照してください。