summaryrefslogtreecommitdiff
path: root/store.go
diff options
context:
space:
mode:
Diffstat (limited to 'store.go')
-rw-r--r--store.go38
1 files changed, 36 insertions, 2 deletions
diff --git a/store.go b/store.go
index 2c0257f..e037e05 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,