Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Casbin -> Fiber v3 #1116

Open
wants to merge 2 commits into
base: v3-beta
Choose a base branch
from
Open
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
50 changes: 28 additions & 22 deletions casbin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,24 @@ id: casbin

Casbin middleware for Fiber.

**Note: Requires Go 1.18 and above**
> **Note**
Requires Go 1.18 and above

## Install
```
go get -u github.com/gofiber/fiber/v2

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

choose an adapter from [here](https://casbin.org/docs/adapters)

```shell
go get -u github.com/casbin/xorm-adapter
```

## Signature

```go
casbin.New(config ...casbin.Config) *casbin.Middleware
```
Expand All @@ -36,11 +41,12 @@ casbin.New(config ...casbin.Config) *casbin.Middleware
| 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)

Expand All @@ -50,7 +56,7 @@ casbin.New(config ...casbin.Config) *casbin.Middleware
package main

import (
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v3"
"github.com/gofiber/contrib/casbin"
_ "github.com/go-sql-driver/mysql"
"github.com/casbin/xorm-adapter/v2"
Expand All @@ -62,23 +68,23 @@ 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
},
authz.RequiresPermissions([]string{"blog:create"}, casbin.WithValidationRule(casbin.MatchAllRule)),
)

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
},
authz.RequiresPermissions([]string{"blog:create", "blog:delete"}, casbin.WithValidationRule(casbin.AtLeastOneRule)),
)

app.Listen(":8080")
Expand All @@ -91,7 +97,7 @@ func main() {
package main

import (
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v3"
"github.com/gofiber/contrib/casbin"
_ "github.com/go-sql-driver/mysql"
"github.com/casbin/xorm-adapter/v2"
Expand All @@ -103,17 +109,17 @@ 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
},
authz.RoutePermission(),
)

app.Listen(":8080")
Expand All @@ -126,7 +132,7 @@ func main() {
package main

import (
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v3"
"github.com/gofiber/contrib/casbin"
_ "github.com/go-sql-driver/mysql"
"github.com/casbin/xorm-adapter/v2"
Expand All @@ -138,16 +144,16 @@ 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
},
authz.RequiresRoles([]string{"admin"}),
)

app.Listen(":8080")
Expand Down
11 changes: 5 additions & 6 deletions casbin/casbin.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package casbin
import (
"fmt"

"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v3"
)

// Middleware ...
Expand All @@ -15,7 +15,7 @@ type Middleware struct {
func New(config ...Config) *Middleware {
cfg, err := configDefault(config...)
if err != nil {
panic(fmt.Errorf("Fiber: casbin middleware error -> %w", err))
panic(fmt.Errorf("fiber: casbin middleware error -> %w", err))
}

return &Middleware{
Expand All @@ -28,7 +28,7 @@ func New(config ...Config) *Middleware {
func (m *Middleware) RequiresPermissions(permissions []string, opts ...Option) fiber.Handler {
options := optionsDefault(opts...)

return func(c *fiber.Ctx) error {
return func(c fiber.Ctx) error {
if len(permissions) == 0 {
return c.Next()
}
Expand Down Expand Up @@ -59,7 +59,6 @@ func (m *Middleware) RequiresPermissions(permissions []string, opts ...Option) f
}
return m.config.Forbidden(c)
}

return c.Next()
}
}
Expand All @@ -68,7 +67,7 @@ func (m *Middleware) RequiresPermissions(permissions []string, opts ...Option) f
// subject has the required permissions according to predefined Casbin policies.
// This method uses http Path and Method as object and action.
func (m *Middleware) RoutePermission() fiber.Handler {
return func(c *fiber.Ctx) error {
return func(c fiber.Ctx) error {
sub := m.config.Lookup(c)
if len(sub) == 0 {
return m.config.Unauthorized(c)
Expand All @@ -89,7 +88,7 @@ func (m *Middleware) RoutePermission() fiber.Handler {
func (m *Middleware) RequiresRoles(roles []string, opts ...Option) fiber.Handler {
options := optionsDefault(opts...)

return func(c *fiber.Ctx) error {
return func(c fiber.Ctx) error {
if len(roles) == 0 {
return c.Next()
}
Expand Down
Loading
Loading