TwoGenIdentity Research
Keycloak Cedar Policy Engine
Cedar as a native policy type in Keycloak Authorization Services, with decisions served through the OpenID AuthZEN endpoint.
One readable, validated policy language for users and AI agents, evaluated inside Keycloak with no external PDP.
Use Case
Fine-grained access for users, APIs and MCP tools
Goals
Readable, testable policies inside Keycloak
Benefits
Policy as code, forbid guardrails, fail closed
Standards
OpenID AuthZEN, Cedar
Why we're exploring this
Authorization logic often ends up spread across application code, gateway configuration and proprietary policy formats, which makes it hard to answer simple questions: who can do what, why was a request allowed, and what changes when a policy is updated? This research brings an open standards stack to Keycloak: OpenID AuthZEN for the authorization decision API and Cedar, an open-source policy language with formally verified semantics, for policy definition and evaluation, integrated natively into Keycloak Authorization Services.
- → Decoupled authorization: applications, gateways and agents enforce decisions without embedding authorization logic.
- → Policy as code: policies are versioned, reviewed and tested like any other software artifact, and validated against a schema before they are used.
- → Fine-grained decisions: identity, roles, resource attributes and authentication context are evaluated together.
- → A standard interface: every PEP (application API, API gateway, agent gateway or MCP server) uses the same AuthZEN API.
- → Auditability and safe guardrails: policy evaluation is separated from enforcement, every decision points to the statement that made it, and a single forbid overrides any permit.
How a decision flows
Every request, from a user or an AI agent, goes through the same path. The PEP enforces, Keycloak decides, and Cedar evaluates the policies.
PEP
API Gateway, Agent Gateway or MCP Server
Keycloak (AuthZEN PDP)
AuthZEN endpoint
Authorization Services
Cedar policy provider
in-process via cedar-java, no external PDP
From AuthZEN to Cedar
Each part of the AuthZEN request becomes part of the Cedar request. Each resource server describes its own resources, actions and context in a Cedar schema, so policies use your names.
| AuthZEN | Keycloak | Cedar |
|---|---|---|
| subject | User, or the service account of a client | Keycloak::User / Keycloak::Client |
| resource | Resource (type and name) | Documents::Document::"document-1" |
| action | Authorization scope | Documents::Action::"read" |
| context | Evaluation context | context, declared keys only |
Design principles
Principal from Keycloak
Roles, groups and attributes are read from Keycloak, never taken from the request. A caller cannot grant itself a role.
Typed context from the PEP
Login facts (acr, amr) and token scopes come from the PEP. Cedar only receives the keys the schema declares, with their types.
One policy set per resource server
All Cedar policies of a resource server decide together, so a forbid in any of them overrides every permit.
Fail closed
Invalid policies are rejected on write, and any error at evaluation time results in a deny, never an allow.
One policy language, users and agents
The same engine evaluates a user's access and an AI agent's tool call, with no separate code path. Each use case shows its Cedar policy and the decisions Keycloak returns through AuthZEN.
Use case 1 · Users
Passkey login required to write
Editors may change a document only when they logged in with a passkey. The role comes from Keycloak; how the user logged in (acr, amr) is sent by the PEP in the AuthZEN context.
@id("editors-write-with-passkey")
permit (
principal in Keycloak::Role::"document-editor",
action == Documents::Action::"write",
resource is Documents::Document
)
when {
context has acr &&
context.acr == "inherence" &&
context has amr &&
context.amr.contains("passkeys")
};Decisions
| Subject | Action | Context | Decision | Decided by |
|---|---|---|---|---|
| carol (document-editor) | write | acr inherence, amr ["passkeys","mfa"] | allow | editors-write-with-passkey |
| carol (document-editor) | write | acr inherence, amr ["pwd"] | deny | no permit matches (default deny) |
Use case 2 · AI agents
An AI agent calling MCP tools
A gateway in front of an MCP server asks Keycloak before every tools/call. Support agents may call the customer read tools when their token grants mcp:tools, tool admins may call every tool, and no agent may ever delete a customer.
@id("support-agents-call-customer-read-tools")
permit (
principal is Keycloak::Client,
action == Mcp::Action::"call",
resource is Mcp::Tool
)
when {
resource in [Mcp::Tool::"get_customer",
Mcp::Tool::"list_customers"] &&
principal.roles.contains("support-agent") &&
context has scope &&
context.scope.contains("mcp:tools")
};
@id("tool-admins-call-all-tools")
permit (
principal is Keycloak::Client,
action == Mcp::Action::"call",
resource is Mcp::Tool
)
when { principal.roles.contains("tool-admin") };
@id("no-agent-calls-delete-customer")
forbid (
principal is Keycloak::Client,
action == Mcp::Action::"call",
resource == Mcp::Tool::"delete_customer"
);Decisions
| Agent role | Tool | Scope in context | Decision | Decided by |
|---|---|---|---|---|
| support-agent | get_customer | ["mcp:tools"] | allow | support-agents-call-customer-read-tools |
| support-agent | get_customer | missing | deny | no permit matches (default deny) |
| support-agent | delete_customer | ["mcp:tools"] | deny | no-agent-calls-delete-customer |
| tool-admin | delete_customer | any | deny | no-agent-calls-delete-customer (forbid overrides the admin permit) |
→ Policies as code: schemas and policies live in Git, one folder per resource server, checked with the Cedar CLI and applied through the Keycloak Admin API.
What's next
- → Delegated authorization: Agents acting on behalf of a user, with the actor carried through OAuth 2.0 Token Exchange (RFC 8693).
- → Workload identity for agents: Federating agent workload identities such as SPIFFE IDs into Keycloak, and using them in Cedar policies.
- → Multi-agent security: Authorizing chains of agents and tools, so each step is checked against the same policies.
Related
Agentic AI Security
How we secure AI agents as they access identities, APIs, tools and enterprise systems.
AI / API AuthZEN Gateway Security
A gateway acting as the AuthZEN PEP for APIs and MCP servers.
Intent Agent Native Authorization (IANA-AP)
Runtime authorization that binds every agent tool call to a user-approved intent.
Experimental
The extension is open source and experimental, not yet recommended for production. TwoGenIdentity helps teams design, implement and support fine-grained authorization with Keycloak and Cedar.
Talk to us about it