Skip to content

Commit cc83e87

Browse files
1 parent 5803148 commit cc83e87

File tree

21 files changed

+3311
-19
lines changed

21 files changed

+3311
-19
lines changed

docs/contrib/README.md

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,25 @@ Repository for third party middlewares and service implementations, with depende
1717

1818
## 📑 Middleware Implementations
1919

20-
* [Casbin](./casbin/README.md) <a href="https://github.com/gofiber/contrib/actions?query=workflow%3A%22Test+Casbin%22"> <img src="https://img.shields.io/github/actions/workflow/status/gofiber/contrib/test-casbin.yml?branch=main&label=%F0%9F%A7%AA%20&style=flat&color=75C46B" /> </a>
21-
* [CircuitBreaker](./circuitbreaker/README.md) <a href="https://github.com/gofiber/contrib/actions?query=workflow%3A%22Test+CircuitBreaker%22"> <img src="https://img.shields.io/github/actions/workflow/status/gofiber/contrib/test-circuitbreaker.yml?branch=main&label=%F0%9F%A7%AA%20&style=flat&color=75C46B" /> </a>
22-
* [Fgprof](./fgprof/README.md) <a href="https://github.com/gofiber/contrib/actions?query=workflow%3A%22Test+Fgprof%22"> <img src="https://img.shields.io/github/actions/workflow/status/gofiber/contrib/test-fgprof.yml?branch=main&label=%F0%9F%A7%AA%20&style=flat&color=75C46B" /> </a>
23-
* [Fiberi18n](./fiberi18n/README.md) <a href="https://github.com/gofiber/contrib/actions?query=workflow%3A%22Test+fiberi18n%22"> <img src="https://img.shields.io/github/actions/workflow/status/gofiber/contrib/test-fiberi18n.yml?branch=main&label=%F0%9F%A7%AA%20&style=flat&color=75C46B" /> </a>
24-
* [Fibersentry](./fibersentry/README.md) <a href="https://github.com/gofiber/contrib/actions?query=workflow%3A%22Test+fibersentry%22"> <img src="https://img.shields.io/github/actions/workflow/status/gofiber/contrib/test-fibersentry.yml?branch=main&label=%F0%9F%A7%AA%20&style=flat&color=75C46B" /> </a>
25-
* [Fiberzap](./fiberzap/README.md) <a href="https://github.com/gofiber/contrib/actions?query=workflow%3A%22Test+fiberzap%22"> <img src="https://img.shields.io/github/actions/workflow/status/gofiber/contrib/test-fiberzap.yml?branch=main&label=%F0%9F%A7%AA%20&style=flat&color=75C46B" /> </a>
26-
* [Fiberzerolog](./fiberzerolog/README.md) <a href="https://github.com/gofiber/contrib/actions?query=workflow%3A%22Test+fiberzerolog%22"> <img src="https://img.shields.io/github/actions/workflow/status/gofiber/contrib/test-fiberzerolog.yml?branch=main&label=%F0%9F%A7%AA%20&style=flat&color=75C46B" /> </a>
27-
* [HCaptcha](./hcaptcha/README.md) <a href="https://github.com/gofiber/contrib/actions?query=workflow%3A%22Test+hcaptcha%22"> <img src="https://img.shields.io/github/actions/workflow/status/gofiber/contrib/test-hcaptcha.yml?branch=main&label=%F0%9F%A7%AA%20&style=flat&color=75C46B" /> </a>
28-
* [JWT](./jwt/README.md) <a href="https://github.com/gofiber/contrib/actions?query=workflow%3A%22Test+jwt%22"> <img src="https://img.shields.io/github/actions/workflow/status/gofiber/contrib/test-jwt.yml?branch=main&label=%F0%9F%A7%AA%20&style=flat&color=75C46B" /> </a>
29-
* [Loadshed](./loadshed/README.md) <a href="https://github.com/gofiber/contrib/actions?query=workflow%3A%22Test+loadshed%22"> <img src="https://img.shields.io/github/actions/workflow/status/gofiber/contrib/test-loadshed.yml?branch=main&label=%F0%9F%A7%AA%20&style=flat&color=75C46B" /> </a>
30-
* [NewRelic](./fibernewrelic/README.md) <a href="https://github.com/gofiber/contrib/actions?query=workflow%3A%22Test+fibernewrelic%22"> <img src="https://img.shields.io/github/actions/workflow/status/gofiber/contrib/test-fibernewrelic.yml?branch=main&label=%F0%9F%A7%AA%20&style=flat&color=75C46B" /> </a>
31-
* [Monitor](./monitor/README.md) <a href="https://github.com/gofiber/contrib/actions?query=workflow%3A%22Test+Monitor%22"> <img src="https://img.shields.io/github/actions/workflow/status/gofiber/contrib/test-monitor.yml?branch=main&label=%F0%9F%A7%AA%20&style=flat&color=75C46B" /> </a>
32-
* [Open Policy Agent](./opafiber/README.md) <a href="https://github.com/gofiber/contrib/actions?query=workflow%3A%22Test+opafiber%22"> <img src="https://img.shields.io/github/actions/workflow/status/gofiber/contrib/test-opafiber.yml?branch=main&label=%F0%9F%A7%AA%20&style=flat&color=75C46B" /> </a>
33-
* [Otelfiber (OpenTelemetry)](./otelfiber/README.md) <a href="https://github.com/gofiber/contrib/actions?query=workflow%3A%22Test+otelfiber%22"> <img src="https://img.shields.io/github/actions/workflow/status/gofiber/contrib/test-otelfiber.yml?branch=main&label=%F0%9F%A7%AA%20&style=flat&color=75C46B" /> </a>
34-
* [Paseto](./paseto/README.md) <a href="https://github.com/gofiber/contrib/actions?query=workflow%3A%22Test+paseto%22"> <img src="https://img.shields.io/github/actions/workflow/status/gofiber/contrib/test-paseto.yml?branch=main&label=%F0%9F%A7%AA%20&style=flat&color=75C46B" /> </a>
35-
* [Socket.io](./socketio/README.md) <a href="https://github.com/gofiber/contrib/actions?query=workflow%3A%22Test+socketio%22"> <img src="https://img.shields.io/github/actions/workflow/status/gofiber/contrib/test-socketio.yml?branch=main&label=%F0%9F%A7%AA%20&style=flat&color=75C46B" /> </a>
36-
* [Swagger](./swagger/README.md) <a href="https://github.com/gofiber/contrib/actions?query=workflow%3A%22Test+swagger%22"> <img src="https://img.shields.io/github/actions/workflow/status/gofiber/contrib/test-swagger.yml?branch=main&label=%F0%9F%A7%AA%20&style=flat&color=75C46B" /> </a>
37-
* [Websocket](./websocket/README.md) <a href="https://github.com/gofiber/contrib/actions?query=workflow%3A%22Test+websocket%22"> <img src="https://img.shields.io/github/actions/workflow/status/gofiber/contrib/test-websocket.yml?branch=main&label=%F0%9F%A7%AA%20&style=flat&color=75C46B" /> </a>
20+
* [Casbin](./v3/casbin/README.md) <a href="https://github.com/gofiber/contrib/actions?query=workflow%3A%22Test+Casbin%22"> <img src="https://img.shields.io/github/actions/workflow/status/gofiber/contrib/test-casbin.yml?branch=main&label=%F0%9F%A7%AA%20&style=flat&color=75C46B" alt="Casbin workflow status" /> </a>
21+
* [CircuitBreaker](./v3/circuitbreaker/README.md) <a href="https://github.com/gofiber/contrib/actions?query=workflow%3A%22Test+CircuitBreaker%22"> <img src="https://img.shields.io/github/actions/workflow/status/gofiber/contrib/test-circuitbreaker.yml?branch=main&label=%F0%9F%A7%AA%20&style=flat&color=75C46B" alt="CircuitBreaker workflow status" /> </a>
22+
* [Fgprof](./v3/fgprof/README.md) <a href="https://github.com/gofiber/contrib/actions?query=workflow%3A%22Test+Fgprof%22"> <img src="https://img.shields.io/github/actions/workflow/status/gofiber/contrib/test-fgprof.yml?branch=main&label=%F0%9F%A7%AA%20&style=flat&color=75C46B" alt="Fgprof workflow status" /> </a>
23+
* [I18n](./v3/i18n/README.md) <a href="https://github.com/gofiber/contrib/actions?query=workflow%3A%22Test+i18n%22"> <img src="https://img.shields.io/github/actions/workflow/status/gofiber/contrib/test-i18n.yml?branch=main&label=%F0%9F%A7%AA%20&style=flat&color=75C46B" alt="i18n workflow status" /> </a>
24+
* [Sentry](./v3/sentry/README.md) <a href="https://github.com/gofiber/contrib/actions?query=workflow%3A%22Test+sentry%22"> <img src="https://img.shields.io/github/actions/workflow/status/gofiber/contrib/test-sentry.yml?branch=main&label=%F0%9F%A7%AA%20&style=flat&color=75C46B" alt="Sentry workflow status" /> </a>
25+
* [Zap](./v3/zap/README.md) <a href="https://github.com/gofiber/contrib/actions?query=workflow%3A%22Test+zap%22"> <img src="https://img.shields.io/github/actions/workflow/status/gofiber/contrib/test-zap.yml?branch=main&label=%F0%9F%A7%AA%20&style=flat&color=75C46B" alt="Zap workflow status" /> </a>
26+
* [Zerolog](./v3/zerolog/README.md) <a href="https://github.com/gofiber/contrib/actions?query=workflow%3A%22Test+zerolog%22"> <img src="https://img.shields.io/github/actions/workflow/status/gofiber/contrib/test-zerolog.yml?branch=main&label=%F0%9F%A7%AA%20&style=flat&color=75C46B" alt="Zerolog workflow status" /> </a>
27+
* [HCaptcha](./v3/hcaptcha/README.md) <a href="https://github.com/gofiber/contrib/actions?query=workflow%3A%22Test+hcaptcha%22"> <img src="https://img.shields.io/github/actions/workflow/status/gofiber/contrib/test-hcaptcha.yml?branch=main&label=%F0%9F%A7%AA%20&style=flat&color=75C46B" alt="HCaptcha workflow status" /> </a>
28+
* [JWT](./v3/jwt/README.md) <a href="https://github.com/gofiber/contrib/actions?query=workflow%3A%22Test+jwt%22"> <img src="https://img.shields.io/github/actions/workflow/status/gofiber/contrib/test-jwt.yml?branch=main&label=%F0%9F%A7%AA%20&style=flat&color=75C46B" alt="JWT workflow status" /> </a>
29+
* [Loadshed](./v3/loadshed/README.md) <a href="https://github.com/gofiber/contrib/actions?query=workflow%3A%22Test+loadshed%22"> <img src="https://img.shields.io/github/actions/workflow/status/gofiber/contrib/test-loadshed.yml?branch=main&label=%F0%9F%A7%AA%20&style=flat&color=75C46B" alt="Loadshed workflow status" /> </a>
30+
* [New Relic](./v3/newrelic/README.md) <a href="https://github.com/gofiber/contrib/actions?query=workflow%3A%22Test+newrelic%22"> <img src="https://img.shields.io/github/actions/workflow/status/gofiber/contrib/test-newrelic.yml?branch=main&label=%F0%9F%A7%AA%20&style=flat&color=75C46B" alt="New Relic workflow status" /> </a>
31+
* [Monitor](./v3/monitor/README.md) <a href="https://github.com/gofiber/contrib/actions?query=workflow%3A%22Test+Monitor%22"> <img src="https://img.shields.io/github/actions/workflow/status/gofiber/contrib/test-monitor.yml?branch=main&label=%F0%9F%A7%AA%20&style=flat&color=75C46B" alt="Monitor workflow status" /> </a>
32+
* [Open Policy Agent](./v3/opa/README.md) <a href="https://github.com/gofiber/contrib/actions?query=workflow%3A%22Test+opa%22"> <img src="https://img.shields.io/github/actions/workflow/status/gofiber/contrib/test-opa.yml?branch=main&label=%F0%9F%A7%AA%20&style=flat&color=75C46B" alt="OPA workflow status" /> </a>
33+
* [Otel (OpenTelemetry)](./v3/otel/README.md) <a href="https://github.com/gofiber/contrib/actions?query=workflow%3A%22Test+otel%22"> <img src="https://img.shields.io/github/actions/workflow/status/gofiber/contrib/test-otel.yml?branch=main&label=%F0%9F%A7%AA%20&style=flat&color=75C46B" alt="Otel workflow status" /> </a>
34+
* [Paseto](./v3/paseto/README.md) <a href="https://github.com/gofiber/contrib/actions?query=workflow%3A%22Test+paseto%22"> <img src="https://img.shields.io/github/actions/workflow/status/gofiber/contrib/test-paseto.yml?branch=main&label=%F0%9F%A7%AA%20&style=flat&color=75C46B" alt="Paseto workflow status" /> </a>
35+
* [Socket.io](./v3/socketio/README.md) <a href="https://github.com/gofiber/contrib/actions?query=workflow%3A%22Test+socketio%22"> <img src="https://img.shields.io/github/actions/workflow/status/gofiber/contrib/test-socketio.yml?branch=main&label=%F0%9F%A7%AA%20&style=flat&color=75C46B" alt="Socket.io workflow status" /> </a>
36+
* [Swagger](./v3/swagger/README.md) <a href="https://github.com/gofiber/contrib/actions?query=workflow%3A%22Test+swagger%22"> <img src="https://img.shields.io/github/actions/workflow/status/gofiber/contrib/test-swagger.yml?branch=main&label=%F0%9F%A7%AA%20&style=flat&color=75C46B" alt="Swagger workflow status" /> </a>
37+
* [Websocket](./v3/websocket/README.md) <a href="https://github.com/gofiber/contrib/actions?query=workflow%3A%22Test+websocket%22"> <img src="https://img.shields.io/github/actions/workflow/status/gofiber/contrib/test-websocket.yml?branch=main&label=%F0%9F%A7%AA%20&style=flat&color=75C46B" alt="Websocket workflow status" /> </a>
3838

