mongostore/store.go

90 lines
2.2 KiB
Go
Raw Normal View History

2020-03-22 01:01:55 +01:00
package mongostore
import (
"context"
2020-04-02 18:42:27 +02:00
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
2020-03-22 01:01:55 +01:00
"net/http"
"time"
"github.com/gorilla/securecookie"
"github.com/gorilla/sessions"
"go.mongodb.org/mongo-driver/mongo"
)
type MongoStore struct {
client *mongo.Client
Codecs []securecookie.Codec
Options *sessions.Options
2020-04-02 18:42:27 +02:00
dbDatabaseName string
dbCollectionName string
dbTimeout time.Duration
2020-03-22 01:01:55 +01:00
}
2020-04-02 18:42:27 +02:00
func NewMongoStore(client *mongo.Client, dbDatabase, dbCollection string, dbTimeout time.Duration) *MongoStore {
2020-03-22 01:01:55 +01:00
s := new(MongoStore)
s.client = client
2020-04-02 18:42:27 +02:00
s.dbDatabaseName = dbDatabase
s.dbCollectionName = dbCollection
s.dbTimeout = dbTimeout
2020-03-22 01:01:55 +01:00
return s
}
func (s *MongoStore) New(r *http.Request, name string) (*sessions.Session, error) {
2020-04-02 18:42:27 +02:00
session := sessions.NewSession(s, name)
options := *s.Options
session.Options = &options
session.IsNew = true
cookie, e := r.Cookie(name)
if e != nil {
return nil, e
}
e = securecookie.DecodeMulti(name, cookie.Value, &session.ID, s.Codecs...)
if e != nil {
return nil, e
}
e = s.load(session)
if e != nil {
return nil, e
} else {
session.IsNew = false
}
return session, nil
}
func (s *MongoStore) Get(r *http.Request, name string) (*sessions.Session, error) {
return sessions.GetRegistry(r).Get(s, name)
2020-03-22 01:01:55 +01:00
}
func (s *MongoStore) Save(r *http.Request, w http.ResponseWriter, session *sessions.Session) error {
panic("not implemented")
}
func (s *MongoStore) Delete(r *http.Request, w http.ResponseWriter, session *sessions.Session) error {
panic("not implemented")
}
func (s *MongoStore) Close() error {
ctx, _ := context.WithTimeout(context.Background(), 3*time.Second)
return s.client.Disconnect(ctx)
}
2020-04-02 18:42:27 +02:00
func (s *MongoStore) load(session *sessions.Session) error {
oid, e := primitive.ObjectIDFromHex(session.ID)
if e != nil {
return e
}
collection := s.client.Database(s.dbDatabaseName).Collection(s.dbCollectionName)
ctx, _ := context.WithTimeout(context.Background(), s.dbTimeout)
result := collection.FindOne(ctx, bson.M{"_id": oid})
ns := new(Session)
if e := result.Decode(ns); e != nil {
return e
}
if e := securecookie.DecodeMulti(session.Name(), string(ns.Data), &session.Values, s.Codecs...); e != nil {
return e
}
return nil
}