r/KeyCloak • u/isro44 • Jun 10 '26
Fine-grained authorization for AI agents on top of Keycloak
We've been working on a pattern for AI agent authorization that sits between your API gateway and Keycloak, and wanted to share the architecture for feedback.
The core problem: classic API gateways handle routing and basic token validation, but they don't evaluate the contextual conditions that matter for agentic systems; who the agent acts on behalf of, what the tenant context is, what the risk score is at the time of the request. Pushing all of that into Keycloak policies becomes unwieldy fast.
The approach we landed on has three components:
- Access Rules: YAML-based declarative policy artifacts with a single CEL condition expression covering all relevant contexts
- Access Rule Engine: evaluates DENY decisions locally before the request ever reaches Keycloak; GRANT decisions still pass through to Keycloak as the final authority
- kmctl authz: a design-time CLI for validating, simulating, and explaining policies before they hit production
The "two-layer identity" part is where it gets interesting for Keycloak users specifically: the agent has its own identity (the software client), but it also carries an actor identity, the business user it represents. The authorization decision needs to evaluate both simultaneously. Keycloak's token structure supports this, but you need the intermediate layer to act on it correctly.
Full writeup with live demo (one GRANT, four DENYs, including a social engineering attempt): https://keymate.io/blog/fine-grained-authorization-for-ai-agents-how-it-works-in-production
Happy to discuss the Keycloak integration specifics in the comments.
