Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/ISSUE_TEMPLATE/bug-report.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ body:
value: |
package main

import "github.com/gofiber/contrib/%package%"
// Replace <MAJOR> with the module's current major version (omit the `/v<MAJOR>` suffix entirely for v1 modules).
import "github.com/gofiber/contrib/%package%/v<MAJOR>"

func main() {
// Steps to reproduce
Expand Down
3 changes: 2 additions & 1 deletion .github/ISSUE_TEMPLATE/feature-request.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ body:
value: |
package main

import "github.com/gofiber/contrib/%package%"
// Replace <MAJOR> with the module's current major version (omit the `/v<MAJOR>` suffix entirely for v1 modules).
import "github.com/gofiber/contrib/%package%/v<MAJOR>"

func main() {
// Steps to reproduce
Expand Down
3 changes: 2 additions & 1 deletion .github/ISSUE_TEMPLATE/question.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ body:
value: |
package main

import "github.com/gofiber/contrib/%package%"
// Replace <MAJOR> with the module's current major version (omit the `/v<MAJOR>` suffix entirely for v1 modules).
import "github.com/gofiber/contrib/%package%/v<MAJOR>"

func main() {
// Steps to reproduce
Expand Down
42 changes: 22 additions & 20 deletions casbin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@ id: casbin

Casbin middleware for Fiber.

**Compatible with Fiber v3.**

**Note: Requires Go 1.25 and above**

## Install
```
```sh
go get -u github.com/gofiber/fiber/v3
go get -u github.com/gofiber/contrib/casbin
go get -u github.com/gofiber/contrib/casbin/v2
```
choose an adapter from [here](https://casbin.org/docs/adapters)
```
```sh
go get -u github.com/casbin/xorm-adapter
```

Expand All @@ -29,18 +31,18 @@ casbin.New(config ...casbin.Config) *casbin.Middleware

## Config

| Property | Type | Description | Default |
|:--------------|:--------------------------|:-----------------------------------------|:--------------------------------------------------------------------|
| ModelFilePath | `string` | Model file path | `"./model.conf"` |
| Property | Type | Description | Default |
|:--------------|:--------------------------|:-----------------------------------------|:--------------------------------------------------------------|
| ModelFilePath | `string` | Model file path | `"./model.conf"` |
| PolicyAdapter | `persist.Adapter` | Database adapter for policies | `./policy.csv` |
| Enforcer | `*casbin.Enforcer` | Custom casbin enforcer | `Middleware generated enforcer using ModelFilePath & PolicyAdapter` |
| Lookup | `func(*fiber.Ctx) string` | Look up for current subject | `""` |
| Unauthorized | `func(*fiber.Ctx) error` | Response body for unauthorized responses | `Unauthorized` |
| Forbidden | `func(*fiber.Ctx) error` | Response body for forbidden responses | `Forbidden` |
| Lookup | `func(fiber.Ctx) string` | Look up for current subject | `""` |
| Unauthorized | `func(fiber.Ctx) error` | Response body for unauthorized responses | `Unauthorized` |
| Forbidden | `func(fiber.Ctx) error` | Response body for forbidden responses | `Forbidden` |

### Examples
- [Gorm Adapter](https://github.com/svcg/-fiber_casbin_demo)
- [File Adapter](https://github.com/gofiber/contrib/casbin/tree/master/example)
- [File Adapter](https://github.com/gofiber/contrib/tree/master/casbin/example)

## CustomPermission

Expand All @@ -49,7 +51,7 @@ package main

import (
"github.com/gofiber/fiber/v3"
"github.com/gofiber/contrib/casbin"
"github.com/gofiber/contrib/casbin/v2"
_ "github.com/go-sql-driver/mysql"
"github.com/casbin/xorm-adapter/v2"
)
Expand All @@ -60,21 +62,21 @@ func main() {
authz := casbin.New(casbin.Config{
ModelFilePath: "path/to/rbac_model.conf",
PolicyAdapter: xormadapter.NewAdapter("mysql", "root:@tcp(127.0.0.1:3306)/"),
Lookup: func(c *fiber.Ctx) string {
Lookup: func(c fiber.Ctx) string {
// fetch authenticated user subject
},
})

app.Post("/blog",
authz.RequiresPermissions([]string{"blog:create"}, casbin.WithValidationRule(casbin.MatchAllRule)),
func(c *fiber.Ctx) error {
func(c fiber.Ctx) error {
// your handler
},
)

app.Delete("/blog/:id",
authz.RequiresPermissions([]string{"blog:create", "blog:delete"}, casbin.WithValidationRule(casbin.AtLeastOneRule)),
func(c *fiber.Ctx) error {
func(c fiber.Ctx) error {
// your handler
},
)
Expand All @@ -90,7 +92,7 @@ package main

import (
"github.com/gofiber/fiber/v3"
"github.com/gofiber/contrib/casbin"
"github.com/gofiber/contrib/casbin/v2"
_ "github.com/go-sql-driver/mysql"
"github.com/casbin/xorm-adapter/v2"
)
Expand All @@ -101,15 +103,15 @@ func main() {
authz := casbin.New(casbin.Config{
ModelFilePath: "path/to/rbac_model.conf",
PolicyAdapter: xormadapter.NewAdapter("mysql", "root:@tcp(127.0.0.1:3306)/"),
Lookup: func(c *fiber.Ctx) string {
Lookup: func(c fiber.Ctx) string {
// fetch authenticated user subject
},
})

// check permission with Method and Path
app.Post("/blog",
authz.RoutePermission(),
func(c *fiber.Ctx) error {
func(c fiber.Ctx) error {
// your handler
},
)
Expand All @@ -125,7 +127,7 @@ package main

import (
"github.com/gofiber/fiber/v3"
"github.com/gofiber/contrib/casbin"
"github.com/gofiber/contrib/casbin/v2"
_ "github.com/go-sql-driver/mysql"
"github.com/casbin/xorm-adapter/v2"
)
Expand All @@ -136,14 +138,14 @@ func main() {
authz := casbin.New(casbin.Config{
ModelFilePath: "path/to/rbac_model.conf",
PolicyAdapter: xormadapter.NewAdapter("mysql", "root:@tcp(127.0.0.1:3306)/"),
Lookup: func(c *fiber.Ctx) string {
Lookup: func(c fiber.Ctx) string {
// fetch authenticated user subject
},
})

app.Put("/blog/:id",
authz.RequiresRoles([]string{"admin"}),
func(c *fiber.Ctx) error {
func(c fiber.Ctx) error {
// your handler
},
)
Expand Down
2 changes: 1 addition & 1 deletion casbin/go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/gofiber/contrib/casbin
module github.com/gofiber/contrib/casbin/v2

go 1.25.0

Expand Down
Loading
Loading