-
Notifications
You must be signed in to change notification settings - Fork 0
/
authorization_checker.go
122 lines (117 loc) · 4.38 KB
/
authorization_checker.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
package security
import (
"context"
"net/http"
"time"
)
const (
Authorization = "authorization"
Uid = "uid"
UserId = "userId"
UserName = "userName"
Username = "username"
UserType = "userType"
Roles = "roles"
Privileges = "privileges"
Permission = "permission"
Permissions = "permissions"
Ip = "ip"
)
type AuthorizationChecker struct {
GetAndVerifyToken func(authorization string, secret string) (bool, string, map[string]interface{}, int64, int64, error)
Secret string
Ip string
CheckBlacklist func(id string, token string, createAt time.Time) string
Authorization string
Key string
CheckWhitelist func(id string, token string) bool
}
func NewAuthorizationChecker(verifyToken func(string, string) (bool, string, map[string]interface{}, int64, int64, error), secret string, key string, options ...string) *AuthorizationChecker {
return NewAuthorizationCheckerWithIp(verifyToken, secret, "", nil, nil, key, options...)
}
func NewAuthorizationCheckerWithBlacklist(verifyToken func(string, string) (bool, string, map[string]interface{}, int64, int64, error), secret string, checkToken func(string, string, time.Time) string, key string, options ...string) *AuthorizationChecker {
return NewAuthorizationCheckerWithIp(verifyToken, secret, "", checkToken, nil, key, options...)
}
func NewAuthorizationCheckerWithWhitelist(verifyToken func(string, string) (bool, string, map[string]interface{}, int64, int64, error), secret string, checkToken func(string, string, time.Time) string, checkWhitelist func(string, string) bool, key string, options ...string) *AuthorizationChecker {
return NewAuthorizationCheckerWithIp(verifyToken, secret, "", checkToken, checkWhitelist, key, options...)
}
func NewAuthorizationCheckerWithIp(verifyToken func(string, string) (bool, string, map[string]interface{}, int64, int64, error), secret string, ip string, checkToken func(string, string, time.Time) string, checkWhitelist func(string, string) bool, key string, options ...string) *AuthorizationChecker {
var authorization string
if len(options) >= 1 {
authorization = options[0]
}
return &AuthorizationChecker{Authorization: authorization, Key: key, CheckBlacklist: checkToken, GetAndVerifyToken: verifyToken, Secret: secret, Ip: ip, CheckWhitelist: checkWhitelist}
}
func (h *AuthorizationChecker) Check(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
au := r.Header["Authorization"]
if len(au) == 0 {
http.Error(w, "'Authorization' is required in http request header.", http.StatusUnauthorized)
return
}
authorization := au[0]
isToken, token, data, issuedAt, _, err := h.GetAndVerifyToken(authorization, h.Secret)
if !isToken || err != nil {
http.Error(w, "invalid Authorization token", http.StatusUnauthorized)
return
}
if data == nil {
data = make(map[string]interface{})
}
iat := time.Unix(issuedAt, 0)
data["token"] = token
data["issuedAt"] = iat
var ctx context.Context
ctx = r.Context()
if len(h.Ip) > 0 {
ip := getRemoteIp(r)
ctx = context.WithValue(ctx, h.Ip, ip)
}
if h.CheckBlacklist != nil {
user := ValueFromMap(h.Key, data)
reason := h.CheckBlacklist(user, token, iat)
if len(reason) > 0 {
http.Error(w, "token is not valid anymore", http.StatusUnauthorized)
} else {
if h.CheckWhitelist != nil {
valid := h.CheckWhitelist(user, token)
if !valid {
http.Error(w, "token is not valid anymore", http.StatusUnauthorized)
return
}
}
if len(h.Authorization) > 0 {
ctx := context.WithValue(ctx, h.Authorization, data)
next.ServeHTTP(w, r.WithContext(ctx))
} else {
for k, e := range data {
if len(k) > 0 {
ctx = context.WithValue(ctx, k, e)
}
}
next.ServeHTTP(w, r.WithContext(ctx))
}
}
} else {
if h.CheckWhitelist != nil {
user := ValueFromMap(h.Key, data)
valid := h.CheckWhitelist(user, token)
if !valid {
http.Error(w, "token is not valid anymore", http.StatusUnauthorized)
return
}
}
if len(h.Authorization) > 0 {
ctx := context.WithValue(ctx, h.Authorization, data)
next.ServeHTTP(w, r.WithContext(ctx))
} else {
for k, e := range data {
if len(k) > 0 {
ctx = context.WithValue(ctx, k, e)
}
}
next.ServeHTTP(w, r.WithContext(ctx))
}
}
})
}