-
Notifications
You must be signed in to change notification settings - Fork 0
/
authz.go
287 lines (224 loc) · 6.96 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
// 🚀 Fiber is an Express inspired web framework written in Go with 💖
// 📌 API Documentation: https://fiber.wiki
// 📝 Github Repository: https://github.com/gofiber/fiber
package authz
import (
"context"
"github.com/gofiber/fiber/v2"
)
// AuthzPrincipal is the subject.
type AuthzPrincipal string
// String is the stringer implementation.
func (a AuthzPrincipal) String() string {
return string(a)
}
// AuthzObject is the object.
type AuthzObject string
// String is the stringer implementation.
func (a AuthzObject) String() string {
return string(a)
}
// AuthzAction is the action.
type AuthzAction string
// String is the stringer implementation.
func (a AuthzAction) String() string {
return string(a)
}
const (
AuthzNoPrincipial AuthzPrincipal = ""
AuthzNoObject AuthzObject = ""
AuthzNoAction AuthzAction = ""
)
// AuthzParams is the struct that holds the principal, object and action from the context.
// There needs to be a :principal, :object and :action in the context.
type AuthzParams struct {
// Principal is the subject.
Principal AuthzPrincipal `json:"principal" params:"principal" query:"principal" form:"principal"`
// Object is the object.
Object AuthzObject `json:"object" params:"object" query:"object" form:"object"`
// Action is the action.
Action AuthzAction `json:"action" params:"action" query:"action" form:"action"`
}
// AuthzChecker is the interface that wraps the Allowed method.
type AuthzChecker interface {
// Allowed ...
Allowed(context.Context, AuthzPrincipal, AuthzObject, AuthzAction) (bool, error)
}
// The contextKey type is unexported to prevent collisions with context keys defined in
// other packages.
type contextKey int
// The keys for the values in context
const (
authzPrincipial contextKey = iota
authzObject
authzAction
authzContext
)
// Unimplemented is the default implementation.
type Unimplemented struct{}
// Allowed is the default implementation.
func (u *Unimplemented) Allowed(_ context.Context, _ AuthzPrincipal, _ AuthzObject, _ AuthzAction) (bool, error) {
return false, nil
}
var _ AuthzChecker = (*Fake)(nil)
// Fake is a fake authz checker.
type Fake struct {
allowd bool
}
// NewFake returns a new Fake authz checker.
func NewFake(allowed bool) *Fake {
return &Fake{allowd: allowed}
}
// Allowed returns true if the principal is allowed to perform the action on the object.
func (f *Fake) Allowed(_ context.Context, _ AuthzPrincipal, _ AuthzObject, _ AuthzAction) (bool, error) {
return f.allowd, nil
}
// Config ...
type Config struct {
// Next defines a function to skip this middleware when returned true.
Next func(c *fiber.Ctx) bool
// Checker is implementing the AuthzChecker interface.
Checker AuthzChecker
// ObjectResolver is the object resolver.
ObjectResolver AuthzObjectResolver
// ActionResolver is the action resolver.
ActionResolver AuthzActionResolver
// PrincipalResolver is the principal resolver.
PrincipalResolver AuthzPrincipalResolver
// ErrorHandler is executed when an error is returned from fiber.Handler.
//
// Optional. Default: DefaultErrorHandler
ErrorHandler fiber.ErrorHandler
}
// ConfigDefault is the default config.
var ConfigDefault = Config{
ErrorHandler: defaultErrorHandler,
ObjectResolver: NewNoopObjectResolver(),
PrincipalResolver: NewNoopPrincipalResolver(),
ActionResolver: NewNoopActionResolver(),
Checker: NewNoop(),
}
// default ErrorHandler that process return error from fiber.Handler
func defaultErrorHandler(_ *fiber.Ctx, _ error) error {
return fiber.ErrBadRequest
}
// AuthzObjectResolver is the interface that wraps the Resolve method.
type AuthzObjectResolver interface {
// Resolve ...
Resolve(c *fiber.Ctx) (AuthzObject, error)
}
// AuthzPrincipalResolver is the interface that wraps the Resolve method.
type AuthzPrincipalResolver interface {
// Resolve ...
Resolve(c *fiber.Ctx) (AuthzPrincipal, error)
}
// AuthzActionResolver is the interface that wraps the Resolve method.
type AuthzActionResolver interface {
// Resolve ...
Resolve(c *fiber.Ctx) (AuthzAction, error)
}
// Authenticate is a middleware that sets the principal and user in the context.
func Authenticate(handler fiber.Handler, config ...Config) fiber.Handler {
cfg := configDefault(config...)
return func(c *fiber.Ctx) error {
if cfg.Next != nil && cfg.Next(c) {
return c.Next()
}
object, err := cfg.ObjectResolver.Resolve(c)
if err != nil {
return err
}
principal, err := cfg.PrincipalResolver.Resolve(c)
if err != nil {
return err
}
action, err := cfg.ActionResolver.Resolve(c)
if err != nil {
return err
}
allowed, err := cfg.Checker.Allowed(c.Context(), principal, object, action)
if err != nil {
return cfg.ErrorHandler(c, err)
}
if !allowed {
return c.SendStatus(403)
}
return handler(c)
}
}
// NewCheckerHandler returns a new fiber.Handler that checks if the principal can perform the action on the object.
func NewCheckerHandler(config ...Config) fiber.Handler {
cfg := configDefault(config...)
return func(c *fiber.Ctx) error {
if cfg.Next != nil && cfg.Next(c) {
return c.Next()
}
payload := struct {
Principal AuthzPrincipal `json:"principal"`
Object AuthzObject `json:"object"`
Permission AuthzAction `json:"action"`
}{}
if err := c.BodyParser(&payload); err != nil {
return cfg.ErrorHandler(c, err)
}
allowed, err := cfg.Checker.Allowed(c.Context(), payload.Principal, payload.Object, payload.Permission)
if err != nil {
return cfg.ErrorHandler(c, err)
}
if allowed {
return c.SendStatus(200)
}
return c.SendStatus(403)
}
}
// Helper function to set default values
func configDefault(config ...Config) Config {
if len(config) < 1 {
return ConfigDefault
}
// Override default config
cfg := config[0]
if cfg.Checker == nil {
cfg.Checker = ConfigDefault.Checker
}
if cfg.ErrorHandler == nil {
cfg.ErrorHandler = ConfigDefault.ErrorHandler
}
if cfg.ObjectResolver == nil {
cfg.ObjectResolver = ConfigDefault.ObjectResolver
}
if cfg.PrincipalResolver == nil {
cfg.PrincipalResolver = ConfigDefault.PrincipalResolver
}
if cfg.ActionResolver == nil {
cfg.ActionResolver = ConfigDefault.ActionResolver
}
return cfg
}
type noopObjectResolver struct{}
// Resolve ...
func (n *noopObjectResolver) Resolve(c *fiber.Ctx) (AuthzObject, error) {
return AuthzNoObject, nil
}
// NewNoopObjectResolver ...
func NewNoopObjectResolver() AuthzObjectResolver {
return &noopObjectResolver{}
}
type noopPrincipalResolver struct{}
// Resolve ...
func (n *noopPrincipalResolver) Resolve(c *fiber.Ctx) (AuthzPrincipal, error) {
return AuthzNoPrincipial, nil
}
// NewNoopPrincipalResolver ...
func NewNoopPrincipalResolver() AuthzPrincipalResolver {
return &noopPrincipalResolver{}
}
type noopActionResolver struct{}
// Resolve ...
func (n *noopActionResolver) Resolve(c *fiber.Ctx) (AuthzAction, error) {
return AuthzNoAction, nil
}
// NewNoopActionResolver ...
func NewNoopActionResolver() AuthzActionResolver {
return &noopActionResolver{}
}