Mercurial > gemma
changeset 1343:9e0beb373690
Added comments how to use the session store.
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Mon, 26 Nov 2018 11:15:52 +0100 |
parents | 20b9c3f261db |
children | eda98694e678 6a021108410b |
files | pkg/auth/store.go |
diffstat | 1 files changed, 21 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/pkg/auth/store.go Mon Nov 26 11:01:11 2018 +0100 +++ b/pkg/auth/store.go Mon Nov 26 11:15:52 2018 +0100 @@ -22,11 +22,14 @@ bolt "github.com/etcd-io/bbolt" ) +// ErrNoSuchToken is returned if a given token does not +// exists th the session store. var ErrNoSuchToken = errors.New("No such token") // Sessions is the global connection pool. var Sessions *SessionStore +// SessionStore encapsulates a set of currently active sessions. type SessionStore struct { storage *bolt.DB sessions map[string]*Session @@ -35,6 +38,11 @@ var sessionsBucket = []byte("sessions") +// NewSessionStore creates a new session store. +// If the filename is empty the session are only hold in memory. +// If the filename is not empty the sessions are mirrored to +// a file with this name. Use the later option if you want +// a persistent session store. func NewSessionStore(filename string) (*SessionStore, error) { ss := &SessionStore{ @@ -125,6 +133,8 @@ } } +// Delete removes a session identified by its token from the +// session store. Returns true if there was such s session. func (ss *SessionStore) Delete(token string) bool { res := make(chan bool) ss.cmds <- func() { @@ -156,6 +166,9 @@ } } +// Add puts a session into the session store identified by +// a given token. An old session with the same key will +// be replaced. func (ss *SessionStore) Add(token string, session *Session) { res := make(chan struct{}) @@ -173,6 +186,9 @@ <-res } +// Renew refreshes a session. It takes an old token to +// identify a session and returns a new token with the +// freshed up one. func (ss *SessionStore) Renew(token string) (string, error) { type result struct { @@ -202,6 +218,8 @@ return r.newToken, r.err } +// Session returns the session associated with given token. +// Returns nil if no matching session was found. func (ss *SessionStore) Session(token string) *Session { res := make(chan *Session) ss.cmds <- func() { @@ -217,6 +235,7 @@ return <-res } +// Logout removes all sessions of a given user from the session store. func (ss *SessionStore) Logout(user string) { ss.cmds <- func() { for token, session := range ss.sessions { @@ -228,6 +247,8 @@ } } +// Shutdown closes the session store. +// If using the persistent mode the backing session database is closed. func (ss *SessionStore) Shutdown() error { if db := ss.storage; db != nil { log.Println("info: shutdown persistent session store.")