-
Notifications
You must be signed in to change notification settings - Fork 1
/
middleware.go
95 lines (85 loc) · 2.21 KB
/
middleware.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package fibercasbinrest
import (
"net/http"
"strings"
"github.com/casbin/casbin/v2"
"github.com/gofiber/fiber/v2"
)
type (
// Skipper middleware function
Skipper func(*fiber.Ctx) bool
// Config middleware model
Config struct {
Skipper Skipper
Enforcer *casbin.Enforcer
Adapter Adapter
}
)
var (
// DefaultConfig initial default config
DefaultConfig = Config{
Skipper: DefaultSkipper,
}
)
// DefaultSkipper create default skipper
func DefaultSkipper(*fiber.Ctx) bool {
return false
}
// New create middleware
func New(ce *casbin.Enforcer, adt Adapter) fiber.Handler {
c := DefaultConfig
c.Enforcer = ce
c.Adapter = adt
return middlewareWithConfig(c)
}
// NewDefault create middleware
func NewDefault(ce *casbin.Enforcer, secret string) fiber.Handler {
c := DefaultConfig
c.Enforcer = ce
c.Adapter = NewRoleAdapter(secret)
return middlewareWithConfig(c)
}
func middlewareWithConfig(config Config) fiber.Handler {
if config.Skipper == nil {
config.Skipper = DefaultConfig.Skipper
}
return func(c *fiber.Ctx) error {
pass, err := config.CheckPermissions(c)
if config.Skipper(c) || (pass && err == nil) {
return c.Next()
}
if err != nil && strings.ToLower(err.Error()) == "token is expired" {
return c.Status(http.StatusUnauthorized).
JSON(fiber.Map{
"code": http.StatusUnauthorized,
"message": err.Error(),
})
}
return c.Status(http.StatusForbidden).
JSON(fiber.Map{
"code": http.StatusForbidden,
"message": http.StatusText(http.StatusForbidden),
})
}
}
// GetRole gets the roles name from the request.
func (a *Config) GetRole(c *fiber.Ctx) ([]string, error) {
token := c.Get(fiber.HeaderAuthorization)
authorization := strings.Split(token, "Bearer")
if len(authorization) == 2 {
return a.Adapter.GetRoleByToken(strings.TrimSpace(authorization[1]))
}
return []string{RoleAnonymous}, nil
}
// CheckPermissions checks the role/path/method combination from the request.
func (a *Config) CheckPermissions(c *fiber.Ctx) (bool, error) {
roles, err := a.GetRole(c)
allowed := false
for _, role := range roles {
result, e := a.Enforcer.Enforce(strings.ToLower(role), c.Path(), c.Method())
if result && e == nil {
allowed = true
}
}
return allowed, err
}