forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
session.go
52 lines (43 loc) · 1.14 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
package session
import (
"net/http"
"github.com/gorilla/context"
"github.com/gorilla/securecookie"
"github.com/gorilla/sessions"
)
type store struct {
store sessions.Store
}
func NewStore(secure bool, maxAgeSeconds int, secrets ...string) Store {
values := [][]byte{}
for _, secret := range secrets {
values = append(values, []byte(secret))
}
cookie := sessions.NewCookieStore(values...)
cookie.Options.MaxAge = maxAgeSeconds
cookie.Options.HttpOnly = true
cookie.Options.Secure = secure
return store{cookie}
}
func (s store) Get(req *http.Request, name string) (Session, error) {
session, err := s.store.Get(req, name)
if err != nil && err.Error() == securecookie.ErrMacInvalid.Error() {
err = nil
}
return sessionWrapper{session}, err
}
func (s store) Save(w http.ResponseWriter, req *http.Request) error {
return sessions.Save(req, w)
}
func (s store) Wrap(h http.Handler) http.Handler {
return context.ClearHandler(h)
}
type sessionWrapper struct {
session *sessions.Session
}
func (s sessionWrapper) Values() map[interface{}]interface{} {
if s.session == nil {
return map[interface{}]interface{}{}
}
return s.session.Values
}