forked from cert-lv/graphoscope
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongostore.go
378 lines (323 loc) · 10.5 KB
/
mongostore.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
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/*
* Fork of the original Mongostore at:
* https://github.com/go-stuff/mongostore/blob/master/mongostore.go
*
* Changes of the current version:
* - create a new session even if can't decode existing one
* - some structures merged
* - don't print debugging info to the stderr, use logger instead
*
* Don't use "log.SetOutput(ioutil.Discard)" to hide printing
* to the stderr, because it also hides panic errors in other places
*/
package main
import (
"context"
"errors"
"fmt"
"net/http"
"time"
"github.com/gorilla/securecookie"
gsessions "github.com/gorilla/sessions"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/x/bsonx"
)
// MongoSession is how sessions are stored in MongoDB.
type MongoSession struct {
ID primitive.ObjectID `bson:"_id,omitempty"`
Data primitive.M `bson:"data,omitempty"`
Modified primitive.DateTime `bson:"modified_at,omitempty"`
Expires primitive.DateTime `bson:"expires_at,omitempty"`
TTL primitive.DateTime `bson:"ttl,omitemtpy"`
}
// Store stores sessions in Secure Cookies and MongoDB.
type MongoStore struct {
defaultCookie http.Cookie // default cookie settings
gsessions.CookieStore
Context context.Context
Collection *mongo.Collection
}
// NewStore uses cookies and mongo to store sessions.
//
// Keys are defined in pairs to allow key rotation, but the common case is
// to set a single authentication key and optionally an encryption key.
//
// The first key in a pair is used for authentication and the second for
// encryption. The encryption key can be set to nil or omitted in the last
// pair, but the authentication key is required in all pairs.
//
// It is recommended to use an authentication key with 32 or 64 bytes.
// The encryption key, if set, must be either 16, 24, or 32 bytes to select
// AES-128, AES-192, or AES-256 modes.
func NewMongoStore(col *mongo.Collection, cookie http.Cookie, keyPairs ...[]byte) (*MongoStore, error) {
s := &MongoStore{
defaultCookie: cookie,
CookieStore: gsessions.CookieStore{
Codecs: securecookie.CodecsFromPairs(keyPairs...),
Options: &gsessions.Options{
Path: cookie.Path,
Domain: cookie.Domain,
MaxAge: cookie.MaxAge,
Secure: cookie.Secure,
HttpOnly: cookie.HttpOnly,
SameSite: cookie.SameSite,
},
},
Context: context.Background(),
Collection: col,
}
// add TTL index if it does not exist
err := s.insertTTL()
if err != nil {
return nil, fmt.Errorf("MongoStore: can't add time to live index: %v", err)
}
return s, nil
}
// Get returns a session for the given name after adding it to the registry.
//
// It returns a new session if the sessions doesn't exist. Access IsNew on
// the session to check if it is an existing session or a new one.
//
// It returns a new session and an error if the session exists but could
// not be decoded.
func (s *MongoStore) Get(r *http.Request, name string) (*gsessions.Session, error) {
return gsessions.GetRegistry(r).Get(s, name)
}
// New returns a session for the given name without adding it to the registry.
//
// The difference between New() and Get() is that calling New() twice will
// decode the session data twice, while Get() registers and reuses the same
// decoded session after the first call.
func (s *MongoStore) New(r *http.Request, name string) (*gsessions.Session, error) {
session := gsessions.NewSession(s, name)
session.Options = s.CookieStore.Options
session.Options.MaxAge = s.defaultCookie.MaxAge
session.IsNew = true
// get session cookie
c, err := r.Cookie(name)
// no cookie
if errors.Is(err, http.ErrNoCookie) {
log.Debug().Msgf("MongoStore: no cookie: %s", err.Error())
return session, nil
}
// decode the session.ID in the cookie and use it to find the existing session in mongo
err = securecookie.DecodeMulti(name, c.Value, &session.ID, s.CookieStore.Codecs...)
if err != nil {
return session, fmt.Errorf("MongoStore: can't decode cookie: %w", err)
}
// if the session does not exist in mongo, expire the cookies and mark the session as new
err = s.findOne(session)
if errors.Is(err, mongo.ErrNoDocuments) {
log.Debug().Msgf("MongoStore: no session: %s", err.Error())
return session, nil
}
// flag as an existing session
session.IsNew = false
return session, nil
}
// Save adds a single session to the response.
func (s *MongoStore) Save(r *http.Request, w http.ResponseWriter, session *gsessions.Session) error {
// expired session
if session.Options.MaxAge == -1 {
res, err := s.deleteOne(session)
if err != nil {
return fmt.Errorf("MongoStore: can't delete session: %v", err)
}
log.Debug().Msgf("MongoStore: %d session(s) deleted", res.DeletedCount)
}
// new session
if session.IsNew && session.Options.MaxAge != -1 {
res, err := s.insertOne(session)
if err != nil {
return fmt.Errorf("MongoStore: can't insert session: %v", err)
}
log.Debug().Msgf("MongoStore: session id: %s, inserted", res.InsertedID.(primitive.ObjectID).Hex())
session.ID = res.InsertedID.(primitive.ObjectID).Hex()
}
// existing session
if !session.IsNew && session.Options.MaxAge != -1 {
res, err := s.updateOne(session)
if err != nil {
return fmt.Errorf("MongoStore: can't update session: %v", err)
}
log.Debug().Msgf("MongoStore: %d session(s) updated", res.ModifiedCount)
}
// encode the cookie with only the session.ID, session.Values are never encoded with
// to the cookie (client side) they are only stored in mongo (server side)
encoded, err := securecookie.EncodeMulti(session.Name(), session.ID, s.CookieStore.Codecs...)
if err != nil {
return fmt.Errorf("MongoStore: can't save cookie: %v", err)
}
// update the cookie
http.SetCookie(w, gsessions.NewCookie(session.Name(), encoded, s.CookieStore.Options))
return nil
}
func (s *MongoStore) insertTTL() error {
var foundTTLIndex bool
// get indexes from mongo into the cursor
cursor, err := s.Collection.Indexes().List(s.Context)
if err != nil {
return err
}
// use the cursor to iterate each index
for cursor.Next(s.Context) {
// decode the current index
var index bson.D
err := cursor.Decode(&index)
if err != nil {
return err
}
// is the index empty
if len(index) > 0 {
// does index contain a key
key := index.Map()["key"]
if key != nil {
// does the key contain ttl
if key.(bson.D).Map()["ttl"] != nil {
foundTTLIndex = true
}
}
}
}
// https://docs.mongodb.com/manual/core/index-ttl/
//
// TTL indexes are special single-field indexes that MongoDB can use to automatically
// remove documents from a collection after a certain amount of time or at a specific
// clock time. Data expiration is useful for certain types of information like machine
// generated event data, logs, and session information that only need to persist in a
// database for a finite amount of time.
//
// To create a TTL index, use the db.collection.createIndex() method with the
// expireAfterSeconds option on a field whose value is either a date or an array that
// contains date values.
//
// TTL indexes expire documents after the specified number of seconds has passed since
// the indexed field value; i.e. the expiration threshold is the indexed field value
// plus the specified number of seconds.
//
// The _id field does not support TTL indexes.
if !foundTTLIndex {
_, err = s.Collection.Indexes().CreateOne(
s.Context,
mongo.IndexModel{
Keys: bsonx.Doc{
bsonx.Elem{
Key: "ttl",
Value: bsonx.Int32(1),
},
},
Options: options.Index().
SetSparse(true).
SetExpireAfterSeconds(int32(s.defaultCookie.MaxAge)),
},
)
if err != nil {
return err
}
}
return nil
}
func (s *MongoStore) findOne(session *gsessions.Session) error {
// get the mongo _id from the cookie
oid, err := primitive.ObjectIDFromHex(session.ID)
if err != nil {
return err
}
// initialize an empty struct for FindOne to fill
mongoSession := &MongoSession{}
// find the session in mongo using the _id and put the result in the empty struct
err = s.Collection.FindOne(
s.Context,
bson.M{
"_id": oid,
},
).Decode(mongoSession)
// no session found
if errors.Is(err, mongo.ErrNoDocuments) {
return fmt.Errorf("MongoStore: no session found: %w", err)
}
// something went wrong with the mongo search
if err != nil {
return fmt.Errorf("MongoStore: can't find session: %w", err)
}
// fill session.Values from mongo
for k, v := range mongoSession.Data {
session.Values[k] = v
}
return nil
}
func (s *MongoStore) insertOne(session *gsessions.Session) (*mongo.InsertOneResult, error) {
// initialize a mongo session to insert
mongoSession := &MongoSession{
Data: make(map[string]interface{}, len(session.Values)),
Modified: primitive.NewDateTimeFromTime(time.Now()),
Expires: primitive.NewDateTimeFromTime(time.Now().Add(time.Duration(s.defaultCookie.MaxAge) * time.Second)),
TTL: primitive.NewDateTimeFromTime(time.Now()),
}
// get current session.Values
for k, v := range session.Values {
mongoSession.Data[k.(string)] = v
}
// insert the mongo session
res, err := s.Collection.InsertOne(
s.Context,
mongoSession,
)
if err != nil {
return nil, err
}
return res, nil
}
func (s *MongoStore) updateOne(session *gsessions.Session) (*mongo.UpdateResult, error) {
// get the mongo _id from the cookie
oid, err := primitive.ObjectIDFromHex(session.ID)
if err != nil {
return nil, err
}
// initialize a mongo session to insert
mongoSession := &MongoSession{
Data: make(map[string]interface{}, len(session.Values)),
Modified: primitive.NewDateTimeFromTime(time.Now()),
Expires: primitive.NewDateTimeFromTime(time.Now().Add(time.Duration(s.defaultCookie.MaxAge) * time.Second)),
TTL: primitive.NewDateTimeFromTime(time.Now()),
}
// get current session.Values
for k, v := range session.Values {
mongoSession.Data[k.(string)] = v
}
// update session.Values in mongo usig the object id
res, err := s.Collection.UpdateOne(
s.Context,
bson.M{
"_id": oid,
},
bson.M{
"$set": mongoSession,
},
)
if err != nil {
return nil, err
}
return res, nil
}
func (s *MongoStore) deleteOne(session *gsessions.Session) (*mongo.DeleteResult, error) {
// convert session id to a mongo object id
oid, err := primitive.ObjectIDFromHex(session.ID)
if err != nil {
return nil, err
}
// delete session using the object id
res, err := s.Collection.DeleteOne(
s.Context,
bson.M{
"_id": oid,
},
)
if err != nil {
return nil, err
}
return res, nil
}