-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsession.go
260 lines (226 loc) · 8 KB
/
session.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
package jeen
import (
"context"
"time"
"github.com/alexedwards/scs/v2"
)
// SCS Session wrapper
// credit: github.com/alexedwards/scs/v2
type Session struct {
session *scs.SessionManager
context context.Context
}
// session value returned from Get
type sessionValue struct {
value interface{}
}
// Create wrapper for scs session manager.
func getSession(ctx context.Context, sess *scs.SessionManager) *Session {
return &Session{
context: ctx,
session: session,
}
}
// Get returns the value for a given key from the session data. The return value
// has the type interface{} so will usually need to be type asserted
// before you can use it.
func (s *Session) Get(key string, pop ...bool) *sessionValue {
var val interface{}
if len(pop) > 0 && pop[0] {
val = s.session.Pop(s.context, key)
} else {
val = s.session.Get(s.context, key)
}
return &sessionValue{
value: val,
}
}
// Set adds a key and corresponding value to the session data. Any existing
// value for the key will be replaced. The session data status will be set to
// Modified.
func (s *Session) Set(key string, value interface{}) {
s.session.Put(s.context, key, value)
}
// SetMap adds a key and corresponding value to the session data from a Map.
// Any existing value for the key will be replaced. The session data
// status will be set to Modified.
func (s *Session) SetMap(m Map) {
for key, value := range m {
s.session.Put(s.context, key, value)
}
}
// Destroy deletes the session data from the session store and sets the session
// status to Destroyed. Any further operations in the same request cycle will
// result in a new session being created.
func (s *Session) Destroy() error {
return s.session.Destroy(s.context)
}
// Remove deletes the given key and corresponding value from the session data.
// The session data status will be set to Modified. If the key is not present
// this operation is a no-op.
func (s *Session) Remove(key string) {
s.session.Remove(s.context, key)
}
// Clear removes all data for the current session. The session token and
// lifetime are unaffected. If there is no data in the current session this is
// a no-op.
func (s *Session) Clear() error {
return s.session.Clear(s.context)
}
// Exists returns true if the given key is present in the session data.
func (s *Session) Exists(key string) bool {
return s.session.Exists(s.context, key)
}
// Keys returns a slice of all key names present in the session data, sorted
// alphabetically. If the data contains no data then an empty slice will be
// returned.
func (s *Session) Keys() []string {
return s.session.Keys(s.context)
}
// RenewToken updates the session data to have a new session token while
// retaining the current session data. The session lifetime is also reset and
// the session data status will be set to Modified.
//
// The old session token and accompanying data are deleted from the session store.
//
// To mitigate the risk of session fixation attacks, it's important that you call
// RenewToken before making any changes to privilege levels (e.g. login and
// logout operations). See https://github.com/OWASP/CheatSheetSeries/blob/master/cheatsheets/Session_Management_Cheat_Sheet.md#renew-the-session-id-after-any-privilege-level-change
// for additional information.
func (s *Session) RenewToken() error {
return s.session.RenewToken(s.context)
}
// MergeSession is used to merge in data from a different session in case strict
// session tokens are lost across an oauth or similar redirect flows. Use Clear()
// if no values of the new session are to be used.
func (s *Session) MergeSession(token string) error {
return s.session.MergeSession(s.context, token)
}
// Status returns the current status of the session data.
func (s *Session) Status() scs.Status {
return s.session.Status(s.context)
}
// RememberMe controls whether the session cookie is persistent (i.e whether it
// is retained after a user closes their browser). RememberMe only has an effect
// if you have set SessionManager.Cookie.Persist = false (the default is true) and
// you are using the standard LoadAndSave() middleware.
func (s *Session) RememberMe(val bool) {
s.session.RememberMe(s.context, val)
}
// Iterate retrieves all active (i.e. not expired) sessions from the store and
// executes the provided function fn for each session. If the session store
// being used does not support iteration then Iterate will panic.
func (s *Session) Iterate(fn func(session *Session) error) error {
return s.session.Iterate(s.context, func(c context.Context) error {
sess := getSession(c, s.session)
return fn(sess)
})
}
// Deadline returns the 'absolute' expiry time for the session. Please note
// that if you are using an idle timeout, it is possible that a session will
// expire due to non-use before the returned deadline.
func (s *Session) Deadline() time.Time {
return s.session.Deadline(s.context)
}
// Token returns the session token. Please note that this will return the
// empty string "" if it is called before the session has been committed to
// the store.
func (s *Session) Token() string {
return s.session.Token(s.context)
}
// SESSION VALUE
// Value returns the interface{} value for a given key from the session data.
func (s *sessionValue) Value() interface{} {
return s.value
}
// String returns the string value for a given key from the session data.
// The zero value for a string ("") is returned if the key does not exist or the
// value could not be type asserted to a string.
func (s *sessionValue) String() string {
str, ok := s.value.(string)
if !ok {
return ""
}
return str
}
// Bool returns the bool value for a given key from the session data. The
// zero value for a bool (false) is returned if the key does not exist or the
// value could not be type asserted to a bool.
func (s *sessionValue) Bool() bool {
val, ok := s.value.(bool)
if !ok {
return false
}
return val
}
// Bytes returns the byte slice ([]byte) value for a given key from the session
// data. The zero value for a slice (nil) is returned if the key does not exist
// or could not be type asserted to []byte.
func (s *sessionValue) Bytes() []byte {
val, ok := s.value.([]byte)
if !ok {
return nil
}
return val
}
// Time returns the time.Time value for a given key from the session data. The
// zero value for a time.Time object is returned if the key does not exist or the
// value could not be type asserted to a time.Time. This can be tested with the
// time.IsZero() method.
func (s *sessionValue) Time() time.Time {
val, ok := s.value.(time.Time)
if !ok {
return time.Time{}
}
return val
}
// Int returns the int value for a given key from the session data. The
// zero value for an int (0) is returned if the key does not exist or the
// value could not be type asserted to an int.
func (s *sessionValue) Int() int {
val, ok := s.value.(int)
if !ok {
return 0
}
return val
}
// Int32 returns the int32 value for a given key from the session data. The
// zero value for an int (0) is returned if the key does not exist or the
// value could not be type asserted to an int32.
func (s *sessionValue) Int32() int32 {
val, ok := s.value.(int32)
if !ok {
return 0
}
return val
}
// Int64 returns the int64 value for a given key from the session data. The
// zero value for an int (0) is returned if the key does not exist or the
// value could not be type asserted to an int64.
func (s *sessionValue) Int64() int64 {
val, ok := s.value.(int64)
if !ok {
return 0
}
return val
}
// Float32 returns the float32 value for a given key from the session data. The
// zero value for an float (0) is returned if the key does not exist or the
// value could not be type asserted to an float32.
func (s *sessionValue) Float32() float32 {
val, ok := s.value.(float32)
if !ok {
return 0.0
}
return val
}
// Float64 returns the float64 value for a given key from the session data. The
// zero value for an float (0) is returned if the key does not exist or the
// value could not be type asserted to an float64.
func (s *sessionValue) Float64() float64 {
val, ok := s.value.(float64)
if !ok {
return 0.0
}
return val
}