开放的 Casbin API
介绍
让我们假设你的应用前端已经获取到了已登录用户的 access_token
,并且现在希望为用户的某些访问进行权限鉴定。 你不能简单地将 access_token
放置到 HTTP 请求头中来使用这些 API,因为 Casdoor 使用 Authorization
字段来检查访问权限。 像其他由 Casdoor 提供的 API 一样,Authorization
字段由应用的 client id 和 secret 组成,使用基本 HTTP 认证方案。 它看起来像 Basic XXX
。 因此,Casbin API 应当被应用的后端服务器调用。 以下是关于如何使用的步骤。
- 前端通过 HTTP 请求头将
access_token
传递到后端服务器。 - 后端服务器从
access_token
中获取用户 ID,这也是下文所描述的 API 中的参数v0
。
提前说明,这些接口也几乎是为 (sub, obj, act)
模型所设计的(就现阶段而言)。 接口中的 id
为所应用的权限策略标识,由组织名和权限策略名组成(即 organization name/permission name
)。 v0
、v1
、v2
依次对应权限模型所描述的策略结构,通常分别代表sub
、obj
、act
。
除了请求强制执行权限控制的 API 接口以外,Casdoor 也提供了其它一些有助于外部应用获取权限策略信息的接口,也一并列在此处。
Enforce
请求:
curl --location --request POST 'http://localhost:8000/api/enforce' \
--header 'Content-Type: application/json' \
--header 'Authorization: Basic client_id_and_secret' \
--data-raw '{"id":"example-org/example-permission", "v0":"example-org/example-user", "v1":"example-resource", "v2":"example-action"}'
响应:
true
BatchEnforce
请求:
curl --location --request POST 'http://localhost:8000/api/batch-enforce' \
--header 'Content-Type: application/json' \
--header 'Authorization: Basic client_id_and_secret' \
--data-raw '[{"id":"example-org/example-permission", "v0":"example-org/example-user1", "v1":"example-resource", "v2":"example-action"}, {"id":"example-org/example-permission", "v0":"example-org/example-user2", "v1":"example-resource", "v2":"example-action"}, {"id":"example-org/example-permission", "v0":"example-org/example-user3", "v1":"example-resource", "v2":"example-action"}]'
响应:
[
true,
true,
false
]
GetAllObjects
请求:
curl --location --request GET 'http://localhost:8000/api/get-all-objects' \
--header 'Authorization: Basic client_id_and_secret'
响应:
[
"app-built-in"
]
GetAllActions
请求:
curl --location --request GET 'http://localhost:8000/api/get-all-actions' \
--header 'Authorization: Basic client_id_and_secret'
响应:
[
"read",
"write",
"admin"
]
GetAllRoles
请求:
curl --location --request GET 'http://localhost:8000/api/get-all-roles' \
--header 'Authorization: Basic client_id_and_secret'
响应:
[
"role_kcx66l"
]