-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsession.go
132 lines (117 loc) · 3.66 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
// Copyright (c) 2023 William Dode. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for details.
package webo
import (
"context"
"fmt"
"log"
"net/http"
"net/url"
"time"
"github.com/gorilla/sessions"
)
type SessionStore struct {
*sessions.Session
}
func (f *SessionStore) PutString(key string, value string) {
f.Values[key] = value
}
func (f *SessionStore) GetString(key string) string {
r, _ := f.Values[key].(string)
return r
}
func (f *SessionStore) PutInt(key string, value int) {
f.Values[key] = value
}
func (f *SessionStore) GetInt(key string) (int, error) {
r, e := f.Values[key].(int)
if !e {
return r, fmt.Errorf("invalid type assertion %v not int %v", f.Values[key], e)
}
return r, nil
}
func (f *SessionStore) PutDate(key string, value time.Time) {
f.Values[key] = value.Unix()
}
func (f *SessionStore) GetDate(key string) (time.Time, error) {
r, e := f.Values[key].(int64)
if !e {
return time.Now(), fmt.Errorf("invalid type assertion %v not unix time.time %v", f.Values[key], e)
}
return time.Unix(r, 0), nil
}
func (f *SessionStore) PutForm(key string, u url.Values) {
f.PutString(key, u.Encode())
}
func (f *SessionStore) GetForm(key string) (url.Values, error) {
return url.ParseQuery(f.GetString(key))
}
func (f *SessionStore) AddFlashf(flag string, msg string, a ...interface{}) {
f.AddFlash(flag, fmt.Sprintf(msg, a...))
}
func (f *SessionStore) AddFlash(flag string, msg string) {
f.Session.AddFlash(msg, flag)
}
func (f *SessionStore) Flashes(flag string) []string {
fls := []string{}
for _, s := range f.Session.Flashes(flag) {
st, _ := s.(string)
fls = append(fls, st)
}
return fls
}
func (f *SessionStore) Info(msg string) { f.AddFlash("info", msg) }
func (f *SessionStore) Infof(msg string, a ...interface{}) {
f.Info(fmt.Sprintf(msg, a...))
}
func (f *SessionStore) Infos() []string { return f.Flashes("info") }
func (f *SessionStore) Warning(msg string) { f.AddFlash("warning", msg) }
func (f *SessionStore) Warningf(msg string, a ...interface{}) {
f.Warning(fmt.Sprintf(msg, a...))
}
func (f *SessionStore) Warnings() []string { return f.Flashes("warning") }
func (f *SessionStore) Alert(msg string) {
f.AddFlash("alert", msg)
}
func (f *SessionStore) Alertf(msg string, a ...interface{}) {
f.Alert(fmt.Sprintf(msg, a...))
}
func (f *SessionStore) Alerts() []string { return f.Flashes("alert") }
func RequestSession(r *http.Request) *SessionStore {
return r.Context().Value("webo-gsession").(*SessionStore)
}
type Session struct {
next http.Handler
}
func (s *Session) ServeHTTP(w http.ResponseWriter, r *http.Request) {
s.next.ServeHTTP(w, r)
}
func NewSession(auth string, key string, age int, next http.Handler) *Session {
return &Session{SessionMiddleware(auth, key, age)(next)}
}
// deprecate : use SessionNameMiddleware
func SessionMiddleware(auth string, key string, age int) func(next http.Handler) http.Handler {
return SessionNameMiddleware("gos", auth, key, age)
}
// session with possibility to set the name
func SessionNameMiddleware(name string, auth string, key string, age int) func(next http.Handler) http.Handler {
authb := make([]byte, 32, 32)
keyb := make([]byte, 32, 32)
copy(authb, []byte(auth))
copy(keyb, []byte(key))
store := sessions.NewCookieStore(authb, keyb)
store.MaxAge(age)
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
s, _ := store.Get(r, name)
sesG := &SessionStore{s}
ctx := context.WithValue(r.Context(), "webo-gsession", sesG)
r = r.WithContext(ctx)
next.ServeHTTP(w, r)
err := sesG.Save(r, w)
if err != nil {
log.Printf("session error : %s", err)
}
})
}
}