-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrethinkstore.go
204 lines (176 loc) · 5.66 KB
/
rethinkstore.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
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
// Copyright 2015 Brian "bojo" Jones. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package rethinkstore
import (
"bytes"
"encoding/base32"
"encoding/gob"
"errors"
"net/http"
"strings"
"time"
r "github.com/dancannon/gorethink"
"github.com/gorilla/securecookie"
"github.com/gorilla/sessions"
)
var ErrNoDatabase = errors.New("no databases available")
// Amount of time for keys to expire.
var sessionExpire = 86400 * 30
type RethinkSession struct {
Id string `gorethink:"id"`
Expires time.Time `gorethink:"expires"`
Session []byte `gorethink:"session"`
}
// RethinkStore stores sessions in a rethinkdb backend.
type RethinkStore struct {
Rethink *r.Session // rethink session
Table string // table to store sessions in
Codecs []securecookie.Codec // session codecs
Options *sessions.Options // default configuration
DefaultMaxAge int // default TTL for a MaxAge == 0 session
}
// NewRethinkStore returns a new RethinkStore.
//
// Takes in the database address, database name, session table,
// max idle connections, max open connections, and session key pairs.
func NewRethinkStore(addr, db, table string, idle, open int, keyPairs ...[]byte) (*RethinkStore, error) {
session, err := r.Connect(r.ConnectOpts{
Address: addr,
Database: db,
MaxIdle: idle,
MaxOpen: open,
})
if err != nil {
return nil, err
}
rs := &RethinkStore{
Rethink: session,
Table: table,
Codecs: securecookie.CodecsFromPairs(keyPairs...),
Options: &sessions.Options{
Path: "/",
MaxAge: sessionExpire,
},
}
rs.MaxAge(sessionExpire)
// Create missing db, table and secondary index. Discard error (database exists)
r.DBCreate(db).RunWrite(session)
r.DB(db).TableCreate(table).RunWrite(session)
// Index for removing expired data
r.Table(table).IndexCreate("expires").Exec(session)
r.Table(table).IndexWait().RunWrite(session)
return rs, nil
}
// Close closes the underlying Rethink Client.
func (s *RethinkStore) Close() {
s.Rethink.Close()
}
// Get returns a session for the given name after adding it to the registry.
func (s *RethinkStore) Get(r *http.Request, name string) (*sessions.Session, error) {
return sessions.GetRegistry(r).Get(s, name)
}
// New returns a session for the given name without adding it to the registry.
func (s *RethinkStore) New(r *http.Request, name string) (*sessions.Session, error) {
var err error
session := sessions.NewSession(s, name)
session.Options = &(*s.Options)
session.IsNew = true
if c, errCookie := r.Cookie(name); errCookie == nil {
err = securecookie.DecodeMulti(name, c.Value, &session.ID, s.Codecs...)
if err == nil {
ok, err := s.load(session)
session.IsNew = !(err == nil && ok) // not new if no error and data available
}
}
return session, err
}
// Save adds a single session to the response.
func (s *RethinkStore) Save(r *http.Request, w http.ResponseWriter, session *sessions.Session) error {
// Marked for deletion.
if session.Options.MaxAge < 0 {
http.SetCookie(w, sessions.NewCookie(session.Name(), "", session.Options))
} else {
// Build an alphanumeric key for the redis store.
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
}
http.SetCookie(w, sessions.NewCookie(session.Name(), encoded, session.Options))
}
return nil
}
// MaxAge sets the maximum age for the store and the underlying cookie
// implementation. Individual sessions can be deleted by setting Options.MaxAge
// = -1 for that session.
func (s *RethinkStore) MaxAge(age int) {
s.Options.MaxAge = age
// Set the maxAge for each securecookie instance.
for _, codec := range s.Codecs {
if sc, ok := codec.(*securecookie.SecureCookie); ok {
sc.MaxAge(age)
}
}
}
// save stores the session in rethink.
func (s *RethinkStore) save(session *sessions.Session) error {
buf := new(bytes.Buffer)
enc := gob.NewEncoder(buf)
err := enc.Encode(session.Values)
if err != nil {
return err
}
age := session.Options.MaxAge
if age == 0 {
age = s.DefaultMaxAge
}
expires := time.Now().Add(time.Duration(age) * time.Second)
_, err = r.Table(s.Table).Get(session.ID).Replace(RethinkSession{Id: session.ID, Expires: expires, Session: buf.Bytes()}).Run(s.Rethink)
return err
}
// load reads the session from rethink.
// returns true if there is session data in the DB.
func (s *RethinkStore) load(session *sessions.Session) (bool, error) {
var data RethinkSession
res, err := r.Table(s.Table).Get(session.ID).Run(s.Rethink)
if err != nil {
return false, err
}
if err := res.One(&data); err != nil {
return false, err
}
dec := gob.NewDecoder(bytes.NewBuffer(data.Session))
return true, dec.Decode(&session.Values)
}
// delete removes keys from rethink
func (s *RethinkStore) delete(session *sessions.Session) error {
_, err := r.Table(s.Table).Get(session.ID).Delete().Run(s.Rethink)
return err
}
// Deletes expired entries
func (s *RethinkStore) DeleteExpired() error {
_, err := r.Table(s.Table).Between(r.MinVal, r.Now(), r.BetweenOpts{Index: "expires"}).Delete().Run(s.Rethink)
return err
}
func (s *RethinkStore) Count() (uint, error) {
var result interface{}
cursor, err := r.Table(s.Table).Count().Run(s.Rethink)
if err != nil {
return 0, err
}
err = cursor.One(&result)
if err != nil {
return 0, err
}
count, ok := result.(float64)
if !ok {
return 0, errors.New("count isn't float64")
}
return uint(count), nil
}