-
Notifications
You must be signed in to change notification settings - Fork 1
/
middleware.go
157 lines (129 loc) · 3.68 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
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
package warrant
import (
"log"
"net/http"
)
type GetObjectIdFunc func(r *http.Request) string
type GetUserIdFunc func(r *http.Request) string
type NewEnsureIsAuthorizedFunc func(handler http.Handler, options EnsureIsAuthorizedOptions) *EnsureIsAuthorized
type NewEnsureHasPermissionFunc func(handler http.Handler, options EnsureHasPermissionOptions) *EnsureHasPermission
type MiddlewareConfig struct {
ApiKey string
GetObjectId GetObjectIdFunc
GetUserId GetUserIdFunc
OnAccessDenied http.HandlerFunc
}
type Middleware struct {
config MiddlewareConfig
client WarrantClient
}
type EnsureIsAuthorizedOptions struct {
ObjectType string
ObjectId string
Relation string
UserId string
}
type EnsureIsAuthorized struct {
handler http.Handler
mw Middleware
options EnsureIsAuthorizedOptions
}
type EnsureHasPermissionOptions struct {
PermissionId string
UserId string
}
type EnsureHasPermission struct {
handler http.Handler
mw Middleware
options EnsureHasPermissionOptions
}
func defaultOnAccessDenied(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusUnauthorized)
}
func (eia *EnsureIsAuthorized) ServeHTTP(w http.ResponseWriter, r *http.Request) {
objectId := eia.options.ObjectId
userId := eia.options.UserId
if objectId == "" {
objectId = eia.mw.config.GetObjectId(r)
}
if userId == "" {
userId = eia.mw.config.GetUserId(r)
}
isAuthorized, err := eia.mw.client.Check(&WarrantCheckParams{
WarrantCheck: WarrantCheck{
Object: Object{
ObjectType: eia.options.ObjectType,
ObjectId: objectId,
},
Relation: eia.options.Relation,
Subject: Subject{
ObjectType: ObjectTypeUser,
ObjectId: userId,
},
},
})
if err != nil {
log.Println(err)
eia.mw.config.OnAccessDenied(w, r)
return
}
if !isAuthorized {
eia.mw.config.OnAccessDenied(w, r)
return
}
eia.handler.ServeHTTP(w, r)
}
func (mw Middleware) NewEnsureIsAuthorized(handler http.Handler, options EnsureIsAuthorizedOptions) *EnsureIsAuthorized {
if options.ObjectId == "" && mw.config.GetObjectId == nil {
panic("You must either provide GetObjectId to the Warrant middleware when calling NewMiddleware or provide an ObjectId when calling NewEnsureIsAuthorized.")
}
if options.UserId == "" && mw.config.GetUserId == nil {
panic("You must either provide GetUserId to the Warrant middleware when calling NewMiddleware or provide a UserId when calling NewEnsureIsAuthorized.")
}
return &EnsureIsAuthorized{
handler: handler,
mw: mw,
options: options,
}
}
func (ehp *EnsureHasPermission) ServeHTTP(w http.ResponseWriter, r *http.Request) {
userId := ehp.options.UserId
if userId == "" {
userId = ehp.mw.config.GetUserId(r)
}
isAuthorized, err := ehp.mw.client.CheckUserHasPermission(&PermissionCheckParams{
PermissionId: ehp.options.PermissionId,
UserId: userId,
})
if err != nil {
log.Println(err)
ehp.mw.config.OnAccessDenied(w, r)
return
}
if !isAuthorized {
ehp.mw.config.OnAccessDenied(w, r)
return
}
ehp.handler.ServeHTTP(w, r)
}
func (mw Middleware) NewEnsureHasPermission(handler http.Handler, options EnsureHasPermissionOptions) *EnsureHasPermission {
return &EnsureHasPermission{
handler: handler,
mw: mw,
options: options,
}
}
func NewMiddleware(middlewareConfig MiddlewareConfig) *Middleware {
if middlewareConfig.OnAccessDenied == nil {
middlewareConfig.OnAccessDenied = defaultOnAccessDenied
}
return &Middleware{
config: middlewareConfig,
client: NewClient(ClientConfig{
ApiKey: middlewareConfig.ApiKey,
ApiEndpoint: ApiEndpoint,
AuthorizeEndpoint: AuthorizeEndpoint,
SelfServiceDashEndpoint: SelfServiceDashEndpoint,
}),
}
}