fixed new logic

This commit is contained in:
Darko Luketic 2020-04-02 21:32:02 +02:00
parent 78b84a1fa0
commit 1022c2d36c
3 changed files with 14 additions and 58 deletions

1
go.mod
View File

@ -3,6 +3,7 @@ module github.com/dalu/mongostore
go 1.14
require (
github.com/davecgh/go-spew v1.1.1
github.com/gorilla/securecookie v1.1.1
github.com/gorilla/sessions v1.2.0
go.mongodb.org/mongo-driver v1.3.1

View File

@ -50,27 +50,18 @@ func (s *MongoStore) MaxAge(age int) {
func (s *MongoStore) New(r *http.Request, name string) (*sessions.Session, error) {
session := sessions.NewSession(s, name)
session.Options = &sessions.Options{
Path: s.Options.Path,
MaxAge: s.Options.MaxAge,
Domain: s.Options.Domain,
Secure: s.Options.Secure,
HttpOnly: s.Options.HttpOnly,
}
opts := *s.Options
session.Options = &opts
session.IsNew = true
cookie, e := r.Cookie(name)
if e != nil {
return session, e
}
e = securecookie.DecodeMulti(name, cookie.Value, &session.ID, s.Codecs...)
if e != nil {
return session, e
}
e = s.load(session)
if e != nil {
return session, e
} else {
session.IsNew = false
if e == nil {
e = securecookie.DecodeMulti(name, cookie.Value, &session.ID, s.Codecs...)
if e == nil {
e = s.load(session)
if e == nil {
session.IsNew = false
}
}
}
return session, nil
}
@ -80,7 +71,7 @@ func (s *MongoStore) Get(r *http.Request, name string) (*sessions.Session, error
}
func (s *MongoStore) Save(r *http.Request, w http.ResponseWriter, session *sessions.Session) error {
if session.Options.MaxAge < 0 {
if session.Options.MaxAge <= 0 {
if e := s.remove(session); e != nil {
return e
}

View File

@ -44,14 +44,14 @@ func TestFlashes(t *testing.T) {
var session *sessions.Session
var flashes []interface{}
ctx, _ := context.WithTimeout(context.Background(), 3*time.Second)
ctx := context.Background()
client, err := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://localhost:27017"))
err = client.Ping(ctx, readpref.Primary())
if err != nil {
panic(err)
}
store := mongostore.NewMongoStore(client, "test", "sessions", 3*time.Second)
store := mongostore.NewMongoStore(client, "test", "sessions", 3*time.Second, []byte("hellol-worlol"))
store.Options.Path = "/"
store.Options.MaxAge = 86400 * 30 * 365
defer store.Close()
@ -199,42 +199,6 @@ func TestFlashes(t *testing.T) {
}
}
func TestCookieStoreMapPanic(t *testing.T) {
defer func() {
err := recover()
if err != nil {
t.Fatal(err)
}
}()
ctx, _ := context.WithTimeout(context.Background(), 3*time.Second)
client, err := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://localhost:27017"))
err = client.Ping(ctx, readpref.Primary())
if err != nil {
panic(err)
}
store := mongostore.NewMongoStore(client, "test", "sessions", 3*time.Second, []byte("aaa0defe5d2839cbc46fc4f080cd7adc"))
store.Options.Path = "/"
store.Options.MaxAge = 86400 * 30 * 365
defer store.Close()
req, err := http.NewRequest("GET", "http://www.example.com", nil)
if err != nil {
t.Fatal("failed to create request", err)
}
w := httptest.NewRecorder()
session := sessions.NewSession(store, "hello")
session.Values["data"] = "hello-world"
err = session.Save(req, w)
if err != nil {
t.Fatal("failed to save session", err)
}
}
func init() {
gob.Register(FlashMessage{})
}