forked from JonathanLogan/caddy-authz
/
authz.go
186 lines (164 loc) · 4.65 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package authz
import (
"fmt"
"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"
"net/http"
"time"
"github.com/casbin/casbin"
"github.com/dafanasiev/authfile"
)
func init() {
caddy.RegisterModule(Authorizer{})
httpcaddyfile.RegisterHandlerDirective("authz", parseCaddyfile)
}
type Authorizer struct {
AuthConfig struct {
ModelPath string
PolicyPath string
Realm string
PasswordFile string
}
Enforcer *casbin.Enforcer
PasswordCheck authfile.IAuthenticationService
}
// 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 {
filebackend, err := authfile.NewROFileBackend(a.AuthConfig.PasswordFile, 0600, time.Second*5)
if err != nil {
return err
}
authProvider := authfile.NewInMemoryService(filebackend, time.Second)
authProvider.Update()
e, err := casbin.NewEnforcerSafe(a.AuthConfig.ModelPath, a.AuthConfig.PolicyPath)
if err != nil {
return err
}
a.PasswordCheck = authProvider
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 {
switch a.CheckPermission(r) {
case AccessDenied:
w.WriteHeader(403)
return nil
case AccessAllowed:
return next.ServeHTTP(w, r)
default:
w.Header().Set("WWW-Authenticate", "Basic realm=\""+a.AuthConfig.Realm+"\"")
w.WriteHeader(401)
return nil
}
}
// 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()
if !d.NextArg() {
return d.ArgErr()
}
a.AuthConfig.Realm = d.Val()
if !d.NextArg() {
return d.ArgErr()
}
a.AuthConfig.PasswordFile = 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.BasicAuth()
return username
}
// checkEnforce verifies if the user has access to the resource. If no
// username is given, the check will be against "nobody" only.
func (a *Authorizer) checkEnforce(user, path, method string) (int, bool) {
if user != "" {
if a.Enforcer.Enforce(user, path, method) {
return IdentifiedAccess, true
}
}
if a.Enforcer.Enforce("nobody", path, method) {
if user != "" {
return IdentifiedAccess, true
}
return AnonymousAccess, true
}
return 0, false
}
const (
// MustAuthenticate is returned if authentication is required.
MustAuthenticate = 0
// AccessAllowed is returned if the user has access to the resource.
AccessAllowed = 1
// AccessDenied is returned if the user has no access to the resource.
AccessDenied = 2
// AnonymousAccess is returned if the access is authorized for anonymous access.
AnonymousAccess = 1
// IdentifiedAccess is returned if the access is authorized for an identified user.
IdentifiedAccess = 2
)
// 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) int {
var goodAuthentication bool
user, password, authenticated := r.BasicAuth()
if authenticated {
if err := a.PasswordCheck.Authenticate(user, password); err != nil {
goodAuthentication = false
} else {
goodAuthentication = true
}
}
method := r.Method
path := r.URL.Path
authorizeLevel, authorized := a.checkEnforce(user, path, method)
if authorized {
switch authorizeLevel {
case AnonymousAccess:
return AccessAllowed
case IdentifiedAccess:
if !authenticated || !goodAuthentication {
return MustAuthenticate
}
if authenticated && goodAuthentication {
return AccessAllowed
}
}
} else if !authenticated {
return MustAuthenticate
}
return AccessDenied
}