Casbin
Authorization library for Go supporting ACL, RBAC and ABAC models expressed as configuration.
Official Documentation ↗casbin v2.90 / Go 1.20+. Check the official docs for the latest API surface.Installation
go get github.com/casbin/casbin/v2Methods
No methods match this filter.
Overview
Casbin separates authorization logic from application code using declarative models and policies. The same enforcer API supports ACL, RBAC and ABAC configurations.
Core Methods
Load a model and policy into an enforcer, call Enforce with the request attributes, and manage policies with AddPolicy and RemovePolicy. Adapters persist policies to files or databases.
Usage Notes
Model permissions declaratively, evaluate with Enforce on each request, and store policies in a shared adapter for multi-instance deployments. Test models with a policy test harness.
Common Pitfalls
Forgetting SavePolicy loses runtime changes. Loading policies on every request is slow, so cache the enforcer. An under-specified model can grant more than intended, so review matcher rules carefully.
FAQ
- How do ACL, RBAC and ABAC differ?
- ACL lists subjects directly, RBAC maps users to roles then roles to permissions, and ABAC evaluates attributes. Switching models is largely a matter of editing the model configuration.
- Where are policies stored?
- Policies load from files or a database adapter. Runtime changes must be saved or the adapter updated to persist, and in distributed systems a shared adapter keeps instances consistent.
- How do I model resource hierarchies?
- Use role or resource matching functions in the model so a rule for a parent path applies to children, which avoids enumerating every resource.
Report a problem with this API docOpens a pre-filled issue on GitHub.