forked from GoIncremental/negroni-sessions
/
dal_store.go
183 lines (159 loc) · 4.43 KB
/
dal_store.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
package sessions
import (
"net/http"
"time"
"github.com/goincremental/dal"
"github.com/gorilla/securecookie"
"github.com/gorilla/sessions"
)
// NewDalStore is a factory function that returns a store object using the provided dal.Connection
func NewDalStore(connection dal.Connection, database string, collection string, maxAge int, ensureTTL bool, keyPairs ...[]byte) Store {
if ensureTTL {
conn := connection.Clone()
defer conn.Close()
db := conn.DB(database)
c := db.C(collection)
c.EnsureIndex(dal.Index{
Key: []string{"modified"},
Background: true,
Sparse: true,
ExpireAfter: time.Duration(maxAge) * time.Second,
})
}
return &dalStore{
Codecs: securecookie.CodecsFromPairs(keyPairs...),
Token: &cookieToken{},
connection: connection,
database: database,
collection: collection,
options: &sessions.Options{
MaxAge: maxAge,
},
}
}
func (d *dalStore) Options(options Options) {
d.options = &sessions.Options{
Path: options.Path,
Domain: options.Domain,
MaxAge: options.MaxAge,
Secure: options.Secure,
HttpOnly: options.HTTPOnly,
}
}
type dalSession struct {
ID dal.ObjectID `bson:"_id,omitempty"`
Data string
Modified time.Time
}
type dalStore struct {
Codecs []securecookie.Codec
Token tokenGetSeter
connection dal.Connection
database string
collection string
options *sessions.Options
}
//Implementation of gorilla/sessions.Store interface
// Get registers and returns a session for the given name and session store.
// It returns a new session if there are no sessions registered for the name.
func (d *dalStore) Get(r *http.Request, name string) (*sessions.Session, error) {
return sessions.GetRegistry(r).Get(d, name)
}
// New returns a session for the given name without adding it to the registry.
func (d *dalStore) New(r *http.Request, name string) (*sessions.Session, error) {
var err error
session := sessions.NewSession(d, name)
options := *d.options
session.Options = &options
session.IsNew = true
if cook, errToken := d.Token.getToken(r, name); errToken == nil {
err = securecookie.DecodeMulti(name, cook, &session.ID, d.Codecs...)
if err == nil {
ok, err := d.load(session)
session.IsNew = !(err == nil && ok) // not new if no error and data available
}
}
return session, err
}
func (d *dalStore) Save(r *http.Request, w http.ResponseWriter, session *sessions.Session) error {
if session.Options.MaxAge < 0 {
if err := d.delete(session); err != nil {
return err
}
d.Token.setToken(w, session.Name(), "", session.Options)
return nil
}
if session.ID == "" {
session.ID = dal.NewObjectID().Hex()
}
if err := d.save(session); err != nil {
return err
}
//save just the id to the cookie, the rest will be saved in the dal store
encoded, err := securecookie.EncodeMulti(session.Name(), session.ID, d.Codecs...)
if err != nil {
return err
}
d.Token.setToken(w, session.Name(), encoded, session.Options)
return err
}
func (d *dalStore) load(session *sessions.Session) (bool, error) {
if !dal.IsObjectIDHex(session.ID) {
return false, ErrInvalidId
}
conn := d.connection.Clone()
defer conn.Close()
db := conn.DB(d.database)
c := db.C(d.collection)
s := dalSession{}
err := c.FindID(dal.ObjectIDHex(session.ID)).One(&s)
if err != nil {
return false, err
}
if err := securecookie.DecodeMulti(session.Name(), s.Data, &session.Values, d.Codecs...); err != nil {
return false, err
}
return true, nil
}
func (d *dalStore) save(session *sessions.Session) error {
if !dal.IsObjectIDHex(session.ID) {
return ErrInvalidId
}
conn := d.connection.Clone()
defer conn.Close()
db := conn.DB(d.database)
c := db.C(d.collection)
var modified time.Time
if val, ok := session.Values["modified"]; ok {
modified, ok = val.(time.Time)
if !ok {
return ErrInvalidModified
}
} else {
modified = time.Now()
}
encoded, err := securecookie.EncodeMulti(session.Name(), session.Values, d.Codecs...)
if err != nil {
return err
}
s := dalSession{
ID: dal.ObjectIDHex(session.ID),
Data: encoded,
Modified: modified,
}
_, err = c.UpsertID(dal.ObjectIDHex(session.ID), &s)
if err != nil {
return err
}
return nil
}
func (d *dalStore) delete(session *sessions.Session) error {
if !dal.IsObjectIDHex(session.ID) {
return ErrInvalidId
}
conn := d.connection.Clone()
defer conn.Close()
db := conn.DB(d.database)
c := db.C(d.collection)
return c.RemoveID(dal.ObjectIDHex(session.ID))
}