-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.go
70 lines (56 loc) · 2.01 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
package webutil
import (
"context"
"net/http"
"github.com/gorilla/securecookie"
"github.com/gorilla/sessions"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
type sessionContextKeyType int
const (
sessionName = `rebuy-go-sdk`
sessionContextKey sessionContextKeyType = 0
)
type SessionSecret []byte
// SessionSecretSourceVolatile creates a session secret that is stored in
// memory only. This implies, that all session data is lost after an
// application restart and an application cannot have more than one replicas.
func SessionSecretSourceVolatile() []byte {
return securecookie.GenerateRandomKey(32)
}
// SessionFromContext extracts the Session store from the given context. The
// session store is injected into the request via the SessionMiddleware.
// Therefore it is required to use this middleware to be able to get the store.
func SessionFromContext(ctx context.Context) (*sessions.Session, error) {
sess, ok := ctx.Value(sessionContextKey).(*sessions.Session)
if !ok {
return nil, errors.Errorf("session not found in context")
}
return sess, nil
}
// SessionFromRequest returns the results of SessionFromContext for the context
// of the given request.
func SessionFromRequest(r *http.Request) (*sessions.Session, error) {
return SessionFromContext(r.Context())
}
// SessionMiddleware inizializes the session store and injects it into the
// context of the requests.
func SessionMiddleware(secret SessionSecret) Middleware {
return func(next http.Handler) http.Handler {
return sessionMiddlewareFunc(next, secret)
}
}
func sessionMiddlewareFunc(next http.Handler, secret SessionSecret) http.Handler {
store := sessions.NewCookieStore(secret)
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
session, err := store.Get(r, sessionName)
if err != nil {
logrus.WithError(err).Warn("failed to restore session; creating new one")
session.Save(r, w)
}
ctx := context.WithValue(r.Context(), sessionContextKey, session)
r = r.WithContext(ctx)
next.ServeHTTP(w, r)
})
}