-
Notifications
You must be signed in to change notification settings - Fork 35
/
redis.go
244 lines (207 loc) · 6.31 KB
/
redis.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package redis
import (
"context"
"fmt"
"time"
"github.com/go-oauth2/oauth2/v4"
"github.com/go-oauth2/oauth2/v4/models"
"github.com/go-redis/redis/v8"
"github.com/google/uuid"
jsoniter "github.com/json-iterator/go"
)
var (
_ oauth2.TokenStore = &TokenStore{}
jsonMarshal = jsoniter.Marshal
jsonUnmarshal = jsoniter.Unmarshal
)
// NewRedisStore create an instance of a redis store
func NewRedisStore(opts *redis.Options, keyNamespace ...string) *TokenStore {
if opts == nil {
panic("options cannot be nil")
}
return NewRedisStoreWithCli(redis.NewClient(opts), keyNamespace...)
}
// NewRedisStoreWithCli create an instance of a redis store
func NewRedisStoreWithCli(cli *redis.Client, keyNamespace ...string) *TokenStore {
return NewRedisStoreWithInterface(cli, keyNamespace...)
}
// NewRedisClusterStore create an instance of a redis cluster store
func NewRedisClusterStore(opts *redis.ClusterOptions, keyNamespace ...string) *TokenStore {
if opts == nil {
panic("options cannot be nil")
}
return NewRedisClusterStoreWithCli(redis.NewClusterClient(opts), keyNamespace...)
}
// NewRedisClusterStoreWithCli create an instance of a redis cluster store
func NewRedisClusterStoreWithCli(cli *redis.ClusterClient, keyNamespace ...string) *TokenStore {
return NewRedisStoreWithInterface(cli, keyNamespace...)
}
// NewRedisStoreWithInterface create an instance of a redis store
func NewRedisStoreWithInterface(cli clienter, keyNamespace ...string) *TokenStore {
store := &TokenStore{
cli: cli,
}
if len(keyNamespace) > 0 {
store.ns = keyNamespace[0]
}
return store
}
type clienter interface {
Get(ctx context.Context, key string) *redis.StringCmd
Exists(ctx context.Context, key ...string) *redis.IntCmd
TxPipeline() redis.Pipeliner
Del(ctx context.Context, keys ...string) *redis.IntCmd
Close() error
}
// TokenStore redis token store
type TokenStore struct {
cli clienter
ns string
}
// Close close the store
func (s *TokenStore) Close() error {
return s.cli.Close()
}
func (s *TokenStore) wrapperKey(key string) string {
return fmt.Sprintf("%s%s", s.ns, key)
}
func (s *TokenStore) checkError(result redis.Cmder) (bool, error) {
if err := result.Err(); err != nil {
if err == redis.Nil {
return true, nil
}
return false, err
}
return false, nil
}
// remove
func (s *TokenStore) remove(ctx context.Context, key string) error {
result := s.cli.Del(ctx, s.wrapperKey(key))
_, err := s.checkError(result)
return err
}
func (s *TokenStore) removeToken(ctx context.Context, tokenString string, isRefresh bool) error {
basicID, err := s.getBasicID(ctx, tokenString)
if err != nil {
return err
} else if basicID == "" {
return nil
}
err = s.remove(ctx, tokenString)
if err != nil {
return err
}
token, err := s.getToken(ctx, basicID)
if err != nil {
return err
} else if token == nil {
return nil
}
checkToken := token.GetRefresh()
if isRefresh {
checkToken = token.GetAccess()
}
iresult := s.cli.Exists(ctx, s.wrapperKey(checkToken))
if err := iresult.Err(); err != nil && err != redis.Nil {
return err
} else if iresult.Val() == 0 {
return s.remove(ctx, basicID)
}
return nil
}
func (s *TokenStore) parseToken(result *redis.StringCmd) (oauth2.TokenInfo, error) {
if ok, err := s.checkError(result); err != nil {
return nil, err
} else if ok {
return nil, nil
}
buf, err := result.Bytes()
if err != nil {
if err == redis.Nil {
return nil, nil
}
return nil, err
}
var token models.Token
if err := jsonUnmarshal(buf, &token); err != nil {
return nil, err
}
return &token, nil
}
func (s *TokenStore) getToken(ctx context.Context, key string) (oauth2.TokenInfo, error) {
result := s.cli.Get(ctx, s.wrapperKey(key))
return s.parseToken(result)
}
func (s *TokenStore) parseBasicID(result *redis.StringCmd) (string, error) {
if ok, err := s.checkError(result); err != nil {
return "", err
} else if ok {
return "", nil
}
return result.Val(), nil
}
func (s *TokenStore) getBasicID(ctx context.Context, token string) (string, error) {
result := s.cli.Get(ctx, s.wrapperKey(token))
return s.parseBasicID(result)
}
// Create Create and store the new token information
func (s *TokenStore) Create(ctx context.Context, info oauth2.TokenInfo) error {
ct := time.Now()
jv, err := jsonMarshal(info)
if err != nil {
return err
}
pipe := s.cli.TxPipeline()
if code := info.GetCode(); code != "" {
pipe.Set(ctx, s.wrapperKey(code), jv, info.GetCodeExpiresIn())
} else {
basicID := uuid.Must(uuid.NewRandom()).String()
aexp := info.GetAccessExpiresIn()
rexp := aexp
if refresh := info.GetRefresh(); refresh != "" {
rexp = info.GetRefreshCreateAt().Add(info.GetRefreshExpiresIn()).Sub(ct)
if aexp.Seconds() > rexp.Seconds() {
aexp = rexp
}
pipe.Set(ctx, s.wrapperKey(refresh), basicID, rexp)
}
pipe.Set(ctx, s.wrapperKey(info.GetAccess()), basicID, aexp)
pipe.Set(ctx, s.wrapperKey(basicID), jv, rexp)
}
if _, err := pipe.Exec(ctx); err != nil {
return err
}
return nil
}
// RemoveByCode Use the authorization code to delete the token information
func (s *TokenStore) RemoveByCode(ctx context.Context, code string) error {
return s.remove(ctx, code)
}
// RemoveByAccess Use the access token to delete the token information
func (s *TokenStore) RemoveByAccess(ctx context.Context, access string) error {
return s.removeToken(ctx, access, false)
}
// RemoveByRefresh Use the refresh token to delete the token information
func (s *TokenStore) RemoveByRefresh(ctx context.Context, refresh string) error {
return s.removeToken(ctx, refresh, true)
}
// GetByCode Use the authorization code for token information data
func (s *TokenStore) GetByCode(ctx context.Context, code string) (oauth2.TokenInfo, error) {
return s.getToken(ctx, code)
}
// GetByAccess Use the access token for token information data
func (s *TokenStore) GetByAccess(ctx context.Context, access string) (oauth2.TokenInfo, error) {
basicID, err := s.getBasicID(ctx, access)
if err != nil || basicID == "" {
return nil, err
}
return s.getToken(ctx, basicID)
}
// GetByRefresh Use the refresh token for token information data
func (s *TokenStore) GetByRefresh(ctx context.Context, refresh string) (oauth2.TokenInfo, error) {
basicID, err := s.getBasicID(ctx, refresh)
if err != nil || basicID == "" {
return nil, err
}
return s.getToken(ctx, basicID)
}