-
Notifications
You must be signed in to change notification settings - Fork 15
/
storage.go
329 lines (261 loc) · 8.32 KB
/
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
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
package osinredis
import (
"bytes"
"encoding/gob"
"fmt"
"github.com/RangelReale/osin"
"github.com/garyburd/redigo/redis"
"github.com/pkg/errors"
"github.com/satori/go.uuid"
)
func init() {
gob.Register(map[string]interface{}{})
gob.Register(&osin.DefaultClient{})
gob.Register(osin.AuthorizeData{})
gob.Register(osin.AccessData{})
}
// Storage implements "github.com/RangelReale/osin".Storage
type Storage struct {
pool *redis.Pool
keyPrefix string
}
// New initializes and returns a new Storage
func New(pool *redis.Pool, keyPrefix string) *Storage {
return &Storage{
pool: pool,
keyPrefix: keyPrefix,
}
}
// Clone the storage if needed. For example, using mgo, you can clone the session with session.Clone
// to avoid concurrent access problems.
// This is to avoid cloning the connection at each method access.
// Can return itself if not a problem.
func (s *Storage) Clone() osin.Storage {
return s
}
// Close the resources the Storage potentially holds (using Clone for example)
func (s *Storage) Close() {}
// CreateClient inserts a new client
func (s *Storage) CreateClient(client osin.Client) error {
conn := s.pool.Get()
if err := conn.Err(); err != nil {
return err
}
defer conn.Close()
payload, err := encode(client)
if err != nil {
return errors.Wrap(err, "failed to encode client")
}
_, err = conn.Do("SET", s.makeKey("client", client.GetId()), payload)
return errors.Wrap(err, "failed to save client")
}
// GetClient gets a client by ID
func (s *Storage) GetClient(id string) (osin.Client, error) {
conn := s.pool.Get()
if err := conn.Err(); err != nil {
return nil, err
}
defer conn.Close()
var (
rawClientGob interface{}
err error
)
if rawClientGob, err = conn.Do("GET", s.makeKey("client", id)); err != nil {
return nil, errors.Wrap(err, "unable to GET client")
}
if rawClientGob == nil {
return nil, nil
}
clientGob, _ := redis.Bytes(rawClientGob, err)
var client osin.DefaultClient
err = decode(clientGob, &client)
return &client, errors.Wrap(err, "failed to decode client gob")
}
// UpdateClient updates a client
func (s *Storage) UpdateClient(client osin.Client) error {
return errors.Wrap(s.CreateClient(client), "failed to update client")
}
// DeleteClient deletes given client
func (s *Storage) DeleteClient(client osin.Client) error {
conn := s.pool.Get()
if err := conn.Err(); err != nil {
return err
}
defer conn.Close()
_, err := conn.Do("DEL", s.makeKey("client", client.GetId()))
return errors.Wrap(err, "failed to delete client")
}
// SaveAuthorize saves authorize data.
func (s *Storage) SaveAuthorize(data *osin.AuthorizeData) (err error) {
conn := s.pool.Get()
if err := conn.Err(); err != nil {
return err
}
defer conn.Close()
payload, err := encode(data)
if err != nil {
return errors.Wrap(err, "failed to encode data")
}
_, err = conn.Do("SETEX", s.makeKey("auth", data.Code), data.ExpiresIn, string(payload))
return errors.Wrap(err, "failed to set auth")
}
// LoadAuthorize looks up AuthorizeData by a code.
// Client information MUST be loaded together.
// Optionally can return error if expired.
func (s *Storage) LoadAuthorize(code string) (*osin.AuthorizeData, error) {
conn := s.pool.Get()
if err := conn.Err(); err != nil {
return nil, err
}
defer conn.Close()
var (
rawAuthGob interface{}
err error
)
if rawAuthGob, err = conn.Do("GET", s.makeKey("auth", code)); err != nil {
return nil, errors.Wrap(err, "unable to GET auth")
}
if rawAuthGob == nil {
return nil, nil
}
authGob, _ := redis.Bytes(rawAuthGob, err)
var auth osin.AuthorizeData
err = decode(authGob, &auth)
return &auth, errors.Wrap(err, "failed to decode auth")
}
// RemoveAuthorize revokes or deletes the authorization code.
func (s *Storage) RemoveAuthorize(code string) (err error) {
conn := s.pool.Get()
if err := conn.Err(); err != nil {
return err
}
defer conn.Close()
_, err = conn.Do("DEL", s.makeKey("auth", code))
return errors.Wrap(err, "failed to delete auth")
}
// SaveAccess creates AccessData.
func (s *Storage) SaveAccess(data *osin.AccessData) (err error) {
conn := s.pool.Get()
if err := conn.Err(); err != nil {
return err
}
defer conn.Close()
payload, err := encode(data)
if err != nil {
return errors.Wrap(err, "failed to encode access")
}
accessID := uuid.NewV4().String()
if _, err := conn.Do("SETEX", s.makeKey("access", accessID), data.ExpiresIn, string(payload)); err != nil {
return errors.Wrap(err, "failed to save access")
}
if _, err := conn.Do("SETEX", s.makeKey("access_token", data.AccessToken), data.ExpiresIn, accessID); err != nil {
return errors.Wrap(err, "failed to register access token")
}
_, err = conn.Do("SETEX", s.makeKey("refresh_token", data.RefreshToken), data.ExpiresIn, accessID)
return errors.Wrap(err, "failed to register refresh token")
}
// LoadAccess gets access data with given access token
func (s *Storage) LoadAccess(token string) (*osin.AccessData, error) {
return s.loadAccessByKey(s.makeKey("access_token", token))
}
// RemoveAccess deletes AccessData with given access token
func (s *Storage) RemoveAccess(token string) error {
return s.removeAccessByKey(s.makeKey("access_token", token))
}
// LoadRefresh gets access data with given refresh token
func (s *Storage) LoadRefresh(token string) (*osin.AccessData, error) {
return s.loadAccessByKey(s.makeKey("refresh_token", token))
}
// RemoveRefresh deletes AccessData with given refresh token
func (s *Storage) RemoveRefresh(token string) error {
return s.removeAccessByKey(s.makeKey("refresh_token", token))
}
func (s *Storage) removeAccessByKey(key string) error {
conn := s.pool.Get()
if err := conn.Err(); err != nil {
return err
}
defer conn.Close()
accessID, err := redis.String(conn.Do("GET", key))
if err != nil {
return errors.Wrap(err, "failed to get access")
}
access, err := s.loadAccessByKey(key)
if err != nil {
return errors.Wrap(err, "unable to load access for removal")
}
if access == nil {
return nil
}
accessKey := s.makeKey("access", accessID)
if _, err := conn.Do("DEL", accessKey); err != nil {
return errors.Wrap(err, "failed to delete access")
}
accessTokenKey := s.makeKey("access_token", access.AccessToken)
if _, err := conn.Do("DEL", accessTokenKey); err != nil {
return errors.Wrap(err, "failed to deregister access_token")
}
refreshTokenKey := s.makeKey("refresh_token", access.RefreshToken)
_, err = conn.Do("DEL", refreshTokenKey)
return errors.Wrap(err, "failed to deregister refresh_token")
}
func (s *Storage) loadAccessByKey(key string) (*osin.AccessData, error) {
conn := s.pool.Get()
if err := conn.Err(); err != nil {
return nil, err
}
defer conn.Close()
var (
rawAuthGob interface{}
err error
)
if rawAuthGob, err = conn.Do("GET", key); err != nil {
return nil, errors.Wrap(err, "unable to GET auth")
}
if rawAuthGob == nil {
return nil, nil
}
accessID, err := redis.String(conn.Do("GET", key))
if err != nil {
return nil, errors.Wrap(err, "unable to get access ID")
}
accessIDKey := s.makeKey("access", accessID)
accessGob, err := redis.Bytes(conn.Do("GET", accessIDKey))
if err != nil {
return nil, errors.Wrap(err, "unable to get access gob")
}
var access osin.AccessData
if err := decode(accessGob, &access); err != nil {
return nil, errors.Wrap(err, "failed to decode access gob")
}
ttl, err := redis.Int(conn.Do("TTL", accessIDKey))
if err != nil {
return nil, errors.Wrap(err, "unable to get access TTL")
}
access.ExpiresIn = int32(ttl)
access.Client, err = s.GetClient(access.Client.GetId())
if err != nil {
return nil, errors.Wrap(err, "unable to get client for access")
}
if access.AuthorizeData != nil && access.AuthorizeData.Client != nil {
access.AuthorizeData.Client, err = s.GetClient(access.AuthorizeData.Client.GetId())
if err != nil {
return nil, errors.Wrap(err, "unable to get client for access authorize data")
}
}
return &access, nil
}
func (s *Storage) makeKey(namespace, id string) string {
return fmt.Sprintf("%s:%s:%s", s.keyPrefix, namespace, id)
}
func encode(v interface{}) ([]byte, error) {
var buf bytes.Buffer
if err := gob.NewEncoder(&buf).Encode(v); err != nil {
return nil, errors.Wrap(err, "unable to encode")
}
return buf.Bytes(), nil
}
func decode(data []byte, v interface{}) error {
err := gob.NewDecoder(bytes.NewBuffer(data)).Decode(v)
return errors.Wrap(err, "unable to decode")
}