From ab250e0cde98b2d4030f0ac1879e7394c2e416a0 Mon Sep 17 00:00:00 2001 From: Matt Silverlock Date: Tue, 11 Aug 2015 20:01:05 +0800 Subject: Provides functionality to set the MaxAge on the underlying securecookie Codecs. - Addresses https://github.com/gorilla/sessions/issues/48 --- store.go | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) (limited to 'store.go') diff --git a/store.go b/store.go index 2c0257f..77ab0bf 100644 --- a/store.go +++ b/store.go @@ -51,13 +51,16 @@ type Store interface { // Use the convenience function securecookie.GenerateRandomKey() to create // strong keys. func NewCookieStore(keyPairs ...[]byte) *CookieStore { - return &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. @@ -110,6 +113,20 @@ func (s *CookieStore) Save(r *http.Request, w http.ResponseWriter, return nil } +// MaxAge sets the maximum age for the store and the underlying cookie +// 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 { + sc.MaxAge(age) + } + } +} + // FilesystemStore ------------------------------------------------------------ var fileMutex sync.RWMutex @@ -124,7 +141,7 @@ func NewFilesystemStore(path string, keyPairs ...[]byte) *FilesystemStore { if path == "" { path = os.TempDir() } - return &FilesystemStore{ + fs := &FilesystemStore{ Codecs: securecookie.CodecsFromPairs(keyPairs...), Options: &Options{ Path: "/", @@ -132,6 +149,9 @@ func NewFilesystemStore(path string, keyPairs ...[]byte) *FilesystemStore { }, path: path, } + + fs.MaxAge(fs.Options.MaxAge) + return fs } // FilesystemStore stores sessions in the filesystem. @@ -206,6 +226,20 @@ func (s *FilesystemStore) Save(r *http.Request, w http.ResponseWriter, return nil } +// MaxAge sets the maximum age for the store and the underlying cookie +// 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 { + sc.MaxAge(age) + } + } +} + // save writes encoded session.Values to a file. func (s *FilesystemStore) save(session *Session) error { encoded, err := securecookie.EncodeMulti(session.Name(), session.Values, -- cgit v1.2.3 From c739570bf8879b63b55c74cd976a1130ae93dfa4 Mon Sep 17 00:00:00 2001 From: Matt Silverlock Date: Tue, 11 Aug 2015 20:09:55 +0800 Subject: Fixed typo in doc string. --- store.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'store.go') diff --git a/store.go b/store.go index 77ab0bf..e037e05 100644 --- a/store.go +++ b/store.go @@ -114,7 +114,7 @@ func (s *CookieStore) Save(r *http.Request, w http.ResponseWriter, } // MaxAge sets the maximum age for the store and the underlying cookie -// implementation. Individual sessions can be deleted by setting Options.MaxAge +// implementation. Individual sessions can be deleted by setting Options.MaxAge // = -1 for that session. func (s *CookieStore) MaxAge(age int) { s.Options.MaxAge = age @@ -227,7 +227,7 @@ func (s *FilesystemStore) Save(r *http.Request, w http.ResponseWriter, } // MaxAge sets the maximum age for the store and the underlying cookie -// implementation. Individual sessions can be deleted by setting Options.MaxAge +// implementation. Individual sessions can be deleted by setting Options.MaxAge // = -1 for that session. func (s *FilesystemStore) MaxAge(age int) { s.Options.MaxAge = age -- cgit v1.2.3