3939
## 🥡 Service Implementations
4040

41-
* [Testcontainers](./testcontainers/README.md) <a href="https://github.com/gofiber/contrib/actions?query=workflow%3A%22Test+Testcontainers%22"> <img src="https://img.shields.io/github/actions/workflow/status/gofiber/contrib/test-testcontainers.yml?branch=main&label=%F0%9F%A7%AA%20&style=flat&color=75C46B" /> </a>
41+
* [Testcontainers](./v3/testcontainers/README.md) <a href="https://github.com/gofiber/contrib/actions?query=workflow%3A%22Test+Testcontainers%22"> <img src="https://img.shields.io/github/actions/workflow/status/gofiber/contrib/test-testcontainers.yml?branch=main&label=%F0%9F%A7%AA%20&style=flat&color=75C46B" alt="Testcontainers workflow status" /> </a>

docs/contrib/v3/casbin/README.md

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
---
2+
id: casbin
3+
---
4+
5+
# Casbin
6+
7+
![Release](https://img.shields.io/github/v/tag/gofiber/contrib?filter=casbin*)
8+
[![Discord](https://img.shields.io/discord/704680098577514527?style=flat&label=%F0%9F%92%AC%20discord&color=00ACD7)](https://gofiber.io/discord)
9+
![Test](https://github.com/gofiber/contrib/workflows/Test%20casbin/badge.svg)
10+
11+
Casbin middleware for Fiber.
12+
13+
**Compatible with Fiber v3.**
14+
15+
**Note: Requires Go 1.25 and above**
16+
17+
## Install
18+
```sh
19+
go get -u github.com/gofiber/fiber/v3
20+
go get -u github.com/gofiber/contrib/v3/casbin
21+
```
22+
choose an adapter from [here](https://casbin.org/docs/adapters)
23+
```sh
24+
go get -u github.com/casbin/xorm-adapter
25+
```
26+
27+
## Signature
28+
```go
29+
casbin.New(config ...casbin.Config) *casbin.Middleware
30+
```
31+
32+
## Config
33+
34+
| Property | Type | Description | Default |
35+
|:--------------|:--------------------------|:-----------------------------------------|:--------------------------------------------------------------|
36+
| ModelFilePath | `string` | Model file path | `"./model.conf"` |
37+
| PolicyAdapter | `persist.Adapter` | Database adapter for policies | `./policy.csv` |
38+
| Enforcer | `*casbin.Enforcer` | Custom casbin enforcer | `Middleware generated enforcer using ModelFilePath & PolicyAdapter` |
39+
| Lookup | `func(fiber.Ctx) string` | Look up for current subject | `""` |
40+
| Unauthorized | `func(fiber.Ctx) error` | Response body for unauthorized responses | `Unauthorized` |
41+
| Forbidden | `func(fiber.Ctx) error` | Response body for forbidden responses | `Forbidden` |
42+
43+
### Examples
44+
- [Gorm Adapter](https://github.com/svcg/-fiber_casbin_demo)
45+
- [File Adapter](https://github.com/gofiber/contrib/tree/master/v3/casbin/example)
46+
47+
## CustomPermission
48+
49+
```go
50+
package main
51+
52+
import (
53+
"github.com/gofiber/fiber/v3"
54+
"github.com/gofiber/contrib/v3/casbin"
55+
_ "github.com/go-sql-driver/mysql"
56+
"github.com/casbin/xorm-adapter/v2"
57+
)
58+
59+
func main() {
60+
app := fiber.New()
61+
62+
authz := casbin.New(casbin.Config{
63+
ModelFilePath: "path/to/rbac_model.conf",
64+
PolicyAdapter: xormadapter.NewAdapter("mysql", "root:@tcp(127.0.0.1:3306)/"),
65+
Lookup: func(c fiber.Ctx) string {
66+
// fetch authenticated user subject
67+
},
68+
})
69+
70+
app.Post("/blog",
71+
authz.RequiresPermissions([]string{"blog:create"}, casbin.WithValidationRule(casbin.MatchAllRule)),
72+
func(c fiber.Ctx) error {
73+
// your handler
74+
},
75+
)
76+
77+
app.Delete("/blog/:id",
78+
authz.RequiresPermissions([]string{"blog:create", "blog:delete"}, casbin.WithValidationRule(casbin.AtLeastOneRule)),
79+
func(c fiber.Ctx) error {
80+
// your handler
81+
},
82+
)
83+
84+
app.Listen(":8080")
85+
}
86+
```
87+
88+
## RoutePermission
89+
90+
```go
91+
package main
92+
93+
import (
94+
"github.com/gofiber/fiber/v3"
95+
"github.com/gofiber/contrib/v3/casbin"
96+
_ "github.com/go-sql-driver/mysql"
97+
"github.com/casbin/xorm-adapter/v2"
98+
)
99+
100+
func main() {
101+
app := fiber.New()
102+
103+
authz := casbin.New(casbin.Config{
104+
ModelFilePath: "path/to/rbac_model.conf",
105+
PolicyAdapter: xormadapter.NewAdapter("mysql", "root:@tcp(127.0.0.1:3306)/"),
106+
Lookup: func(c fiber.Ctx) string {
107+
// fetch authenticated user subject
108+
},
109+
})
110+
111+
// check permission with Method and Path
112+
app.Post("/blog",
113+
authz.RoutePermission(),
114+
func(c fiber.Ctx) error {
115+
// your handler
116+
},
117+
)
118+
119+
app.Listen(":8080")
120+
}
121+
```
122+
123+
## RoleAuthorization
124+
125+
```go
126+
package main
127+
128+
import (
129+
"github.com/gofiber/fiber/v3"
130+
"github.com/gofiber/contrib/v3/casbin"
131+
_ "github.com/go-sql-driver/mysql"
132+
"github.com/casbin/xorm-adapter/v2"
133+
)
134+
135+
func main() {
136+
app := fiber.New()
137+
138+
authz := casbin.New(casbin.Config{
139+
ModelFilePath: "path/to/rbac_model.conf",
140+
PolicyAdapter: xormadapter.NewAdapter("mysql", "root:@tcp(127.0.0.1:3306)/"),
141+
Lookup: func(c fiber.Ctx) string {
142+
// fetch authenticated user subject
143+
},
144+
})
145+
146+
app.Put("/blog/:id",
147+
authz.RequiresRoles([]string{"admin"}),
148+
func(c fiber.Ctx) error {
149+
// your handler
150+
},
151+
)
152+
153+
app.Listen(":8080")
154+
}
155+
```

0 commit comments

Comments
 (0)