casbin-caddy-authz is an authorization middleware for Caddy, based on Apache Casbin. It controls access to your web resources by enforcing authorization policies defined with Apache Casbin.
⚠️ Important: This plugin handles authorization only — it does NOT validate passwords or verify user identity.
You must place an authentication middleware before this plugin to verify credentials. Without it, anyone can set an arbitrary Authorization header and impersonate any user.
This plugin is designed to be used alongside a dedicated authentication layer:
Caddy’s built-in basicauth directive is recommended for HTTP Basic Authentication. For other schemes (OAuth, JWT, LDAP, etc.), use the appropriate authentication plugin and ensure it runs before authz.
If you need to validate credentials programmatically, set the CredentialValidator hook before the server starts:
import authz "github.com/apache/casbin-caddy-authz/v3"
func init() {
authz.CredentialValidator = func(username, password string) bool {
// query your database or LDAP here
return myDB.CheckPassword(username, password)
}
}
How Access Control Works
Authorization is determined based on {subject, object, action}:
Field
Meaning
subject
The authenticated user name (extracted from HTTP Basic Auth header)
object
The URL path of the requested resource, e.g. dataset1/item1
casbin-caddy-authz
casbin-caddy-authz is an authorization middleware for Caddy, based on Apache Casbin. It controls access to your web resources by enforcing authorization policies defined with Apache Casbin.
Installation
Simple Example
Caddyfile Syntax
Or using global options to control directive ordering:
The
authzdirective takes two arguments:.conf) — describes the access control model (ACL, RBAC, ABAC, etc.).csv) — describes the authorization rulesFor how to write these files, refer to the Apache Casbin documentation.
Security: Authentication vs Authorization
This plugin is designed to be used alongside a dedicated authentication layer:
Caddy’s built-in
basicauthdirective is recommended for HTTP Basic Authentication. For other schemes (OAuth, JWT, LDAP, etc.), use the appropriate authentication plugin and ensure it runs beforeauthz.If you need to validate credentials programmatically, set the
CredentialValidatorhook before the server starts:How Access Control Works
Authorization is determined based on
{subject, object, action}:subjectobjectdataset1/item1actionGET,POST,PUT,DELETEWorking Example
Build Caddy with this plugin using xcaddy:
Place your Apache Casbin model file authz_model.conf and policy file authz_policy.csv in a known directory.
Add the
authzdirective to yourCaddyfile:Run
caddyand enjoy.Getting Help
License
This project is under Apache 2.0 License. See the LICENSE file for the full license text.