-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
authz.go
113 lines (97 loc) · 2.74 KB
/
authz.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package authz
import (
"fmt"
"net/http"
"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
"github.com/casbin/casbin/v2"
)
func init() {
caddy.RegisterModule(Authorizer{})
httpcaddyfile.RegisterHandlerDirective("authz", parseCaddyfile)
}
type Authorizer struct {
AuthConfig struct {
ModelPath string
PolicyPath string
}
Enforcer *casbin.Enforcer
}
// CaddyModule returns the Caddy module information.
func (Authorizer) CaddyModule() caddy.ModuleInfo {
return caddy.ModuleInfo{
ID: "http.handlers.authz",
New: func() caddy.Module { return new(Authorizer) },
}
}
// Provision implements caddy.Provisioner.
func (a *Authorizer) Provision(ctx caddy.Context) error {
e, err := casbin.NewEnforcer(a.AuthConfig.ModelPath, a.AuthConfig.PolicyPath)
if err != nil {
return err
}
a.Enforcer = e
return nil
}
// Validate implements caddy.Validator.
func (a *Authorizer) Validate() error {
if a.Enforcer == nil {
return fmt.Errorf("no Enforcer")
}
return nil
}
// ServeHTTP implements caddyhttp.MiddlewareHandler.
func (a Authorizer) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error {
allowed, err := a.CheckPermission(r)
if err != nil {
return err
}
if !allowed {
w.WriteHeader(403)
return nil
}
return next.ServeHTTP(w, r)
}
// UnmarshalCaddyfile implements caddyfile.Unmarshaler.
func (a *Authorizer) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
for d.Next() {
if !d.NextArg() {
return d.ArgErr()
}
a.AuthConfig.ModelPath = d.Val()
if !d.NextArg() {
return d.ArgErr()
}
a.AuthConfig.PolicyPath = d.Val()
}
return nil
}
// parseCaddyfile unmarshals tokens from h into a new Authorizer.
func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) {
var m Authorizer
err := m.UnmarshalCaddyfile(h.Dispenser)
return m, err
}
// GetUserName gets the user name from the request.
// Currently, only HTTP basic authentication is supported
func (a *Authorizer) GetUserName(r *http.Request) string {
username := r.Header.Get("Authorization")
return username
}
// CheckPermission checks the user/method/path combination from the request.
// Returns true (permission granted) or false (permission forbidden)
func (a *Authorizer) CheckPermission(r *http.Request) (bool, error) {
user := a.GetUserName(r)
method := r.Method
path := r.URL.Path
return a.Enforcer.Enforce(user, path, method)
}
// Interface guards
var (
_ caddy.Provisioner = (*Authorizer)(nil)
_ caddy.Validator = (*Authorizer)(nil)
_ caddyhttp.MiddlewareHandler = (*Authorizer)(nil)
_ caddyfile.Unmarshaler = (*Authorizer)(nil)
)