-
Notifications
You must be signed in to change notification settings - Fork 0
/
gsgopg.go
174 lines (153 loc) · 3.86 KB
/
gsgopg.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
package gsgopg
import (
"encoding/base32"
"net/http"
"strings"
"time"
"github.com/go-pg/pg"
"github.com/go-pg/pg/orm"
"github.com/gorilla/securecookie"
"github.com/gorilla/sessions"
)
// Session schema
type Session struct {
tableName struct{} `sql:"gorilla_session"`
Key string `sql:",type:varchar(52),pk"`
Data string `sql:",notnull"`
Expire time.Time `sql:",default:now()"`
}
// Creates session table if not present
func createSessionTable(db *pg.DB) error {
for _, model := range []interface{}{(*Session)(nil)} {
err := db.CreateTable(model, &orm.CreateTableOptions{
IfNotExists: true,
})
if err != nil {
return err
}
}
return nil
}
// GoPgStore implements Session store
type GoPgStore struct {
Codecs []securecookie.Codec
Options *sessions.Options
db *pg.DB
}
func NewGoPgStore(db *pg.DB, keyPairs ...[]byte) (*GoPgStore, error) {
createSessionTable(db)
store := &GoPgStore{
db: db,
Codecs: securecookie.CodecsFromPairs(keyPairs...),
}
return store, nil
}
func (s *GoPgStore) load(session *sessions.Session) error {
sess := &Session{Key: session.ID}
err := s.db.Model(sess).Limit(1).Select()
if err != nil {
return err
}
return securecookie.DecodeMulti(session.Name(), string(sess.Data),
&session.Values, s.Codecs...)
}
func (s *GoPgStore) delete(session *sessions.Session) error {
sess := &Session{
Key: session.ID,
}
_, err := s.db.Model(sess).WherePK().Delete()
if err != nil {
return err
}
return nil
}
func (s *GoPgStore) save(session *sessions.Session) error {
var err error
encoded, err := securecookie.EncodeMulti(session.Name(), session.Values, s.Codecs...)
if err != nil {
return err
}
sess := &Session{
Key: session.ID,
Data: encoded,
Expire: time.Now().Add(time.Second * time.Duration(session.Options.MaxAge)),
}
if session.IsNew == true {
err = s.db.Insert(sess)
} else {
_, err = s.db.Model(sess).Column("data").WherePK().Update()
}
if err != nil {
return err
}
return nil
}
func (s *GoPgStore) Save(r *http.Request, w http.ResponseWriter, session *sessions.Session) error {
if session.Options.MaxAge < 0 {
err := s.delete(session)
if err != nil {
return err
}
http.SetCookie(w, sessions.NewCookie(session.Name(), "", session.Options))
return nil
}
if session.ID == "" {
session.ID = strings.TrimRight(
base32.StdEncoding.EncodeToString(
securecookie.GenerateRandomKey(32)), "=")
}
if err := s.save(session); err != nil {
return err
}
encoded, err := securecookie.EncodeMulti(session.Name(), session.ID, s.Codecs...)
if err != nil {
return err
}
newCook := sessions.NewCookie(session.Name(), encoded, session.Options)
http.SetCookie(w, newCook)
return nil
}
func (s *GoPgStore) New(r *http.Request, name string) (*sessions.Session, error) {
session := sessions.NewSession(s, name)
options := *s.Options
session.Options = &options
session.IsNew = true
var err error
if cook, errCookie := r.Cookie(name); errCookie == nil {
err = securecookie.DecodeMulti(name, cook.Value, &session.ID, s.Codecs...)
if err == nil {
err = s.load(session)
if err == nil {
session.IsNew = false
} else {
err = nil
}
}
}
return session, err
}
func (s *GoPgStore) Get(r *http.Request, name string) (*sessions.Session, error) {
return sessions.GetRegistry(r).Get(s, name)
}
func (s *GoPgStore) Delete(r *http.Request, w http.ResponseWriter, session *sessions.Session) error {
err := s.delete(session)
return err
}
// Cleanup deletes all expired sessions
func (s *GoPgStore) Cleanup() {
sess := new(Session)
s.db.Model(sess).Where("expire < ?", time.Now().UTC()).Delete()
}
// PeriodicCleanup will execute expired sessions deletion per interval
func (s *GoPgStore) PeriodicCleanup(i time.Duration, quit <-chan struct{}) {
t := time.NewTicker(i)
defer t.Stop()
for {
select {
case <-t.C:
s.Cleanup()
case <-quit:
return
}
}
}