https://github.com/mikespook/gorbac
goRBAC provides a lightweight role-based access control (RBAC) implementation in Golang.
https://github.com/mikespook/gorbac
go rbac
Last synced: about 1 month ago
JSON representation
goRBAC provides a lightweight role-based access control (RBAC) implementation in Golang.
- Host: GitHub
- URL: https://github.com/mikespook/gorbac
- Owner: mikespook
- License: mit
- Created: 2013-12-26T10:00:41.000Z (almost 13 years ago)
- Default Branch: master
- Last Pushed: 2026-07-21T00:28:11.000Z (4 months ago)
- Last Synced: 2026-09-24T03:12:54.943Z (2 months ago)
- Topics: go, rbac
- Language: Go
- Homepage:
- Size: 135 KB
- Stars: 1,676
- Watchers: 62
- Forks: 180
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-go - gorbac - goRBAC provides a lightweight role-based access control (RBAC) implementation in Golang. - ★ 779 (Authentication and OAuth)
- awesome-auth - goRBAC - Lightweight role-based access control implementation in Go. (Authorization / <a name="authZ-golang"></a>Golang)
- cybersecurity-auth - goRBAC - Lightweight role-based access control implementation in Go. (Authorization Development / Golang)
- awesome-Char - gorbac - provides a lightweight role-based access control (RBAC) implementation in Golang. (Authentication and OAuth)
- awesome-go - gorbac - provides a lightweight role-based access control (RBAC) implementation in Golang. (Authentication and Authorization)
- awesome-go-cn - gorbac
- awesome-go-processed - gorbac - provides a lightweight role-based access control (RBAC) implementation in Golang.| (Authentication and OAuth)
- awesome-go - gorbac - provides a lightweight role-based access control (RBAC) implementation in Golang. - :arrow_down:10 - :star:401 (Authentication and OAuth)
- awesome-go - gorbac - based access control (RBAC) implementation in Golang. | 866 | 121 | 2 | (Authentication and OAuth)
- awesome-ccamel - mikespook/gorbac - goRBAC provides a lightweight role-based access control (RBAC) implementation in Golang. (Go)
- fucking-awesome-go - gorbac - provides a lightweight role-based access control (RBAC) implementation in Golang. (Authentication and Authorization)
- awesome-go-plus - gorbac - provides a lightweight role-based access control (RBAC) implementation in Golang.  (Authentication and OAuth)
- go-awesome-with-star-updatetime - gorbac - provides a lightweight role-based access control (RBAC) implementation in Golang. (Authentication and OAuth)
- awesome-go - gorbac - provides a lightweight role-based access control (RBAC) implementation in Golang. (Authentication and OAuth)
- awesome-go - gorbac - provides a lightweight role-based access control (RBAC) implementation in Golang. (Authentication and OAuth)
- awesome-go-cn - gorbac
- awesome-go - gorbac - provides a lightweight role-based access control (RBAC) implementation in Golang. (Authentication and OAuth)
- fucking-awesome-go - :octocat: gorbac - provides a lightweight role-based access control (RBAC) implementation in Golang. :star: 352 :fork_and_knife: 70 (Authentication & OAuth)
- awesome-go - gorbac - 提供了一个轻量级的基于角色的访问控制(RBAC)。 (<span id="身份验证和oauth-authentication-and-auth">身份验证和OAuth Authentication and Auth</span>)
- awesome-go - gorbac - provides a lightweight role-based access control (RBAC) implementation in Golang. (Authentication and OAuth)
- awesome-go - gorbac - provides a lightweight role-based access control (RBAC) implementation in Golang. (Authentication and OAuth)
- awesome-golang-repositories - gorbac - based access control (RBAC) implementation in Golang. (Repositories)
- awesome-go - gorbac - provides a lightweight role-based access control (RBAC) implementation in Golang. Stars:`1.6K`. (Authentication and Authorization)
- awesome-go-info - gorbac - provides a lightweight role-based access control (RBAC) implementation in Golang. (Uncategorized)
- awesome-go - gorbac - provides a lightweight role-based access control (RBAC) implementation in Golang. (Authentication and OAuth)
- awesome-go-cn - gorbac - star:734 在Golang中提供了一个轻量级的基于角色的访问控制(RBAC)实现。 (provides a lightweight role-based access control (RBAC) implementation in Golang.) (身份验证和OAuth)
- awesome-go-cn - gorbac - Golang中一个轻量级的基于角色的访问控制`(RBAC)`实现。 (认证和OAuth授权)
README
goRBAC
======
[](https://travis-ci.org/mikespook/gorbac)
[](https://godoc.org/github.com/mikespook/gorbac)
[](https://coveralls.io/github/mikespook/gorbac?branch=master)
goRBAC provides a lightweight role-based access control implementation
in Golang.
For the purposes of this package:
* an identity has one or more roles.
* a role requests access to a permission.
* a permission is given to a role.
Thus, RBAC has the following model:
* many to many relationship between identities and roles.
* many to many relationship between roles and permissions.
* roles can have parent roles (inheriting permissions).
Version
=======
The current version of goRBAC uses Go generics (Go 1.18+) and is under active development.
Install
=======
Install the package:
```bash
$ go get github.com/mikespook/gorbac/v4
```
Usage
=====
Although you can adjust the RBAC instance anytime and it's absolutely safe, the library is designed for use with two phases:
1. Preparing
2. Checking
Preparing
---------
Import the library:
```go
import "github.com/mikespook/gorbac/v4"
```
Get a new instance of RBAC (using string as the ID type):
```go
rbac := gorbac.New[string]()
```
Get some new roles:
```go
rA := gorbac.NewRole("role-a")
rB := gorbac.NewRole("role-b")
rC := gorbac.NewRole("role-c")
rD := gorbac.NewRole("role-d")
rE := gorbac.NewRole("role-e")
```
Get some new permissions:
```go
pA := gorbac.NewPermission("permission-a")
pB := gorbac.NewPermission("permission-b")
pC := gorbac.NewPermission("permission-c")
pD := gorbac.NewPermission("permission-d")
pE := gorbac.NewPermission("permission-e")
```
Add the permissions to roles:
```go
rA.Assign(pA)
rB.Assign(pB)
rC.Assign(pC)
rD.Assign(pD)
rE.Assign(pE)
```
Also, you can implement `gorbac.Permission` for your own data structure.
After initialization, add the roles to the RBAC instance:
```go
rbac.Add(rA)
rbac.Add(rB)
rbac.Add(rC)
rbac.Add(rD)
rbac.Add(rE)
```
And set the inheritance:
```go
rbac.SetParent("role-a", "role-b")
rbac.SetParents("role-b", []string{"role-c", "role-d"})
rbac.SetParent("role-e", "role-d")
```
Checking
--------
Checking the permission is easy:
```go
if rbac.IsGranted("role-a", pA, nil) &&
rbac.IsGranted("role-a", pB, nil) &&
rbac.IsGranted("role-a", pC, nil) &&
rbac.IsGranted("role-a", pD, nil) {
fmt.Println("The role-a has been granted permis-a, b, c and d.")
}
```
Advanced Checking with Assertion Functions
------------------------------------------
You can also use assertion functions for more fine-grained permission controls:
```go
assertion := func(rbac *gorbac.RBAC[string], id string, p gorbac.Permission[string]) bool {
// Custom logic to determine if permission should be granted
return true // or false based on your logic
}
if rbac.IsGranted("role-a", pA, assertion) {
fmt.Println("The role-a has been granted permission-a based on the assertion.")
}
```
Utility Functions
-----------------
goRBAC provides several built-in utility functions:
### InherCircle
Detects circular inheritance in the role hierarchy:
```go
rbac.SetParent("role-c", "role-a")
if err := gorbac.InherCircle(rbac); err != nil {
fmt.Println("A circle inheritance occurred.")
}
```
### AnyGranted
Checks if any of the specified roles have a permission:
```go
roles := []string{"role-a", "role-b", "role-c"}
if gorbac.AnyGranted(rbac, roles, pA, nil) {
fmt.Println("At least one role has permission-a.")
}
```
### AllGranted
Checks if all of the specified roles have a permission:
```go
roles := []string{"role-a", "role-b", "role-c"}
if gorbac.AllGranted(rbac, roles, pA, nil) {
fmt.Println("All roles have permission-a.")
}
```
### Walk
Iterates through all roles in the RBAC instance:
```go
handler := func(r *gorbac.Role[string], parents []string) error {
fmt.Printf("Role: %s, Parents: %v\n", r.ID, parents)
return nil
}
gorbac.Walk(rbac, handler)
```
Custom Types
------------
goRBAC supports custom types for role and permission IDs through Go generics:
```go
// Using integer IDs
rbacInt := gorbac.New[int]()
role1 := gorbac.NewRole(1)
permission1 := gorbac.NewPermission(100)
// Using custom struct IDs
type RoleID struct {
Name string
Type string
}
rbacStruct := gorbac.New[RoleID]()
roleCustom := gorbac.NewRole(RoleID{Name: "admin", Type: "system"})
permissionCustom := gorbac.NewPermission(RoleID{Name: "read", Type: "data"})
```
Persistence
-----------
The most asked question is how to persist the goRBAC instance. Please check the post [HOW TO PERSIST GORBAC INSTANCE](https://mikespook.com/2017/04/how-to-persist-gorbac-instance/) for the details.
Authors
=======
* Xing Xing [Blog](http://mikespook.com)
[@Twitter](http://twitter.com/mikespook)
Open Source - MIT Software License
==================================
See LICENSE.