From 3f6343127eb192ea157bdc4281d8d28d3833faed Mon Sep 17 00:00:00 2001 From: Wenhui Shen Date: Sat, 22 Jul 2017 12:18:03 +0800 Subject: [PATCH] update --- .vscode/temp.sql | 0 shared/consts.go | 1 - store/config.go | 9 --------- store/store.go | 21 ++++++++++++--------- 4 files changed, 12 insertions(+), 19 deletions(-) create mode 100644 .vscode/temp.sql diff --git a/.vscode/temp.sql b/.vscode/temp.sql new file mode 100644 index 0000000..e69de29 diff --git a/shared/consts.go b/shared/consts.go index 17c1b02..3c8f4a6 100644 --- a/shared/consts.go +++ b/shared/consts.go @@ -4,7 +4,6 @@ import "time" // Defaults for sessions.Options const ( - DefaultPath = "/" DefaultMaxAge = 60 * 60 * 24 * 30 // 30days ) diff --git a/store/config.go b/store/config.go index 044f3c6..37af62a 100644 --- a/store/config.go +++ b/store/config.go @@ -2,25 +2,16 @@ package store import ( "github.com/admpub/boltstore/shared" - "github.com/admpub/sessions" ) // Config represents a config for a session store. type Config struct { - // SessionOptions represents options for a session. - SessionOptions sessions.Options // DBOptions represents options for a database. DBOptions Options } // setDefault sets default to the config. func (c *Config) setDefault() { - if c.SessionOptions.Path == "" { - c.SessionOptions.Path = shared.DefaultPath - } - if c.SessionOptions.MaxAge == 0 { - c.SessionOptions.MaxAge = shared.DefaultMaxAge - } if c.DBOptions.BucketName == nil { c.DBOptions.BucketName = []byte(shared.DefaultBucketName) } diff --git a/store/store.go b/store/store.go index 9e42bda..6062682 100644 --- a/store/store.go +++ b/store/store.go @@ -9,9 +9,9 @@ import ( "github.com/gogo/protobuf/proto" "github.com/admpub/boltstore/shared" + "github.com/admpub/securecookie" "github.com/admpub/sessions" "github.com/boltdb/bolt" - "github.com/gorilla/securecookie" "github.com/webx-top/echo" ) @@ -35,10 +35,9 @@ func (s *Store) Get(ctx echo.Context, name string) (*sessions.Session, error) { func (s *Store) New(ctx echo.Context, name string) (*sessions.Session, error) { var err error session := sessions.NewSession(s, name) - session.Options = &s.config.SessionOptions session.IsNew = true if v := ctx.GetCookie(name); len(v) > 0 { - err = securecookie.DecodeMulti(name, v, &session.ID, s.codecs...) + err = securecookie.DecodeMultiWithMaxAge(name, v, &session.ID, ctx.CookieOptions().MaxAge, s.codecs...) if err == nil { ok, err := s.load(session) session.IsNew = !(err == nil && ok) // not new if no error and data available @@ -49,22 +48,22 @@ func (s *Store) New(ctx echo.Context, name string) (*sessions.Session, error) { // Save adds a single session to the response. func (s *Store) Save(ctx echo.Context, session *sessions.Session) error { - if session.Options.MaxAge < 0 { + if ctx.CookieOptions().MaxAge < 0 { s.delete(session) - sessions.SetCookie(ctx, session.Name(), "", session.Options) + sessions.SetCookie(ctx, session.Name(), "") } else { // Build an alphanumeric ID. if len(session.ID) == 0 { session.ID = strings.TrimRight(base32.StdEncoding.EncodeToString(securecookie.GenerateRandomKey(32)), "=") } - if err := s.save(session); err != nil { + if err := s.save(ctx, session); err != nil { return err } encoded, err := securecookie.EncodeMulti(session.Name(), session.ID, s.codecs...) if err != nil { return err } - sessions.SetCookie(ctx, session.Name(), encoded, session.Options) + sessions.SetCookie(ctx, session.Name(), encoded) } return nil } @@ -112,14 +111,18 @@ func (s *Store) delete(session *sessions.Session) error { } // save stores the session data in the database. -func (s *Store) save(session *sessions.Session) error { +func (s *Store) save(ctx echo.Context, session *sessions.Session) error { var buf bytes.Buffer enc := gob.NewEncoder(&buf) err := enc.Encode(session.Values) if err != nil { return err } - data, err := proto.Marshal(shared.NewSession(buf.Bytes(), session.Options.MaxAge)) + maxAge := ctx.CookieOptions().MaxAge + if maxAge == 0 { + maxAge = shared.DefaultMaxAge + } + data, err := proto.Marshal(shared.NewSession(buf.Bytes(), maxAge)) if err != nil { return err }