-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblacklist_token_checker.go
79 lines (68 loc) · 2.02 KB
/
blacklist_token_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
package security
import (
"strconv"
"strings"
"time"
)
const joinChar = "-"
type DefaultBlacklistTokenChecker struct {
CacheService CacheService
TokenPrefix string
TokenExpires int64
}
func NewTokenBlacklistChecker(cacheService CacheService, keyPrefix string, tokenExpires int64) *DefaultBlacklistTokenChecker {
return &DefaultBlacklistTokenChecker{CacheService: cacheService, TokenPrefix: keyPrefix, TokenExpires: tokenExpires}
}
func (s *DefaultBlacklistTokenChecker) generateKey(token string) string {
return s.TokenPrefix + token
}
func (s *DefaultBlacklistTokenChecker) generateKeyForId(id string) string {
return s.TokenPrefix + id
}
func (s *DefaultBlacklistTokenChecker) Revoke(token string, reason string, expiredDate time.Time) error {
key := s.generateKey(token)
var value string
if len(reason) > 0 {
value = reason
} else {
value = ""
}
today := time.Now()
expiresInSecond := expiredDate.Sub(today)
if expiresInSecond <= 0 {
return nil // Token already expires, don't need add to cache
} else {
return s.CacheService.Put(key, value, expiresInSecond*time.Second)
}
}
func (s *DefaultBlacklistTokenChecker) RevokeAllTokens(id string, reason string) error {
key := s.generateKeyForId(id)
today := time.Now()
value := reason + joinChar + strconv.Itoa(int(today.Unix()))
return s.CacheService.Put(key, value, time.Duration(s.TokenExpires)*time.Second)
}
func (s *DefaultBlacklistTokenChecker) Check(id string, token string, createAt time.Time) string {
idKey := s.generateKeyForId(id)
tokenKey := s.generateKey(token)
keys := []string{idKey, tokenKey}
value, _, err := s.CacheService.GetManyStrings(keys)
if err != nil {
return ""
}
if len(value[idKey]) > 0 {
index := strings.Index(value[idKey], joinChar)
reason := value[idKey][0:index]
strDate := value[idKey][index+1:]
i, err := strconv.ParseInt(strDate, 10, 64)
if err == nil {
tmDate := time.Unix(i, 0)
if tmDate.Sub(createAt) > 0 {
return reason
}
}
}
if len(value[tokenKey]) > 0 {
return value[tokenKey]
}
return ""
}