1
1
package scs
2
2
3
3
import (
4
- "bytes"
5
4
"context"
6
5
"crypto/rand"
7
6
"encoding/base64"
8
- "encoding/gob"
9
7
"fmt"
10
8
"sort"
11
9
"sync"
@@ -30,18 +28,18 @@ const (
30
28
)
31
29
32
30
type sessionData struct {
33
- Deadline time.Time // Exported for gob encoding.
31
+ deadline time.Time
34
32
status Status
35
33
token string
36
- Values map [string ]interface {} // Exported for gob encoding.
34
+ values map [string ]interface {}
37
35
mu sync.Mutex
38
36
}
39
37
40
38
func newSessionData (lifetime time.Duration ) * sessionData {
41
39
return & sessionData {
42
- Deadline : time .Now ().Add (lifetime ).UTC (),
40
+ deadline : time .Now ().Add (lifetime ).UTC (),
43
41
status : Unmodified ,
44
- Values : make (map [string ]interface {}),
42
+ values : make (map [string ]interface {}),
45
43
}
46
44
}
47
45
@@ -71,7 +69,7 @@ func (s *SessionManager) Load(ctx context.Context, token string) (context.Contex
71
69
status : Unmodified ,
72
70
token : token ,
73
71
}
74
- err = sd . decode (b )
72
+ sd . deadline , sd . values , err = s . Codec . Decode (b )
75
73
if err != nil {
76
74
return nil , err
77
75
}
@@ -104,12 +102,12 @@ func (s *SessionManager) Commit(ctx context.Context) (string, time.Time, error)
104
102
}
105
103
}
106
104
107
- b , err := sd . encode ( )
105
+ b , err := s . Codec . Encode ( sd . deadline , sd . values )
108
106
if err != nil {
109
107
return "" , time.Time {}, err
110
108
}
111
109
112
- expiry := sd .Deadline
110
+ expiry := sd .deadline
113
111
if s .IdleTimeout > 0 {
114
112
ie := time .Now ().Add (s .IdleTimeout )
115
113
if ie .Before (expiry ) {
@@ -143,9 +141,9 @@ func (s *SessionManager) Destroy(ctx context.Context) error {
143
141
144
142
// Reset everything else to defaults.
145
143
sd .token = ""
146
- sd .Deadline = time .Now ().Add (s .Lifetime ).UTC ()
147
- for key := range sd .Values {
148
- delete (sd .Values , key )
144
+ sd .deadline = time .Now ().Add (s .Lifetime ).UTC ()
145
+ for key := range sd .values {
146
+ delete (sd .values , key )
149
147
}
150
148
151
149
return nil
@@ -158,7 +156,7 @@ func (s *SessionManager) Put(ctx context.Context, key string, val interface{}) {
158
156
sd := s .getSessionDataFromContext (ctx )
159
157
160
158
sd .mu .Lock ()
161
- sd .Values [key ] = val
159
+ sd .values [key ] = val
162
160
sd .status = Modified
163
161
sd .mu .Unlock ()
164
162
}
@@ -180,7 +178,7 @@ func (s *SessionManager) Get(ctx context.Context, key string) interface{} {
180
178
sd .mu .Lock ()
181
179
defer sd .mu .Unlock ()
182
180
183
- return sd .Values [key ]
181
+ return sd .values [key ]
184
182
}
185
183
186
184
// Pop acts like a one-time Get. It returns the value for a given key from the
@@ -193,11 +191,11 @@ func (s *SessionManager) Pop(ctx context.Context, key string) interface{} {
193
191
sd .mu .Lock ()
194
192
defer sd .mu .Unlock ()
195
193
196
- val , exists := sd .Values [key ]
194
+ val , exists := sd .values [key ]
197
195
if ! exists {
198
196
return nil
199
197
}
200
- delete (sd .Values , key )
198
+ delete (sd .values , key )
201
199
sd .status = Modified
202
200
203
201
return val
@@ -212,12 +210,12 @@ func (s *SessionManager) Remove(ctx context.Context, key string) {
212
210
sd .mu .Lock ()
213
211
defer sd .mu .Unlock ()
214
212
215
- _ , exists := sd .Values [key ]
213
+ _ , exists := sd .values [key ]
216
214
if ! exists {
217
215
return
218
216
}
219
217
220
- delete (sd .Values , key )
218
+ delete (sd .values , key )
221
219
sd .status = Modified
222
220
}
223
221
@@ -230,12 +228,12 @@ func (s *SessionManager) Clear(ctx context.Context) error {
230
228
sd .mu .Lock ()
231
229
defer sd .mu .Unlock ()
232
230
233
- if len (sd .Values ) == 0 {
231
+ if len (sd .values ) == 0 {
234
232
return nil
235
233
}
236
234
237
- for key := range sd .Values {
238
- delete (sd .Values , key )
235
+ for key := range sd .values {
236
+ delete (sd .values , key )
239
237
}
240
238
sd .status = Modified
241
239
return nil
@@ -246,7 +244,7 @@ func (s *SessionManager) Exists(ctx context.Context, key string) bool {
246
244
sd := s .getSessionDataFromContext (ctx )
247
245
248
246
sd .mu .Lock ()
249
- _ , exists := sd .Values [key ]
247
+ _ , exists := sd .values [key ]
250
248
sd .mu .Unlock ()
251
249
252
250
return exists
@@ -259,9 +257,9 @@ func (s *SessionManager) Keys(ctx context.Context) []string {
259
257
sd := s .getSessionDataFromContext (ctx )
260
258
261
259
sd .mu .Lock ()
262
- keys := make ([]string , len (sd .Values ))
260
+ keys := make ([]string , len (sd .values ))
263
261
i := 0
264
- for key := range sd .Values {
262
+ for key := range sd .values {
265
263
keys [i ] = key
266
264
i ++
267
265
}
@@ -298,7 +296,7 @@ func (s *SessionManager) RenewToken(ctx context.Context) error {
298
296
}
299
297
300
298
sd .token = newToken
301
- sd .Deadline = time .Now ().Add (s .Lifetime ).UTC ()
299
+ sd .deadline = time .Now ().Add (s .Lifetime ).UTC ()
302
300
sd .status = Modified
303
301
304
302
return nil
@@ -477,21 +475,6 @@ func (s *SessionManager) getSessionDataFromContext(ctx context.Context) *session
477
475
return c
478
476
}
479
477
480
- func (sd * sessionData ) encode () ([]byte , error ) {
481
- var b bytes.Buffer
482
- err := gob .NewEncoder (& b ).Encode (sd )
483
- if err != nil {
484
- return nil , err
485
- }
486
-
487
- return b .Bytes (), nil
488
- }
489
-
490
- func (sd * sessionData ) decode (b []byte ) error {
491
- r := bytes .NewReader (b )
492
- return gob .NewDecoder (r ).Decode (sd )
493
- }
494
-
495
478
func generateToken () (string , error ) {
496
479
b := make ([]byte , 32 )
497
480
_ , err := rand .Read (b )
0 commit comments