-
Notifications
You must be signed in to change notification settings - Fork 1
/
redis-storage.go
92 lines (80 loc) · 2.48 KB
/
redis-storage.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
package storage
import (
"context"
"github.com/ennaque/go-gin-jwt"
"github.com/go-redis/redis/v8"
"time"
)
type RedisStorage struct {
adapter redisAdapterInterface
}
func (rs *RedisStorage) DeleteTokens(userId string, uuid ...string) error {
if err := rs.adapter.Del(context.Background(), rs._getStorageKeys(userId, uuid...)...); err != nil {
return err
}
return nil
}
func (rs *RedisStorage) SaveTokens(
userId string,
accessUuid string,
refreshUuid string,
accessExpire int64,
refreshExpire int64,
accessToken string,
refreshToken string) error {
_, err := rs.adapter.SaveMultipleInPipe(
context.Background(),
redisValue{key: rs._getStorageKey("a"+userId, accessUuid), value: accessToken,
expiration: time.Unix(accessExpire, 0).Sub(time.Now())},
redisValue{key: rs._getStorageKey("r"+userId, refreshUuid), value: refreshToken,
expiration: time.Unix(refreshExpire, 0).Sub(time.Now())},
)
if err != nil {
return gwt.ErrCannotSaveToken
}
return nil
}
func (rs *RedisStorage) HasRefreshToken(uuid string, token string, userId string) error {
return rs._isExpired("a"+rs._getStorageKey(userId, uuid), token)
}
func (rs *RedisStorage) HasAccessToken(uuid string, token string, userId string) error {
return rs._isExpired("r"+rs._getStorageKey(userId, uuid), token)
}
func (rs *RedisStorage) DeleteAllTokens(userId string) error {
userIdUuidKeys := rs._getUserIdUuidStorageKeys(userId)
if len(userIdUuidKeys) == 0 {
return gwt.ErrNotAuthUser
}
return rs.adapter.Del(context.Background(), userIdUuidKeys...)
}
func (rs *RedisStorage) _isExpired(key string, token string) error {
tkn, err := rs.adapter.Get(context.Background(), key)
if err != nil {
return gwt.ErrTokenExpired
}
if tkn != token {
return gwt.ErrTokenInvalid
}
return nil
}
func (rs *RedisStorage) _getUserIdUuidStorageKeys(userId string) []string {
var keysToDelete []string
iter := rs.adapter.GetScanIterator(context.Background(), 0, "[ar]"+userId+"_*", 0)
for iter.Next(context.Background()) {
keysToDelete = append(keysToDelete, iter.Val())
}
return keysToDelete
}
func (rs *RedisStorage) _getStorageKeys(userId string, uuids ...string) []string {
var keys []string
for _, key := range uuids {
keys = append(keys, rs._getStorageKey(userId, key))
}
return keys
}
func (rs *RedisStorage) _getStorageKey(userId string, uuid string) string {
return userId + "_" + uuid
}
func InitRedisStorage(client *redis.Client) gwt.StorageInterface {
return &RedisStorage{adapter: &redisAdapter{con: client}}
}