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 go 1.14
require ( require (
github.com/davecgh/go-spew v1.1.1
github.com/gorilla/securecookie v1.1.1 github.com/gorilla/securecookie v1.1.1
github.com/gorilla/sessions v1.2.0 github.com/gorilla/sessions v1.2.0
go.mongodb.org/mongo-driver v1.3.1 go.mongodb.org/mongo-driver v1.3.1

View File

@ -50,28 +50,19 @@ func (s *MongoStore) MaxAge(age int) {
func (s *MongoStore) New(r *http.Request, name string) (*sessions.Session, error) { func (s *MongoStore) New(r *http.Request, name string) (*sessions.Session, error) {
session := sessions.NewSession(s, name) session := sessions.NewSession(s, name)
session.Options = &sessions.Options{ opts := *s.Options
Path: s.Options.Path, session.Options = &opts
MaxAge: s.Options.MaxAge,
Domain: s.Options.Domain,
Secure: s.Options.Secure,
HttpOnly: s.Options.HttpOnly,
}
session.IsNew = true session.IsNew = true
cookie, e := r.Cookie(name) cookie, e := r.Cookie(name)
if e != nil { if e == nil {
return session, e
}
e = securecookie.DecodeMulti(name, cookie.Value, &session.ID, s.Codecs...) e = securecookie.DecodeMulti(name, cookie.Value, &session.ID, s.Codecs...)
if e != nil { if e == nil {
return session, e
}
e = s.load(session) e = s.load(session)
if e != nil { if e == nil {
return session, e
} else {
session.IsNew = false session.IsNew = false
} }
}
}
return session, nil 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 { 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 { if e := s.remove(session); e != nil {
return e return e
} }

View File

@ -44,14 +44,14 @@ func TestFlashes(t *testing.T) {
var session *sessions.Session var session *sessions.Session
var flashes []interface{} 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")) client, err := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://localhost:27017"))
err = client.Ping(ctx, readpref.Primary()) err = client.Ping(ctx, readpref.Primary())
if err != nil { if err != nil {
panic(err) 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.Path = "/"
store.Options.MaxAge = 86400 * 30 * 365 store.Options.MaxAge = 86400 * 30 * 365
defer store.Close() 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() { func init() {
gob.Register(FlashMessage{}) gob.Register(FlashMessage{})
} }