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

Casbin API

Casdoor は Casbin エンジンを API 経由で公開しており、バックエンドから Casdoor で定義した permissions をチェックできます。

認証​

Casbin API は必ずバックエンドから呼び出し、ブラウザからは呼び出さないでください。HTTP Basic 認証 (RFC 7617) を使用して認証します。ユーザー名にはアプリケーションのクライアント ID を、パスワードにはクライアントシークレットを使用します。curl を使用する場合は、--user '<client-id>:<client-secret>' を指定します。

典型的なフロー:

  1. フロントエンドがユーザーのアクセストークンをバックエンドに送信します。
  2. バックエンドがトークンからユーザー ID を読み取ります。
  3. バックエンドがアプリケーションの資格情報とともに、権限のモデル形式(通常は [sub, obj, act])のリクエストボディで Casbin API を呼び出します。

強制​

POST /api/enforce は 1 つのリクエストをチェックします。以下のクエリパラメータのうち、ちょうど 1 つを送信してください:

ParameterChecks the request against
permissionIdOne permission: <organization>/<permission-name>
modelIdAll permissions that use the model: <organization>/<model-name>
resourceIdAll permissions for the resource
enforcerIdOne enforcer
ownerAll permissions of the organization

permissionId を使用する場合:

curl --location --request POST 'http://localhost:8000/api/enforce?permissionId=example-org/example-permission' \
--header 'Content-Type: application/json' \
--user '<client-id>:<client-secret>' \
--data-raw '["example-org/example-user", "example-resource", "example-action"]'

modelId を使用する場合:

curl --location --request POST 'http://localhost:8000/api/enforce?modelId=example-org/example-model' \
--header 'Content-Type: application/json' \
--user '<client-id>:<client-secret>' \
--data-raw '["example-org/example-user", "example-resource", "example-action"]'

resourceId を使用する場合:

curl --location --request POST 'http://localhost:8000/api/enforce?resourceId=example-org/example-resource' \
--header 'Content-Type: application/json' \
--user '<client-id>:<client-secret>' \
--data-raw '["example-org/example-user", "example-resource", "example-action"]'

レスポンス:

{
"status": "ok",
"msg": "",
"sub": "",
"name": "",
"data": [
true
],
"data2": [
"example-org/example-model/example-adapter"
]
}

modelId、resourceId、enforcerId、または owner を使用する場合、data には複数のブール値(権限ごとに 1 つ)を含めることができ、data2 には対応するモデルとアダプターのリストが含まれます。

バッチ Enforce​

POST /api/batch-enforce checks several requests at once. It takes the same query parameters as enforce, one at a time. The body is an array of requests, each in the form [sub, obj, act].

permissionId を使用する場合:

curl --location --request POST 'http://localhost:8000/api/batch-enforce?permissionId=example-org/example-permission' \
--header 'Content-Type: application/json' \
--user '<client-id>:<client-secret>' \
--data-raw '[["example-org/example-user", "example-resource", "example-action"], ["example-org/example-user2", "example-resource", "example-action"], ["example-org/example-user3", "example-resource", "example-action"]]'

modelId を使用する場合:

curl --location --request POST 'http://localhost:8000/api/batch-enforce?modelId=example-org/example-model' \
--header 'Content-Type: application/json' \
--user '<client-id>:<client-secret>' \
--data-raw '[["example-org/example-user", "example-resource", "example-action"], ["example-org/example-user2", "example-resource", "example-action"]]'

レスポンス:

{
"status": "ok",
"msg": "",
"sub": "",
"name": "",
"data": [
[
true,
true,
false
]
],
"data2": [
"example-org/example-model/example-adapter"
]
}

modelId、enforcerId、または owner を指定する場合、data には権限ごとにブール値の配列が1つ含まれ、data2 には対応するモデルとアダプターがリストされます。

すべてのオブジェクトを取得​

GET /api/get-all-objects は、ユーザーがアクセスできるオブジェクトを返します。オプションの userId パラメータでユーザーを指定します。指定しない場合、Casdoor はセッションのユーザーを使用します。

curl --location --request GET 'http://localhost:8000/api/get-all-objects?userId=example-org/example-user' \
--user '<client-id>:<client-secret>'
curl --location --request GET 'http://localhost:8000/api/get-all-objects' \
--user '<client-id>:<client-secret>'

レスポンス:

{
"status": "ok",
"msg": "",
"data": [
"app-built-in",
"example-resource"
]
}

すべてのアクションを取得​

GET /api/get-all-actions は、ユーザーが実行できるアクションを返します。userId の動作はオブジェクトの場合と同じです。

curl --location --request GET 'http://localhost:8000/api/get-all-actions?userId=example-org/example-user' \
--user '<client-id>:<client-secret>'
curl --location --request GET 'http://localhost:8000/api/get-all-actions' \
--user '<client-id>:<client-secret>'

レスポンス:

{
"status": "ok",
"msg": "",
"data": [
"read",
"write",
"admin"
]
}

すべてのロールを取得​

GET /api/get-all-roles は、ユーザーのロールを返します。userId の動作はオブジェクトの場合と同じです。

curl --location --request GET 'http://localhost:8000/api/get-all-roles?userId=example-org/example-user' \
--user '<client-id>:<client-secret>'
curl --location --request GET 'http://localhost:8000/api/get-all-roles' \
--user '<client-id>:<client-secret>'

レスポンス:

{
"status": "ok",
"msg": "",
"data": [
"role_kcx66l"
]
}

関連項目​