メインコンテンツにスキップ

標準 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 に切り替えることができます。

  1. ライブラリを選択します。例えば:

    OIDCクライアントライブラリ言語リンク
    go-oidcGohttps://github.com/coreos/go-oidc
    pac4j-oidcJavahttps://www.pac4j.org/docs/clients/openid-connect.html

    その他のライブラリについては、oauth.net/code および 認証済み OpenID 開発者ツール を参照してください。

  2. ライブラリに以下の値を指定します:

    SettingValue
    Issuer or discovery URLhttps://<your-casdoor-host>. The library appends /.well-known/openid-configuration
    Client IDClient ID of the Casdoor application
    Client secretClient secret of the Casdoor application
    Redirect URLCallback URL of your application. It must be listed in Redirect URLs of the Casdoor application
    Scopesopenid, plus profile and email as needed

ライブラリはディスカバリドキュメントから Casdoor のエンドポイントと機能を読み取するため、これらを手動で設定する必要はありません。

ディスカバリエンドポイント​

Casdoor はメタデータを 2 つの URL で公開しています。どちらも同じドキュメントを返します。

StandardURL
OpenID Connect Discoveryhttps://<your-casdoor-host>/.well-known/openid-configuration
OAuth 2.0 Authorization Server Metadata (RFC 8414)https://<your-casdoor-host>/.well-known/oauth-authorization-server

クライアントが OAuth 2.0 のみをサポートしている場合は、2 番目の 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
}

このドキュメントはクライアントに以下を伝えます:

  • Grant types: Casdoor は認可コード、インプリシット、パスワード、クライアントクレデンシャル、リフレッシュトークンの各グラントをサポートしています。また、スマート TV や CLI ツールなど入力手段が限られたデバイス向けにデバイスコードグラント (urn:ietf:params:oauth:grant-type:device_code)、トークン交換、そして独自のカスタムグラントである検証コードグラント と モバイルワンクリックグラント もサポートしています。
  • Response types and modes: response_types_supported には、認可コード、インプリシット、およびハイブリッドの組み合わせがリストされています。response_modes_supported には、3 つのレスポンモードがリストされています。
  • Client authentication: token_endpoint_auth_methods_supported は、クライアントが Authorization ヘッダーまたはボディ内のシークレットで認証するか、署名付き JWT (private_key_jwt、連合資格情報を参照) で認証するか、PKCE を使用する公開クライアントとして認証しない (none) かを示します。
  • PKCE: code_challenge_methods_supported は、Casdoor が S256 メソッドによる Proof Key for Code Exchange (PKCE) をサポートしていることを示します。PKCE は、モバイルアプリや SPA(シングルページアプリケーション)などの公開クライアントにおいて、認可コードのインターセプションから保護します。PKCE をサポートするライブラリは、このメタデータから自動的に PKCE を有効にします。PKCE を自前で実装する場合は、OAuth 2.0 を参照してください。
  • Issuer in the authorization response: authorization_response_iss_parameter_supported は true です。これは、Casdoor がすべての認可レスポンスに iss を追加する (RFC 9207) ためです。
  • Client ID metadata documents: client_id_metadata_document_supported は、built-in 組織がメタデータドキュメントの URL としてのクライアント ID を受け入れる場合に true になります。動的クライアント登録を参照してください。

単一アプリケーションのディスカバリエンドポイント​

各アプリケーションにも独自のディスカバリエンドポイントがあります。マルチテナントデプロイなどでアプリケーションが独自のコピーでトークンを署名する場合や、アプリケーションごとに新しいコピーへ移行する場合などに使用してください。

EndpointURL
OpenID Connect Discoveryhttps://<your-casdoor-host>/.well-known/<application-name>/openid-configuration
OAuth 2.0 Authorization Server Metadatahttps://<your-casdoor-host>/.well-known/<application-name>/oauth-authorization-server
JSON Web Key Set (JWKS)https://<your-casdoor-host>/.well-known/<application-name>/jwks
WebFingerhttps://<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_endpoint には、https://door.casdoor.com/api/oauth/register?organization=<organization-name> のようにアプリケーションの組織が含まれます。これにより、動的クライアント登録を通じて登録されるクライアントはその組織に登録されます。グローバルドキュメントには organization パラメータがなく、クライアントは built-in に登録されます。
  • 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 を付加したものではありません。そのため、クライアントが発行者 URL のみを取得して独自にディスカバリー URL を構築する場合、そのドキュメントを見つけることができません。このようなクライアントに対しては、組織固有の発行者を提供してください。

組織のディスカバリーエンドポイント​

組織の編集ページで Use organization issuer を有効にすると、その組織が独自の発行者として機能し、https://<your-casdoor-host>/orgs/<organization-name> となります。発行者に /.well-known/openid-configuration を付加する、または RFC 8414 に従ってパスの前に /.well-known/oauth-authorization-server を挿入するクライアントは、組織のドキュメントを検出できます:

EndpointURL
OpenID Connect Discoveryhttps://<your-casdoor-host>/orgs/<organization-name>/.well-known/openid-configuration
OpenID Connect Discoveryhttps://<your-casdoor-host>/.well-known/openid-configuration/orgs/<organization-name>
OAuth 2.0 Authorization Server Metadatahttps://<your-casdoor-host>/orgs/<organization-name>/.well-known/oauth-authorization-server
OAuth 2.0 Authorization Server Metadatahttps://<your-casdoor-host>/.well-known/oauth-authorization-server/orgs/<organization-name>

組織のドキュメントは、以下のフィールドにおいてグローバルなドキュメントと異なります:

  • issuer は https://<your-casdoor-host>/orgs/<organization-name> です。この組織のアプリケーションのトークン、UserInfo レスポンス、および認可レスポンスに含まれる iss も、この発行者になります。
  • registration_endpoint には組織が含まれており、これはアプリケーションのドキュメントと同様です。
  • scopes_supported には、その組織のすべてのアプリケーションのカスタム Scopes が追加されます。

例えば、Stalwart などのメールサーバーは、scopes_supported に urn:ietf:params:oauth:scope:mail と他のサービスのスコープがリストされている場合にのみ、メールクライアントへ発行者を公開します。これらのスコープをメールサーバーのアプリケーションの Scopes に追加し、メールサーバーの設定で組織の発行者を指定してください。

独自の発行者を持たない組織の場合、これらのエンドポイントは 404 を返します。すでに組織のアプリケーションに対してグローバルな発行者を検証しているクライアントは、この機能を有効にした後、新しい発行者に切り替える必要があります。

UserInfo フィールド​

/api/userinfo エンドポイントは以下のフィールドを返します。表は、それぞれが Casdoor ユーザーのどのフィールド由来かを示しています。

CasdoorユーザーフィールドOIDC UserInfoフィールド
Idsub
originBackendiss
Audaud
Namepreferred_username
DisplayNamename
Emailemail
Avatarpicture
Locationaddress
Phonephone

マッピングは object/user.go で定義されています。

注

/api/userinfo は、ユーザーの Location フィールドからのプレーン文字列として address クレームを返します。例えば "New York" です。JWT-Standard フォーマットのトークンは、ユーザーの Address 配列から構築された構造化された OIDC アドレスオブジェクトとして address を返します。OIDC address claim を参照してください。

関連項目​