Перейти до основного вмісту

AuthZEN decision API

Casdoor implements the OpenID AuthZEN Authorization API 1.0. A policy enforcement point, such as an API gateway, an MCP server, or the retriever of a RAG pipeline, asks Casdoor whether a subject may do an action on a resource, and Casdoor decides with the permissions of the organization. Because the API is a standard, the enforcement point doesn't need a Casdoor SDK, and any AuthZEN client works.

For a RAG pipeline, this keeps an agent from quoting documents that the user can't read: the retriever asks which of the documents it found the user may read, and passes only those to the model.

Перед початком роботи​

  1. Create an application for the enforcement point in the organization whose permissions decide, and turn on Client credentials in Grant types.
  2. Add the permissions. Each permission has a model, its users, groups or roles, its resources, and its actions. Only enabled permissions count.

The enforcement point authenticates in one of two ways:

  • With an access token of its application from the client credentials grant, in Authorization: Bearer <token>.
  • With its client ID and secret, in Authorization: Basic <base64(client_id:client_secret)>.

Without them, Casdoor answers 401 with WWW-Authenticate: Bearer realm="authzen". When the request has an X-Request-ID header, the response has it too.

Casdoor publishes the endpoints at https://<casdoor-host>/.well-known/authzen-configuration.

Ask for one decision​

Send a POST request to https://<casdoor-host>/api/authzen/access/v1/evaluation:

{
"subject": {"type": "user", "id": "alice"},
"resource": {"type": "document", "id": "/handbook/salaries.pdf"},
"action": {"name": "read"},
"context": {}
}

The response is the decision:

{"decision": true, "context": {"permission": "my-org/permission-hr"}}

When the decision is false, context.reason_admin says why, for example no permission of the organization: my-org allows it. Show it in logs, not to the user.

Casdoor builds the request of each model of the permissions from the evaluation:

Request fieldValue
r.subsubject.id. A user or group without an organization is taken as <organization>/<id>, the way permissions name them
r.objresource.id
r.actaction.name
r.domdomain from context, from resource.properties, or from subject.properties
OthersThe field of the same name in context

When the matcher of a model reads attributes, such as r.sub.Age or r.obj.Owner, Casdoor passes properties of the subject or the resource, with Id set to its id. The decision is true when the permissions of any model allow it. A user who is forbidden or deleted is always denied.

Ask for many decisions​

Send them in one request to https://<casdoor-host>/api/authzen/access/v1/evaluations. The top-level subject, resource, action, and context are the defaults of every evaluation:

{
"subject": {"type": "user", "id": "alice"},
"action": {"name": "read"},
"evaluations": [
{"resource": {"type": "document", "id": "/handbook/holidays.pdf"}},
{"resource": {"type": "document", "id": "/handbook/salaries.pdf"}}
],
"options": {"evaluations_semantic": "execute_all"}
}
{
"evaluations": [
{"decision": true, "context": {"permission": "my-org/permission-staff"}},
{"decision": false, "context": {"reason_admin": {"en": "no permission of the organization: my-org allows it"}}}
]
}

evaluations_semantic is execute_all (default), deny_on_first_deny, or permit_on_first_permit. With the latter two, Casdoor stops at the first denial or permission. One request can have at most 1,000 evaluations.

A RAG retriever filters the chunks it found this way: one evaluation for each document, and only the documents with "decision": true go to the model.

The search endpoints answer the reverse questions. Casdoor takes the candidates from the permissions of the organization and returns those that the permissions allow:

EndpointQuestionRequired
/api/authzen/access/v1/search/subjectWhich users may do the action on the resource?resource, action
/api/authzen/access/v1/search/resourceOn which resources may the subject do the action?subject, action
/api/authzen/access/v1/search/actionWhich actions may the subject do on the resource?subject, resource
{
"subject": {"type": "user", "id": "alice"},
"action": {"name": "read"},
"resource": {"type": "document"},
"page": {"limit": 50}
}
{
"results": [
{"type": "document", "id": "/handbook/holidays.pdf"}
],
"page": {"next_token": "", "count": 1, "total": 1}
}

A page has 50 results by default and at most 1,000. Pass next_token as page.token to get the next page. The subject search returns users only, and expands roles and groups to their users. When a permission names users with a wildcard, it returns the matching users of the organization.

Tools of MCP servers​

An MCP server that isn't behind the MCP gateway can still apply the tool rules of an MCP server registered in Casdoor: its allowlist and the untrusted content policy. Ask with a resource of type mcp_tool:

{
"subject": {"type": "user", "id": "alice"},
"resource": {"type": "mcp_tool", "id": "my-org/github-server/create_pull_request"},
"action": {"name": "call"},
"context": {"client_id": "<client-id-of-the-agent>", "trace_id": "4bf92f35"}
}

The id is <organization>/<server>/<tool>, or the tool name with the server in properties.server. context.client_id names the agent, and defaults to the client of the enforcement point. Casdoor denies a tool that isn't allowed on the server. When a permitted tool reads untrusted content, Casdoor takes it as called, as the gateway does. When the user has to approve the call, the decision is false with "requires_approval": true in context.