login_access.go
1.9 KB
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
package cache
import (
"time"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/domain"
)
//登录凭证缓存
type LoginTokenCache struct {
}
func (ca LoginTokenCache) keyAccessToken(account string, platform string) string {
str := KEY_PREFIX + "accesstoken:" + account + ":" + platform
return str
}
func (ca LoginTokenCache) keyRefreshToken(account string, platform string) string {
str := KEY_PREFIX + "refreshtoken" + account + ":" + platform
return str
}
func (ca LoginTokenCache) SaveAccessToken(access *domain.LoginAccess) error {
nowTime := time.Now().Unix()
exp := access.AccessExpired - nowTime
if exp <= 0 {
exp = 60 * 60 * 2
}
key := ca.keyAccessToken(access.Account, access.Platform)
result := clientRedis.Set(key, access.AccessToken, time.Duration(exp))
return result.Err()
}
func (ca LoginTokenCache) RemoveAccessToken(account string, platform string) error {
key := ca.keyAccessToken(account, platform)
result := clientRedis.Del(key)
return result.Err()
}
func (ca LoginTokenCache) GetAccessToken(account string, platform string) (string, error) {
key := ca.keyAccessToken(account, platform)
result := clientRedis.Get(key)
return result.Result()
}
func (ca LoginTokenCache) SaveRefreshToken(access *domain.LoginAccess) error {
nowTime := time.Now().Unix()
exp := access.RefreshExpired - nowTime
if exp <= 0 {
exp = 60 * 60 * 2
}
key := ca.keyAccessToken(access.Account, access.Platform)
result := clientRedis.Set(key, access.RefreshToken, time.Duration(exp))
return result.Err()
}
func (ca LoginTokenCache) RemoveRefreshToken(account string, platform string) error {
key := ca.keyRefreshToken(account, platform)
result := clientRedis.Del(key)
return result.Err()
}
func (ca LoginTokenCache) GetRefreshToken(account string, platform string) (string, error) {
key := ca.keyRefreshToken(account, platform)
result := clientRedis.Get(key)
return result.Result()
}