-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.go
211 lines (181 loc) · 5.37 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
package session
import (
"context"
"encoding/gob"
"log"
"net/http"
"time"
"github.com/gorilla/securecookie"
)
type sessionCtxKeyType struct{}
const (
defaultSessionName = "_session"
defaultMaxAge = 86400 * 365
)
var (
sessionCtxKey = sessionCtxKeyType{}
)
func init() {
// Register the encodings used in this package with gob such that we can
// successfully save session data in the session.
gob.Register(map[string]interface{}{})
gob.Register(&session{})
}
// A Session manages setting and getting data from the cookie that stores the
// session data.
type Session struct {
sc *securecookie.SecureCookie
name string
}
// Options to customize the behaviour of the session.
type Options struct {
// The name of the cookie (default is "_session").
Name string
// MaxAge of the cookie before expiry (default is 365 days). Set it to
// -1 for no expiry.
MaxAge int
}
// New creates a new session with the given key.
func New(secret []byte, opts ...Options) *Session {
var o Options
for _, opt := range opts {
o = opt
}
if o.Name == "" {
o.Name = defaultSessionName
}
switch o.MaxAge {
case 0:
// Default to one year, since some browsers don't set their cookies
// with the same defaults.
o.MaxAge = defaultMaxAge
case -1:
o.MaxAge = 0
}
sc := securecookie.New(secret, nil)
sc.MaxAge(o.MaxAge)
return &Session{
sc: sc,
name: o.Name,
}
}
// A session holds the session data. It contains two fields:
//
// - "data" for long-lived session data that persists between requests,
//
// - "flashes" for session data that should be deleted as soon as it is shown.
type session struct {
Data map[string]interface{}
Flashes map[string]interface{}
}
// init ensures that both of the underlying maps have been initialized.
func (s *session) init() {
if s.Data == nil {
s.Data = make(map[string]interface{})
}
if s.Flashes == nil {
s.Flashes = make(map[string]interface{})
}
}
// fromReq returns the map of session values from the request. It will
// never return a nil map, instead, the map will be an initialized empty map
// in the case where the session has no data.
func (s *Session) fromReq(r *http.Request) *session {
// Fastpath: if the context has already been decoded, access the
// underlying map and return the value associated with the given key.
v := r.Context().Value(sessionCtxKey)
if v != nil {
ss, ok := v.(*session)
if ok {
return ss
}
}
cookie, err := r.Cookie(s.name)
if err != nil {
// The only error that can be returned by r.Cookie() is ErrNoCookie,
// so if the error is not nil, that means that the cookie doesn't
// exist. When that is the case, the value associated with the given
// key is guaranteed to be nil, so we return nil.
ss := &session{}
ss.init()
return ss
}
ss := &session{}
if err := s.sc.Decode(s.name, cookie.Value, ss); err != nil {
log.Println("[error] failed to decode session from cookie:", err)
ss.init()
return ss
}
return ss
}
// saveCtx saves a map of session data in the current request's context. It
// also updates the Set-Cookie header of the
func (s *Session) saveCtx(w http.ResponseWriter, r *http.Request, session *session) {
ctx := context.WithValue(r.Context(), sessionCtxKey, session)
r2 := r.Clone(ctx)
*r = *r2
encoded, err := s.sc.Encode(s.name, session)
if err != nil {
log.Println("error encoding cookie:", err)
return
}
http.SetCookie(w, &http.Cookie{
Name: s.name,
MaxAge: defaultMaxAge,
Expires: time.Now().UTC().Add(time.Duration(defaultMaxAge * time.Second)),
Value: encoded,
Path: "/",
HttpOnly: true,
Secure: true,
})
}
// Session creates a new session from the given HTTP request. If the
// request already has a cookie with an associated session, the session data
// is created from the cookie. If not, a new session is created.
func (s *Session) Get(r *http.Request, key string) interface{} {
data := s.fromReq(r)
return data.Data[key]
}
// List returns all key value pairs of session data from the given request.
func (s *Session) List(r *http.Request) map[string]interface{} {
return s.fromReq(r).Data
}
// Set sets or updates the given value on the session.
func (s *Session) Set(w http.ResponseWriter, r *http.Request, key string, value interface{}) {
data := s.fromReq(r)
data.Data[key] = value
s.saveCtx(w, r, data)
}
// Delete deletes and returns the session value with the given key.
func (s *Session) Delete(w http.ResponseWriter, r *http.Request, key string) interface{} {
data := s.fromReq(r)
value := data.Data[key]
delete(data.Data, key)
s.saveCtx(w, r, data)
return value
}
// Reset resets the session, deleting all values.
func (s *Session) Reset(w http.ResponseWriter, r *http.Request) {
s.saveCtx(w, r, &session{
Data: make(map[string]interface{}),
Flashes: make(map[string]interface{}),
})
}
// Flash sets a flash message on a request.
func (s *Session) Flash(w http.ResponseWriter, r *http.Request, key string, value interface{}) {
data := s.fromReq(r)
data.Flashes[key] = value
s.saveCtx(w, r, data)
}
// Flashes returns all flash messages, clearing all saved flashes.
func (s *Session) Flashes(w http.ResponseWriter, r *http.Request) map[string]interface{} {
data := s.fromReq(r)
// Copy the map before clearing it from the session.
values := make(map[string]interface{})
for k, v := range data.Flashes {
values[k] = v
}
data.Flashes = make(map[string]interface{})
s.saveCtx(w, r, data)
return values
}