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
283
284
|
/*
Gorilla Sessions backend for SQLite.
Copyright (c) 2013 Contributors. See the list of contributors in the CONTRIBUTORS file for details.
This software is licensed under a MIT style license available in the LICENSE file.
*/
package main
import (
"database/sql"
"encoding/gob"
"errors"
"fmt"
"log"
"net/http"
"strings"
"time"
"github.com/gorilla/securecookie"
"github.com/gorilla/sessions"
_ "modernc.org/sqlite"
)
type SqliteStore struct {
db DB
stmtInsert *sql.Stmt
stmtDelete *sql.Stmt
stmtUpdate *sql.Stmt
stmtSelect *sql.Stmt
Codecs []securecookie.Codec
Options *sessions.Options
table string
}
type sessionRow struct {
id string
data string
createdOn time.Time
modifiedOn time.Time
expiresOn time.Time
}
type DB interface {
Exec(query string, args ...interface{}) (sql.Result, error)
Prepare(query string) (*sql.Stmt, error)
Close() error
}
func init() {
gob.Register(time.Time{})
}
func NewSqliteStore(endpoint string, tableName string, path string, maxAge int, keyPairs ...[]byte) (*SqliteStore, error) {
db, err := sql.Open("sqlite3", endpoint)
if err != nil {
return nil, err
}
return NewSqliteStoreFromConnection(db, tableName, path, maxAge, keyPairs...)
}
func NewSqliteStoreFromConnection(db DB, tableName string, path string, maxAge int, keyPairs ...[]byte) (*SqliteStore, error) {
// Make sure table name is enclosed.
tableName = "`" + strings.Trim(tableName, "`") + "`"
cTableQ := "CREATE TABLE IF NOT EXISTS " +
tableName + " (id INTEGER PRIMARY KEY, " +
"session_data LONGBLOB, " +
"created_on TIMESTAMP DEFAULT 0, " +
"modified_on TIMESTAMP DEFAULT CURRENT_TIMESTAMP, " +
"expires_on TIMESTAMP DEFAULT 0);"
if _, err := db.Exec(cTableQ); err != nil {
return nil, err
}
insQ := "INSERT INTO " + tableName +
"(id, session_data, created_on, modified_on, expires_on) VALUES (NULL, ?, ?, ?, ?)"
stmtInsert, stmtErr := db.Prepare(insQ)
if stmtErr != nil {
return nil, stmtErr
}
delQ := "DELETE FROM " + tableName + " WHERE id = ?"
stmtDelete, stmtErr := db.Prepare(delQ)
if stmtErr != nil {
return nil, stmtErr
}
updQ := "UPDATE " + tableName + " SET session_data = ?, created_on = ?, expires_on = ? " +
"WHERE id = ?"
stmtUpdate, stmtErr := db.Prepare(updQ)
if stmtErr != nil {
return nil, stmtErr
}
selQ := "SELECT id, session_data, created_on, modified_on, expires_on from " +
tableName + " WHERE id = ?"
stmtSelect, stmtErr := db.Prepare(selQ)
if stmtErr != nil {
return nil, stmtErr
}
return &SqliteStore{
db: db,
stmtInsert: stmtInsert,
stmtDelete: stmtDelete,
stmtUpdate: stmtUpdate,
stmtSelect: stmtSelect,
Codecs: securecookie.CodecsFromPairs(keyPairs...),
Options: &sessions.Options{
Path: path,
MaxAge: maxAge,
},
table: tableName,
}, nil
}
func (m *SqliteStore) Close() {
m.stmtSelect.Close()
m.stmtUpdate.Close()
m.stmtDelete.Close()
m.stmtInsert.Close()
m.db.Close()
}
func (m *SqliteStore) Get(r *http.Request, name string) (*sessions.Session, error) {
return sessions.GetRegistry(r).Get(m, name)
}
func (m *SqliteStore) New(r *http.Request, name string) (*sessions.Session, error) {
session := sessions.NewSession(m, name)
session.Options = &sessions.Options{
Path: m.Options.Path,
MaxAge: m.Options.MaxAge,
}
session.IsNew = true
var err error
if cook, errCookie := r.Cookie(name); errCookie == nil {
err = securecookie.DecodeMulti(name, cook.Value, &session.ID, m.Codecs...)
if err == nil {
err = m.load(session)
if err == nil {
session.IsNew = false
} else {
err = nil
}
}
}
return session, err
}
func (m *SqliteStore) Save(r *http.Request, w http.ResponseWriter, session *sessions.Session) error {
var err error
if session.ID == "" {
if err = m.insert(session); err != nil {
return err
}
} else if err = m.save(session); err != nil {
return err
}
encoded, err := securecookie.EncodeMulti(session.Name(), session.ID, m.Codecs...)
if err != nil {
return err
}
http.SetCookie(w, sessions.NewCookie(session.Name(), encoded, session.Options))
return nil
}
func (m *SqliteStore) insert(session *sessions.Session) error {
var createdOn time.Time
var modifiedOn time.Time
var expiresOn time.Time
crOn := session.Values["created_on"]
if crOn == nil {
createdOn = time.Now()
} else {
createdOn = crOn.(time.Time)
}
modifiedOn = createdOn
exOn := session.Values["expires_on"]
if exOn == nil {
expiresOn = time.Now().Add(time.Second * time.Duration(session.Options.MaxAge))
} else {
expiresOn = exOn.(time.Time)
}
delete(session.Values, "created_on")
delete(session.Values, "expires_on")
delete(session.Values, "modified_on")
encoded, encErr := securecookie.EncodeMulti(session.Name(), session.Values, m.Codecs...)
if encErr != nil {
return encErr
}
res, insErr := m.stmtInsert.Exec(encoded, createdOn, modifiedOn, expiresOn)
if insErr != nil {
return insErr
}
lastInserted, lInsErr := res.LastInsertId()
if lInsErr != nil {
return lInsErr
}
session.ID = fmt.Sprintf("%d", lastInserted)
return nil
}
func (m *SqliteStore) Delete(r *http.Request, w http.ResponseWriter, session *sessions.Session) error {
// Set cookie to expire.
options := *session.Options
options.MaxAge = -1
http.SetCookie(w, sessions.NewCookie(session.Name(), "", &options))
// Clear session values.
for k := range session.Values {
delete(session.Values, k)
}
_, delErr := m.stmtDelete.Exec(session.ID)
if delErr != nil {
return delErr
}
return nil
}
func (m *SqliteStore) save(session *sessions.Session) error {
if session.IsNew == true {
return m.insert(session)
}
var createdOn time.Time
var expiresOn time.Time
crOn := session.Values["created_on"]
if crOn == nil {
createdOn = time.Now()
} else {
createdOn = crOn.(time.Time)
}
exOn := session.Values["expires_on"]
if exOn == nil {
expiresOn = time.Now().Add(time.Second * time.Duration(session.Options.MaxAge))
log.Print("nil")
} else {
expiresOn = exOn.(time.Time)
if expiresOn.Sub(time.Now().Add(time.Second*time.Duration(session.Options.MaxAge))) < 0 {
expiresOn = time.Now().Add(time.Second * time.Duration(session.Options.MaxAge))
}
}
delete(session.Values, "created_on")
delete(session.Values, "expires_on")
delete(session.Values, "modified_on")
encoded, encErr := securecookie.EncodeMulti(session.Name(), session.Values, m.Codecs...)
if encErr != nil {
return encErr
}
_, updErr := m.stmtUpdate.Exec(encoded, createdOn, expiresOn, session.ID)
if updErr != nil {
return updErr
}
return nil
}
func (m *SqliteStore) load(session *sessions.Session) error {
row := m.stmtSelect.QueryRow(session.ID)
sess := sessionRow{}
scanErr := row.Scan(&sess.id, &sess.data, &sess.createdOn, &sess.modifiedOn, &sess.expiresOn)
if scanErr != nil {
return scanErr
}
if sess.expiresOn.Sub(time.Now()) < 0 {
log.Printf("Session expired on %s, but it is %s now.", sess.expiresOn, time.Now())
return errors.New("Session expired")
}
err := securecookie.DecodeMulti(session.Name(), sess.data, &session.Values, m.Codecs...)
if err != nil {
return err
}
session.Values["created_on"] = sess.createdOn
session.Values["modified_on"] = sess.modifiedOn
session.Values["expires_on"] = sess.expiresOn
return nil
}
|