-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathauthz.go
More file actions
119 lines (102 loc) · 3.14 KB
/
authz.go
File metadata and controls
119 lines (102 loc) · 3.14 KB
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
114
115
116
117
118
119
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/v3"
)
// CredentialValidator validates a username and password from HTTP Basic Auth.
// Assign a custom implementation before using the plugin.
// If nil, any non-empty username is accepted without password verification.
var CredentialValidator func(username, password string) bool
func init() {
caddy.RegisterModule(Authorizer{})
httpcaddyfile.RegisterHandlerDirective("authz", parseCaddyfile)
}
type Authorizer struct {
AuthConfig struct {
ModelPath string `json:"model_path"`
PolicyPath string `json:"policy_path"`
} `json:"auth_config"`
Enforcer *casbin.Enforcer `json:"-"`
}
// 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 {
username, password, ok := r.BasicAuth()
if !ok {
w.Header().Set("WWW-Authenticate", `Basic realm="restricted"`)
w.WriteHeader(http.StatusUnauthorized)
return nil
}
if CredentialValidator != nil && !CredentialValidator(username, password) {
w.Header().Set("WWW-Authenticate", `Basic realm="restricted"`)
w.WriteHeader(http.StatusUnauthorized)
return nil
}
allowed, err := a.CheckPermission(username, r)
if err != nil {
return err
}
if !allowed {
w.WriteHeader(http.StatusForbidden)
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
}
// CheckPermission checks if the given user is allowed to access the resource.
func (a *Authorizer) CheckPermission(username string, r *http.Request) (bool, error) {
return a.Enforcer.Enforce(username, r.URL.Path, r.Method)
}
// Interface guards
var (
_ caddy.Provisioner = (*Authorizer)(nil)
_ caddy.Validator = (*Authorizer)(nil)
_ caddyhttp.MiddlewareHandler = (*Authorizer)(nil)
_ caddyfile.Unmarshaler = (*Authorizer)(nil)
)