LDAP Role Manager is an LDAP-based role manager for Casbin. With this library, Casbin can load role hierarchies and user-role mappings from LDAP directories like Active Directory, OpenLDAP, etc.
go get github.com/casbin/ldap-role-manager
package main import ( "fmt" "log" "github.com/casbin/casbin/v2" ldaprolemanager "github.com/casbin/ldap-role-manager" ) func main() { // Initialize LDAP role manager opts := &ldaprolemanager.LDAPOptions{ URL: "ldap://localhost:389", BaseDN: "dc=example,dc=com", UserFilter: "(uid=%s)", GroupFilter: "(member=%s)", RoleAttr: "cn", BindDN: "cn=admin,dc=example,dc=com", BindPassword: "password", MaxHierarchyLevel: 10, } rm, err := ldaprolemanager.NewRoleManager(opts) if err != nil { log.Fatalf("Failed to create role manager: %v", err) } defer rm.Close() // Create a new enforcer e, err := casbin.NewEnforcer("examples/rbac_model.conf", "examples/rbac_policy.csv") if err != nil { log.Fatalf("Failed to create enforcer: %v", err) } // Set the role manager e.SetRoleManager(rm) // Load policy err = e.LoadPolicy() if err != nil { log.Fatalf("Failed to load policy: %v", err) } // Check permissions if res, _ := e.Enforce("alice", "data1", "read"); res { fmt.Println("alice can read data1") } else { fmt.Println("alice cannot read data1") } }
The LDAPOptions struct supports the following configuration options:
ldap://localhost:389 or ldaps://localhost:636)dc=example,dc=com)(uid=%s))(member=%s))cn)false)false)10)This role manager expects the following LDAP schema:
UserFilter (e.g., uid attribute)uid=alice,ou=users,dc=example,dc=commember attribute listing user DNsRoleAttr, default is cn)cn=admin,ou=groups,dc=example,dc=com# User entry dn: uid=alice,ou=users,dc=example,dc=com objectClass: inetOrgPerson uid: alice cn: Alice Smith sn: Smith # Group entry dn: cn=admin,ou=groups,dc=example,dc=com objectClass: groupOfNames cn: admin member: uid=alice,ou=users,dc=example,dc=com
For Active Directory, you might use different filters:
opts := &ldaprolemanager.LDAPOptions{
URL: "ldaps://ad.example.com:636",
BaseDN: "dc=example,dc=com",
UserFilter: "(sAMAccountName=%s)",
GroupFilter: "(member=%s)",
RoleAttr: "cn",
BindDN: "cn=service-account,ou=users,dc=example,dc=com",
BindPassword: "password",
UseTLS: true,
}
MaxHierarchyLevelThe LDAP Role Manager implements the rbac.RoleManager interface:
GetRoles(name string, domain ...string) ([]string, error) - Get roles for a userGetUsers(roleName string, domain ...string) ([]string, error) - Get users with a specific roleHasLink(name1 string, name2 string, domain ...string) (bool, error) - Check if a user has a roleGetImplicitRoles(name string, domain ...string) ([]string, error) - Get all roles including nested onesGetImplicitUsers(roleName string, domain ...string) ([]string, error) - Get all users with a roleNote: Methods like AddLink and DeleteLink are no-ops since roles are managed in LDAP.
Run the tests with:
go test -v ./...
For tests with coverage:
go test -v -coverprofile=coverage.out ./... go tool cover -html=coverage.out
This project is licensed under the Apache License 2.0. See the LICENSE file for the full license text.