-
Notifications
You must be signed in to change notification settings - Fork 0
/
array_authorizer.go
46 lines (42 loc) · 1.08 KB
/
array_authorizer.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
package security
import "net/http"
type ArrayAuthorizer struct {
Authorization string
Key string
sortedUsers bool
}
func NewArrayAuthorizer(sortedUsers bool, options ...string) *ArrayAuthorizer {
authorization := ""
key := "userId"
if len(options) >= 2 {
authorization = options[1]
}
if len(options) >= 1 && len(options[0]) > 0 {
key = options[0]
}
return &ArrayAuthorizer{sortedUsers: sortedUsers, Authorization: authorization, Key: key}
}
func (h *ArrayAuthorizer) Authorize(next http.Handler, array []string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
v := FromContext(r, h.Authorization, h.Key)
if len(v) == 0 {
http.Error(w, "cannot get '" + h.Key + "' from http request", http.StatusForbidden)
return
}
if len(array) == 0 {
http.Error(w, "no permission", http.StatusForbidden)
return
}
if h.sortedUsers {
if IncludeOfSort(array, v) {
next.ServeHTTP(w, r)
return
}
}
if Include(array, v) {
next.ServeHTTP(w, r)
return
}
http.Error(w, "no permission", http.StatusForbidden)
})
}