-
Notifications
You must be signed in to change notification settings - Fork 12
/
secure_cookie.go
346 lines (283 loc) · 7.68 KB
/
secure_cookie.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
package securecookie
import (
"fmt"
"sync"
"github.com/gorilla/securecookie"
)
const (
defaultCookieName = "session"
)
var (
// Error codes for store errors. This should match the codes
// defined in the /simplesessions package exactly.
ErrInvalidSession = &Err{code: 1, msg: "invalid session"}
ErrAssertType = &Err{code: 2, msg: "assertion failed"}
ErrNil = &Err{code: 3, msg: "nil returned"}
)
type Err struct {
code int
msg string
}
func (e *Err) Error() string {
return e.msg
}
func (e *Err) Code() int {
return e.code
}
// Store represents secure cookie session store
type Store struct {
// Temp map to store values before commit.
tempSetMap map[string]map[string]interface{}
mu sync.RWMutex
sc *securecookie.SecureCookie
cookieName string
}
// New creates a new secure cookie store instance. Gorilla/securecookie is used to encode and
// encrypt cookie.
// The secretKey is required, used to authenticate the cookie value using HMAC.
// It is recommended to use a key with 32 or 64 bytes.
// The blockKey is optional, used to encrypt the cookie value -- set it to nil to not use encryption.
// If set, the length must correspond to the block size of the encryption algorithm.
// For AES, used by default, valid lengths are 16, 24, or 32 bytes to select AES-128, AES-192, or AES-256.
func New(secretKey []byte, blockKey []byte) *Store {
return &Store{
cookieName: defaultCookieName,
sc: securecookie.New(secretKey, blockKey),
tempSetMap: make(map[string]map[string]interface{}),
}
}
// encode and encrypt given interface
func (s *Store) encode(val interface{}) (string, error) {
return s.sc.Encode(s.cookieName, val)
}
// decode encoded value to map
func (s *Store) decode(cookieVal string) (map[string]interface{}, error) {
val := make(map[string]interface{})
err := s.sc.Decode(s.cookieName, cookieVal, &val)
return val, err
}
// SetCookieName sets the cookie name for securecookie
func (s *Store) SetCookieName(cookieName string) {
s.cookieName = cookieName
}
// IsValid checks if the given cookie value is valid.
func (s *Store) IsValid(cv string) bool {
if _, err := s.decode(cv); err != nil {
return false
}
return true
}
// Create creates a new secure cookie session with empty map.
// Once called, Flush() should be called to retrieve the updated.
func (s *Store) Create(id string) error {
s.mu.Lock()
defer s.mu.Unlock()
s.tempSetMap[id] = make(map[string]interface{})
return nil
}
// Get returns a field value from session
func (s *Store) Get(cv, key string) (interface{}, error) {
// Decode cookie value
vals, err := s.decode(cv)
if err != nil {
return nil, ErrInvalidSession
}
// Get given field
val, ok := vals[key]
if !ok {
return nil, nil
}
return val, nil
}
// GetMulti returns values for multiple fields in session.
// If a field is not present then nil is returned.
func (s *Store) GetMulti(cv string, keys ...string) (map[string]interface{}, error) {
// Decode cookie value
vals, err := s.decode(cv)
if err != nil {
return nil, ErrInvalidSession
}
// Get all given fields
var (
ok bool
res = make(map[string]interface{})
)
for _, k := range keys {
res[k], ok = vals[k]
if !ok {
res[k] = nil
}
}
return res, nil
}
// GetAll returns all field for given session.
func (s *Store) GetAll(cv string) (map[string]interface{}, error) {
vals, err := s.decode(cv)
if err != nil {
return nil, ErrInvalidSession
}
return vals, nil
}
// Set sets a field in session but not saved untill commit is called.
// Flush() should be called to retrieve the updated, unflushed values
// and written to the cookie externally.
func (s *Store) Set(cv, key string, val interface{}) error {
s.mu.Lock()
defer s.mu.Unlock()
// Create session map if doesn't exist
if _, ok := s.tempSetMap[cv]; !ok {
s.tempSetMap[cv] = make(map[string]interface{})
}
// set value to map
s.tempSetMap[cv][key] = val
return nil
}
// SetMulti sets given map of kv pairs to session. Flush() should be
// called to retrieve the updated, unflushed values and written to the cookie
// externally.
func (s *Store) SetMulti(cv string, vals map[string]interface{}) error {
s.mu.Lock()
defer s.mu.Unlock()
// Create session map if doesn't exist
if _, ok := s.tempSetMap[cv]; !ok {
s.tempSetMap[cv] = make(map[string]interface{})
}
for k, v := range vals {
s.tempSetMap[cv][k] = v
}
return nil
}
// Flush flushes the 'set' buffer and returns encoded secure cookie value ready to be saved.
// This value should be written to the cookie externally.
// This can be used with simplessions.Session.WriteCookie.
// val, _ := str.Flush(cookieVal)
// sess.WriteCookie(val)
func (s *Store) Flush(cv string) (string, error) {
s.mu.Lock()
defer s.mu.Unlock()
vals, ok := s.tempSetMap[cv]
if !ok {
return "", fmt.Errorf("nothing to flush")
}
delete(s.tempSetMap, cv)
encoded, err := s.encode(vals)
return encoded, err
}
// Delete deletes a field from session. Once called, Flush() should be
// called to retrieve the updated, unflushed values and written to the cookie
// externally.
func (s *Store) Delete(cv string, keys ...string) error {
// Decode current cookie
vals, err := s.decode(cv)
if err != nil {
return ErrInvalidSession
}
for _, k := range keys {
// Delete given key in current values.
delete(vals, k)
}
// Create session map if doesn't exist.
s.mu.Lock()
defer s.mu.Unlock()
if _, ok := s.tempSetMap[cv]; !ok {
s.tempSetMap[cv] = make(map[string]interface{})
}
for k, v := range vals {
s.tempSetMap[cv][k] = v
}
// After this, Flush() should be called to obtain the updated encoded
// values to be written to the cookie externally.
return nil
}
// Clear clears the session. Once called, Flush() should be
// called to retrieve the updated, unflushed values and written to the cookie
// externally.
func (s *Store) Clear(cv string) error {
s.mu.Lock()
defer s.mu.Unlock()
s.tempSetMap[cv] = make(map[string]interface{})
return nil
}
// Destroy clears the session. Once called, Flush() should be
// called to retrieve the updated, unflushed values and written to the cookie
// externally.
func (s *Store) Destroy(cv string) error {
return s.Clear(cv)
}
// Int is a helper method to type assert as integer
func (s *Store) Int(r interface{}, err error) (int, error) {
if err != nil {
return 0, err
}
v, ok := r.(int)
if !ok {
err = ErrAssertType
}
return v, err
}
// Int64 is a helper method to type assert as Int64
func (s *Store) Int64(r interface{}, err error) (int64, error) {
if err != nil {
return 0, err
}
v, ok := r.(int64)
if !ok {
err = ErrAssertType
}
return v, err
}
// UInt64 is a helper method to type assert as UInt64
func (s *Store) UInt64(r interface{}, err error) (uint64, error) {
if err != nil {
return 0, err
}
v, ok := r.(uint64)
if !ok {
err = ErrAssertType
}
return v, err
}
// Float64 is a helper method to type assert as Float64
func (s *Store) Float64(r interface{}, err error) (float64, error) {
if err != nil {
return 0, err
}
v, ok := r.(float64)
if !ok {
err = ErrAssertType
}
return v, err
}
// String is a helper method to type assert as String
func (s *Store) String(r interface{}, err error) (string, error) {
if err != nil {
return "", err
}
v, ok := r.(string)
if !ok {
err = ErrAssertType
}
return v, err
}
// Bytes is a helper method to type assert as Bytes
func (s *Store) Bytes(r interface{}, err error) ([]byte, error) {
if err != nil {
return nil, err
}
v, ok := r.([]byte)
if !ok {
err = ErrAssertType
}
return v, err
}
// Bool is a helper method to type assert as Bool
func (s *Store) Bool(r interface{}, err error) (bool, error) {
if err != nil {
return false, err
}
v, ok := r.(bool)
if !ok {
err = ErrAssertType
}
return v, err
}