-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsub_authorizer.go
55 lines (51 loc) · 1.42 KB
/
sub_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
package security
import (
"context"
"net/http"
)
type SubAuthorizer struct {
Privilege func(ctx context.Context, userId string, privilegeId string, sub string) int32
Authorization string
Key string
Exact bool
}
func NewSubAuthorizer(loadPrivilege func(context.Context, string, string, string) int32, exact bool, options ...string) *SubAuthorizer {
authorization := ""
key := "userId"
if len(options) >= 2 {
authorization = options[1]
}
if len(options) >= 1 {
key = options[0]
}
return &SubAuthorizer{Privilege: loadPrivilege, Exact: exact, Authorization: authorization, Key: key}
}
func (h *SubAuthorizer) Authorize(next http.Handler, privilegeId string, sub string, action int32) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
userId := FromContext(r, h.Authorization, h.Key)
if len(userId) == 0 {
http.Error(w, "invalid User Id in http request", http.StatusForbidden)
return
}
p := h.Privilege(r.Context(), userId, privilegeId, sub)
if p == ActionNone {
http.Error(w, "no permission for this user", http.StatusForbidden)
return
}
if action == ActionNone || action == ActionAll {
next.ServeHTTP(w, r)
return
}
sum := action & p
if h.Exact {
if sum == action {
next.ServeHTTP(w, r)
return
}
} else if sum >= action {
next.ServeHTTP(w, r)
return
}
http.Error(w, "no permission", http.StatusForbidden)
})
}