-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsession.go
282 lines (245 loc) · 7.44 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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
// Package sessions provides a minimalist and lightweight HTTP session cookie
// implementation for Go. Session cookies are encrypted and authenticated using
// nacl/secretbox.
//
// Example usage:
// package main
//
// import (
// "net/http"
// "time"
//
// "github.com/golangcollege/sessions"
// )
//
// var session *sessions.Session
// var secret = []byte("u46IpCV9y5Vlur8YvODJEhgOY8m9JVE4")
//
// func main() {
// session = sessions.New(secret)
// session.Lifetime = 3 * time.Hour
//
// mux := http.NewServeMux()
// mux.HandleFunc("/put", putHandler)
// mux.HandleFunc("/get", getHandler)
// http.ListenAndServe(":4000", session.Enable(mux))
// }
//
// func putHandler(w http.ResponseWriter, r *http.Request) {
// session.Put(r, "msg", "Hello world")
// w.WriteHeader(200)
// }
//
// func getHandler(w http.ResponseWriter, r *http.Request) {
// msg := session.GetString(r, "msg")
// w.Write([]byte(msg))
// }
//
package sessions
import (
"bufio"
"bytes"
"errors"
"log"
"net"
"net/http"
"time"
)
const cookieName = "session"
var ErrCookieTooLong = errors.New("session: cookie length greater than 4096 bytes")
// Session holds the configuration settings that you want to use for your sessions.
type Session struct {
// Domain sets the 'Domain' attribute on the session cookie. By default
// it will be set to the domain name that the cookie was issued from.
Domain string
// HttpOnly sets the 'HttpOnly' attribute on the session cookie. The
// default value is true.
HttpOnly bool
// Lifetime sets the maximum length of time that a session is valid for
// before it expires. The lifetime is an 'absolute expiry' which is set when
// the session is first created and does not change. The default value is 24
// hours.
Lifetime time.Duration
// Path sets the 'Path' attribute on the session cookie. The default value
// is "/". Passing the empty string "" will result in it being set to the
// path that the cookie was issued from.
Path string
// Persist sets whether the session cookie should be persistent or not
// (i.e. whether it should be retained after a user closes their browser).
// The default value is true, which means that the session cookie will not
// be destroyed when the user closes their browser and the appropriate
// 'Expires' and 'MaxAge' values will be added to the session cookie.
Persist bool
// Secure sets the 'Secure' attribute on the session cookie. The default
// value is false. It's recommended that you set this to true and serve all
// requests over HTTPS in production environments.
Secure bool
// SameSite controls the value of the 'SameSite' attribute on the session
// cookie. By default this is set to 'SameSite=Lax'. If you want no SameSite
// attribute or value in the session cookie then you should set this to 0.
SameSite http.SameSite
// ErrorHandler allows you to control behaviour when an error is encountered
// loading or writing the session cookie. By default the client is sent a
// generic "500 Internal Server Error" response and the actual error message
// is logged using the standard logger. If a custom ErrorHandler function is
// provided then control will be passed to this instead.
ErrorHandler func(http.ResponseWriter, *http.Request, error)
keys [][32]byte
}
// New initializes a new Session object to hold the configuration settings for
// your sessions.
//
// The key parameter is the secret you want to use to authenticate and encrypt
// session cookies. It should be exactly 32 bytes long.
//
// Optionally, the variadic oldKeys parameter can be used to provide an arbitrary
// number of old Keys. This can be used to ensure that valid cookies continue
// to work correctly after key rotation.
func New(key []byte, oldKeys ...[]byte) *Session {
keys := make([][32]byte, 1)
copy(keys[0][:], key)
for _, key := range oldKeys {
var newKey [32]byte
copy(newKey[:], key)
keys = append(keys, newKey)
}
return &Session{
Domain: "",
HttpOnly: true,
Lifetime: 24 * time.Hour,
Path: "/",
Persist: true,
Secure: false,
SameSite: http.SameSiteLaxMode,
ErrorHandler: defaultErrorHandler,
keys: keys,
}
}
// Enable is middleware which loads and saves session data to and from the
// session cookie. You should use this middleware to wrap ALL handlers which
// need to access to the session data. A common way to do this is to wrap your
// servemux.
//
// Note that session cookies are only sent to the client when the session data
// has been modified.
func (s *Session) Enable(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var err error
c, ok := r.Context().Value(contextKeyCache).(*cache)
if !ok {
c, err = s.load(r)
if err != nil {
s.ErrorHandler(w, r, err)
return
}
r = addCacheToRequestContext(r, c)
}
bw := &bufferedResponseWriter{ResponseWriter: w}
next.ServeHTTP(bw, r)
err = s.save(w, c)
if err != nil {
s.ErrorHandler(w, r, err)
return
}
if bw.code != 0 {
w.WriteHeader(bw.code)
}
w.Write(bw.buf.Bytes())
})
}
func (s *Session) load(r *http.Request) (*cache, error) {
cookie, err := r.Cookie(cookieName)
if err == http.ErrNoCookie {
return newCache(s.Lifetime), nil
} else if err != nil {
return nil, err
}
c := &cache{}
err = c.decode(cookie.Value, s.keys)
if err == errInvalidToken {
return newCache(s.Lifetime), nil
} else if err != nil {
return nil, err
}
if time.Now().After(c.Expiry) {
return newCache(s.Lifetime), nil
}
return c, nil
}
func (s *Session) save(w http.ResponseWriter, c *cache) error {
c.mu.Lock()
defer c.mu.Unlock()
if !c.modified {
return nil
}
if c.destroyed {
http.SetCookie(w, &http.Cookie{
Name: cookieName,
Value: "",
Path: s.Path,
Domain: s.Domain,
Secure: s.Secure,
HttpOnly: s.HttpOnly,
SameSite: s.SameSite,
Expires: time.Unix(1, 0),
MaxAge: -1,
})
return nil
}
token, err := c.encode(s.keys[0])
if err != nil {
return err
}
cookie := &http.Cookie{
Name: cookieName,
Value: token,
Path: s.Path,
Domain: s.Domain,
Secure: s.Secure,
HttpOnly: s.HttpOnly,
SameSite: s.SameSite,
}
if s.Persist {
cookie.Expires = time.Unix(c.Expiry.Unix()+1, 0) // Round up to the nearest second.
cookie.MaxAge = int(time.Until(c.Expiry).Seconds() + 1) // Round up to the nearest second.
}
if len(cookie.String()) > 4096 {
return ErrCookieTooLong
}
w.Header().Add("Vary", "Cookie")
http.SetCookie(w, cookie)
return nil
}
type bufferedResponseWriter struct {
http.ResponseWriter
buf bytes.Buffer
code int
}
func (bw *bufferedResponseWriter) Write(b []byte) (int, error) {
return bw.buf.Write(b)
}
func (bw *bufferedResponseWriter) WriteHeader(code int) {
bw.code = code
}
func (bw *bufferedResponseWriter) Push(target string, opts *http.PushOptions) error {
if pusher, ok := bw.ResponseWriter.(http.Pusher); ok {
return pusher.Push(target, opts)
}
return http.ErrNotSupported
}
func (bw *bufferedResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
hj := bw.ResponseWriter.(http.Hijacker)
return hj.Hijack()
}
func (bw *bufferedResponseWriter) Flush() {
f, ok := bw.ResponseWriter.(http.Flusher)
if ok == true {
bw.ResponseWriter.Write(bw.buf.Bytes())
f.Flush()
bw.buf.Reset()
}
}
func defaultErrorHandler(w http.ResponseWriter, r *http.Request, err error) {
log.Output(2, err.Error())
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
}