-
Notifications
You must be signed in to change notification settings - Fork 0
/
role_authorizer.go
66 lines (60 loc) · 1.43 KB
/
role_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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package security
import (
"net/http"
"sort"
)
type RoleAuthorizer struct {
Authorization string
Key string
sortedRoles bool
}
func NewRoleAuthorizer(sortedRoles bool, options ...string) *RoleAuthorizer {
authorization := ""
key := "roleId"
if len(options) >= 2 {
authorization = options[1]
}
if len(options) >= 1 {
key = options[0]
}
return &RoleAuthorizer{sortedRoles: sortedRoles, Authorization: authorization, Key: key}
}
func (h *RoleAuthorizer) Authorize(next http.Handler, roles []string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
userRoles := ValuesFromContext(r, h.Authorization, h.Key)
if userRoles == nil || len(*userRoles) == 0 {
http.Error(w, "no permission: Require roles for this user", http.StatusForbidden)
return
}
if h.sortedRoles {
if HasSortedRole(roles, *userRoles) {
next.ServeHTTP(w, r)
return
}
}
if HasRole(roles, *userRoles) {
next.ServeHTTP(w, r)
return
}
http.Error(w, "no permission", http.StatusForbidden)
})
}
func HasRole(roles []string, userRoles []string) bool {
for _, role := range roles {
for _, userRole := range userRoles {
if role == userRole {
return true
}
}
}
return false
}
func HasSortedRole(roles []string, userRoles []string) bool {
for _, role := range roles {
i := sort.SearchStrings(userRoles, role)
if i >= 0 && userRoles[i] == role {
return true
}
}
return false
}