diff options
author | Wenhui Shen <swhbox@foxmail.com> | 2017-07-22 07:28:45 +0300 |
---|---|---|
committer | Wenhui Shen <swhbox@foxmail.com> | 2017-07-22 07:28:45 +0300 |
commit | dc272174175f7f9647ee7184d4eba13ee98859a1 (patch) | |
tree | c5412da696ee17355c8b763e714efd55a089fd5c | |
parent | 33a965795ae743ec3a0ea7f083780110be5b62ea (diff) |
update
-rw-r--r-- | sessions.go | 39 | ||||
-rw-r--r-- | store.go | 60 |
2 files changed, 31 insertions, 68 deletions
diff --git a/sessions.go b/sessions.go index 115f3ab..2d19c8a 100644 --- a/sessions.go +++ b/sessions.go @@ -16,22 +16,6 @@ import ( // Default flashes key. const flashesKey = "_flash" -// Options -------------------------------------------------------------------- - -// Options stores configuration for a session or session store. -// -// Fields are a subset of http.Cookie fields. -type Options struct { - Path string - Domain string - // MaxAge=0 means no 'Max-Age' attribute specified. - // MaxAge<0 means delete cookie now, equivalently 'Max-Age: 0'. - // MaxAge>0 means Max-Age attribute present and given in seconds. - MaxAge int - Secure bool - HttpOnly bool -} - // Session -------------------------------------------------------------------- // NewSession is called by session stores to create a new session instance. @@ -49,11 +33,10 @@ type Session struct { // user data. ID string // Values contains the user-data for the session. - Values map[interface{}]interface{} - Options *Options - IsNew bool - store Store - name string + Values map[interface{}]interface{} + IsNew bool + store Store + name string } // Flashes returns a slice of flash messages from the session. @@ -189,7 +172,8 @@ func Save(ctx echo.Context) error { // NewCookie returns an http.Cookie with the options set. It also sets // the Expires field calculated based on the MaxAge value, for Internet // Explorer compatibility. -func NewCookie(name, value string, options *Options) *http.Cookie { +func NewCookie(ctx echo.Context, name, value string) *http.Cookie { + options := ctx.CookieOptions() cookie := &http.Cookie{ Name: name, Value: value, @@ -210,15 +194,8 @@ func NewCookie(name, value string, options *Options) *http.Cookie { } // SetCookie for echo -func SetCookie(ctx echo.Context, key string, value string, options *Options) { - ctx.SetCookie( - key, value, - options.MaxAge, - options.Path, - options.Domain, - options.Secure, - options.HttpOnly, - ) +func SetCookie(ctx echo.Context, key string, value string) { + ctx.SetCookie(key, value) } // Error ---------------------------------------------------------------------- @@ -12,7 +12,7 @@ import ( "strings" "sync" - "github.com/gorilla/securecookie" + "github.com/admpub/securecookie" "github.com/webx-top/echo" ) @@ -53,20 +53,13 @@ type Store interface { func NewCookieStore(keyPairs ...[]byte) *CookieStore { cs := &CookieStore{ Codecs: securecookie.CodecsFromPairs(keyPairs...), - Options: &Options{ - Path: "/", - MaxAge: 86400 * 30, - }, } - - cs.MaxAge(cs.Options.MaxAge) return cs } // CookieStore stores sessions using secure cookies. type CookieStore struct { - Codecs []securecookie.Codec - Options *Options // default configuration + Codecs []securecookie.Codec } // Get returns a session for the given name after adding it to the registry. @@ -87,12 +80,12 @@ func (s *CookieStore) Get(ctx echo.Context, name string) (*Session, error) { // decoded session after the first call. func (s *CookieStore) New(ctx echo.Context, name string) (*Session, error) { session := NewSession(s, name) - opts := *s.Options - session.Options = &opts session.IsNew = true var err error if v := ctx.GetCookie(name); len(v) > 0 { - err = securecookie.DecodeMulti(name, v, &session.Values, + err = securecookie.DecodeMultiWithMaxAge( + name, v, &session.Values, + ctx.CookieOptions().MaxAge, s.Codecs...) if err == nil { session.IsNew = false @@ -108,7 +101,7 @@ func (s *CookieStore) Save(ctx echo.Context, session *Session) error { if err != nil { return err } - SetCookie(ctx, session.Name(), encoded, session.Options) + SetCookie(ctx, session.Name(), encoded) return nil } @@ -116,8 +109,6 @@ func (s *CookieStore) Save(ctx echo.Context, session *Session) error { // implementation. Individual sessions can be deleted by setting Options.MaxAge // = -1 for that session. func (s *CookieStore) MaxAge(age int) { - s.Options.MaxAge = age - // Set the maxAge for each securecookie instance. for _, codec := range s.Codecs { if sc, ok := codec.(*securecookie.SecureCookie); ok { @@ -142,14 +133,8 @@ func NewFilesystemStore(path string, keyPairs ...[]byte) *FilesystemStore { } fs := &FilesystemStore{ Codecs: securecookie.CodecsFromPairs(keyPairs...), - Options: &Options{ - Path: "/", - MaxAge: 86400 * 30, - }, - path: path, + path: path, } - - fs.MaxAge(fs.Options.MaxAge) return fs } @@ -159,9 +144,8 @@ func NewFilesystemStore(path string, keyPairs ...[]byte) *FilesystemStore { // // This store is still experimental and not well tested. Feedback is welcome. type FilesystemStore struct { - Codecs []securecookie.Codec - Options *Options // default configuration - path string + Codecs []securecookie.Codec + path string } // MaxLength restricts the maximum length of new sessions to l. @@ -187,14 +171,15 @@ func (s *FilesystemStore) Get(ctx echo.Context, name string) (*Session, error) { // See CookieStore.New(). func (s *FilesystemStore) New(ctx echo.Context, name string) (*Session, error) { session := NewSession(s, name) - opts := *s.Options - session.Options = &opts session.IsNew = true var err error 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 { - err = s.load(session) + err = s.load(ctx, session) if err == nil { session.IsNew = false } @@ -207,11 +192,11 @@ func (s *FilesystemStore) New(ctx echo.Context, name string) (*Session, error) { func (s *FilesystemStore) Save(ctx echo.Context, session *Session) error { // Delete if max-age is <= 0 - if session.Options.MaxAge <= 0 { + if ctx.CookieOptions().MaxAge <= 0 { if err := s.erase(session); err != nil { return err } - SetCookie(ctx, session.Name(), "", session.Options) + SetCookie(ctx, session.Name(), "") return nil } if len(session.ID) == 0 { @@ -229,7 +214,7 @@ func (s *FilesystemStore) Save(ctx echo.Context, if err != nil { return err } - SetCookie(ctx, session.Name(), encoded, session.Options) + SetCookie(ctx, session.Name(), encoded) return nil } @@ -247,8 +232,6 @@ func (s *FilesystemStore) erase(session *Session) error { // implementation. Individual sessions can be deleted by setting Options.MaxAge // = -1 for that session. func (s *FilesystemStore) MaxAge(age int) { - s.Options.MaxAge = age - // Set the maxAge for each securecookie instance. for _, codec := range s.Codecs { if sc, ok := codec.(*securecookie.SecureCookie); ok { @@ -271,7 +254,7 @@ func (s *FilesystemStore) save(session *Session) error { } // load reads a file and decodes its content into session.Values. -func (s *FilesystemStore) load(session *Session) error { +func (s *FilesystemStore) load(ctx echo.Context, session *Session) error { filename := filepath.Join(s.path, "session_"+session.ID) fileMutex.RLock() defer fileMutex.RUnlock() @@ -279,8 +262,11 @@ func (s *FilesystemStore) load(session *Session) error { if err != nil { return err } - if err = securecookie.DecodeMulti(session.Name(), string(fdata), - &session.Values, s.Codecs...); err != nil { + if err = securecookie.DecodeMultiWithMaxAge( + session.Name(), string(fdata), + &session.Values, + ctx.CookieOptions().MaxAge, + s.Codecs...); err != nil { return err } return nil |