Documentation ¶
Index ¶
- func AddFlash(overseer Overseer, w http.ResponseWriter, r *http.Request, key string, ...) error
- func AddFlashObj(overseer Overseer, w http.ResponseWriter, r *http.Request, key string, ...) error
- func Del(overseer Overseer, w http.ResponseWriter, r *http.Request, key string) error
- func Get(overseer Overseer, w http.ResponseWriter, r *http.Request, key string) (string, error)
- func GetFlash(overseer Overseer, w http.ResponseWriter, r *http.Request, key string) (string, error)
- func GetFlashObj(overseer Overseer, w http.ResponseWriter, r *http.Request, key string, ...) error
- func GetObj(overseer Overseer, w http.ResponseWriter, r *http.Request, pointer interface{}) error
- func IsNoMapKeyError(err error) bool
- func IsNoSessionError(err error) bool
- func Middleware(next http.Handler) http.Handler
- func Set(overseer Overseer, w http.ResponseWriter, r *http.Request, key string, ...) error
- func SetObj(overseer Overseer, w http.ResponseWriter, r *http.Request, value interface{}) error
- type CookieOptions
- type CookieOverseer
- func (c *CookieOverseer) Del(w http.ResponseWriter, r *http.Request) error
- func (c *CookieOverseer) Get(w http.ResponseWriter, r *http.Request) (string, error)
- func (m CookieOverseer) MiddlewareWithReset(next http.Handler) http.Handler
- func (c *CookieOverseer) Regenerate(w http.ResponseWriter, r *http.Request) error
- func (c *CookieOverseer) ResetExpiry(w http.ResponseWriter, r *http.Request) error
- func (m CookieOverseer) ResetMiddleware(next http.Handler) http.Handler
- func (c *CookieOverseer) SessionID(w http.ResponseWriter, r *http.Request) (string, error)
- func (c *CookieOverseer) Set(w http.ResponseWriter, r *http.Request, value string) error
- type DiskStorer
- func (d *DiskStorer) All() ([]string, error)
- func (d *DiskStorer) Clean()
- func (d *DiskStorer) Del(key string) error
- func (d *DiskStorer) Get(key string) (value string, err error)
- func (d *DiskStorer) ResetExpiry(key string) error
- func (d *DiskStorer) Set(key, value string) error
- func (d *DiskStorer) StartCleaner()
- func (d *DiskStorer) StopCleaner()
- type MemoryStorer
- func (m *MemoryStorer) All() ([]string, error)
- func (m *MemoryStorer) Clean()
- func (m *MemoryStorer) Del(key string) error
- func (m *MemoryStorer) Get(key string) (value string, err error)
- func (m *MemoryStorer) ResetExpiry(key string) error
- func (m *MemoryStorer) Set(key, value string) error
- func (m *MemoryStorer) StartCleaner()
- func (m *MemoryStorer) StopCleaner()
- type Overseer
- type RedisStorer
- type Resetter
- type StorageOverseer
- func (s *StorageOverseer) Del(w http.ResponseWriter, r *http.Request) error
- func (s *StorageOverseer) Get(w http.ResponseWriter, r *http.Request) (value string, err error)
- func (m StorageOverseer) MiddlewareWithReset(next http.Handler) http.Handler
- func (s *StorageOverseer) Regenerate(w http.ResponseWriter, r *http.Request) error
- func (s *StorageOverseer) ResetExpiry(w http.ResponseWriter, r *http.Request) error
- func (m StorageOverseer) ResetMiddleware(next http.Handler) http.Handler
- func (s *StorageOverseer) SessionID(w http.ResponseWriter, r *http.Request) (string, error)
- func (s *StorageOverseer) Set(w http.ResponseWriter, r *http.Request, value string) error
- type Storer
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AddFlash ¶
func AddFlash(overseer Overseer, w http.ResponseWriter, r *http.Request, key string, value string) error
AddFlash adds a flash message to the session that will be deleted when it is retrieved with GetFlash
func AddFlashObj ¶
func AddFlashObj(overseer Overseer, w http.ResponseWriter, r *http.Request, key string, value interface{}) error
AddFlashObj adds a flash message to the session that will be deleted when it is retrieved with GetFlash
func Del ¶
Del is a JSON helper used for deleting keys from a key-value session values store. Del is a noop on nonexistent keys, but will error if the session does not exist.
func Get ¶
Get is a JSON helper used for retrieving key-value session values. Get returns the value pointed to by the key of the marshalled map stored in the session.
func GetFlash ¶
func GetFlash(overseer Overseer, w http.ResponseWriter, r *http.Request, key string) (string, error)
GetFlash retrieves a flash message from the session then deletes it
func GetFlashObj ¶
func GetFlashObj(overseer Overseer, w http.ResponseWriter, r *http.Request, key string, pointer interface{}) error
GetFlashObj unmarshals a flash message from the session into the users pointer then deletes it from the session.
func GetObj ¶
GetObj is a JSON helper used for retrieving object or variable session values. GetObj unmarshals the session value into the pointer pointed to by pointer.
func IsNoMapKeyError ¶
IsNoMapKeyError checks an error to see if it means that there was no session map key
func IsNoSessionError ¶
IsNoSessionError checks an error to see if it means that there was no session
func Middleware ¶
Middleware converts the ResponseWriter object to a sessionsResponseWriter for buffering cookies across session API requests. The sessionsResponseWriter implements cookieWriter.
If you would also like to reset the users session expiry on each request (recommended), then use MiddlewareWithReset instead.
Types ¶
type CookieOptions ¶
type CookieOptions struct { // Domain is the domain name the cookie is for Domain string // Path is the URI path the cookie is for Path string // Name for the session cookie, defaults to "id" Name string // MaxAge sets the max-age and the expires fields of a cookie // A value of 0 means the browser will expire the session on browser close MaxAge time.Duration // Secure ensures the cookie is only given on https connections Secure bool // HTTPOnly means the browser will never allow JS to touch this cookie HTTPOnly bool }
CookieOptions for the session cookies themselves. See https://tools.ietf.org/html/rfc6265 for details.
func NewCookieOptions ¶
func NewCookieOptions() CookieOptions
NewCookieOptions gives healthy defaults for session cookies
type CookieOverseer ¶
type CookieOverseer struct {
// contains filtered or unexported fields
}
CookieOverseer oversees cookie operations that are encrypted and verified but does store all data client side which means it is a possible attack vector. Uses GCM to verify and encrypt data.
func NewCookieOverseer ¶
func NewCookieOverseer(opts CookieOptions, secretKey []byte) *CookieOverseer
NewCookieOverseer creates an overseer from cookie options and a secret key for use in encryption. Panic's on any errors that deal with cryptography.
func (*CookieOverseer) Del ¶
func (c *CookieOverseer) Del(w http.ResponseWriter, r *http.Request) error
Del a value from the cookie overseer
func (*CookieOverseer) Get ¶
func (c *CookieOverseer) Get(w http.ResponseWriter, r *http.Request) (string, error)
Get a value from the cookie overseer
func (CookieOverseer) MiddlewareWithReset ¶
Middleware converts the ResponseWriter object to a sessionsResponseWriter for buffering cookies across session API requests. The sessionsResponseWriter implements cookieWriter.
MiddlewareWithReset also resets the users session expiry on each request. If you do not want this added functionality use Middleware instead.
func (*CookieOverseer) Regenerate ¶
func (c *CookieOverseer) Regenerate(w http.ResponseWriter, r *http.Request) error
Regenerate for the cookie overseer will panic because cookie sessions do not have session IDs, only values.
func (*CookieOverseer) ResetExpiry ¶
func (c *CookieOverseer) ResetExpiry(w http.ResponseWriter, r *http.Request) error
ResetExpiry resets the age of the session to time.Now(), so that MaxAge calculations are renewed
func (CookieOverseer) ResetMiddleware ¶
ResetMiddleware resets the users session expiry on each request.
Note: Generally you will want to use Middleware or MiddlewareWithReset instead of this middleware, but if you have a requirement to insert a middleware between the sessions Middleware and the sessions ResetMiddleware then you can use the Middleware first, and this one second, as a two-step process, instead of the combined MiddlewareWithReset middleware.
It's also important to note that the sessions Middleware must come BEFORE this middleware in the chain, or you will get a panic.
func (*CookieOverseer) SessionID ¶
func (c *CookieOverseer) SessionID(w http.ResponseWriter, r *http.Request) (string, error)
SessionID for the cookie overseer will panic because cookie sessions do not have session IDs, only values.
func (*CookieOverseer) Set ¶
func (c *CookieOverseer) Set(w http.ResponseWriter, r *http.Request, value string) error
Set a value into the cookie overseer
type DiskStorer ¶
type DiskStorer struct {
// contains filtered or unexported fields
}
DiskStorer is a session storer implementation for saving sessions to disk.
func NewDefaultDiskStorer ¶
func NewDefaultDiskStorer(tmpSubFolder string) (*DiskStorer, error)
NewDefaultDiskStorer returns a DiskStorer object with default values. The default values are: FolderPath: system tmp dir + random folder maxAge: 2 days (clear session stored on server after 2 days) cleanInterval: 1 hour (delete sessions older than maxAge every 1 hour)
func NewDiskStorer ¶
func NewDiskStorer(folderPath string, maxAge, cleanInterval time.Duration) (*DiskStorer, error)
NewDiskStorer initializes and returns a new DiskStorer object. It takes the maxAge of how long each session should live on disk, and a cleanInterval duration which defines how often the clean task should check for maxAge expired sessions to be removed from disk. Persistent storage can be attained by setting maxAge and cleanInterval to zero.
func (*DiskStorer) Clean ¶
func (d *DiskStorer) Clean()
Clean checks all session files on disk to see if they are older than maxAge by checking their access time. If it finds an expired session file it will remove it from disk.
func (*DiskStorer) Del ¶
func (d *DiskStorer) Del(key string) error
Del the session pointed to by the session id key and remove it.
func (*DiskStorer) Get ¶
func (d *DiskStorer) Get(key string) (value string, err error)
Get returns the value string saved in the session pointed to by the session id key.
func (*DiskStorer) ResetExpiry ¶
func (d *DiskStorer) ResetExpiry(key string) error
ResetExpiry resets the expiry of the key
func (*DiskStorer) Set ¶
func (d *DiskStorer) Set(key, value string) error
Set saves the value string to the session pointed to by the session id key.
func (*DiskStorer) StartCleaner ¶
func (d *DiskStorer) StartCleaner()
StartCleaner starts the disk session cleaner go routine. This go routine will delete expired disk sessions on the cleanInterval interval.
func (*DiskStorer) StopCleaner ¶
func (d *DiskStorer) StopCleaner()
StopCleaner stops the cleaner go routine
type MemoryStorer ¶
type MemoryStorer struct {
// contains filtered or unexported fields
}
MemoryStorer is a session storer implementation for saving sessions to memory.
func NewDefaultMemoryStorer ¶
func NewDefaultMemoryStorer() (*MemoryStorer, error)
NewDefaultMemoryStorer returns a MemoryStorer object with default values. The default values are: maxAge: 2 days (clear session stored on server after 2 days) cleanInterval: 1 hour (delete sessions older than maxAge every hour)
func NewMemoryStorer ¶
func NewMemoryStorer(maxAge, cleanInterval time.Duration) (*MemoryStorer, error)
NewMemoryStorer initializes and returns a new MemoryStorer object. It takes the maxAge of how long each session should live in memory, and a cleanInterval duration which defines how often the clean task should check for maxAge sessions to be removed from memory. Persistent storage can be attained by setting maxAge and cleanInterval to zero, however the memory will be wiped when the server is restarted.
func (*MemoryStorer) All ¶
func (m *MemoryStorer) All() ([]string, error)
All keys in the memory store
func (*MemoryStorer) Clean ¶
func (m *MemoryStorer) Clean()
Clean checks all sessions in memory to see if they are older than maxAge by checking their expiry. If it finds an expired session it will remove it from memory.
func (*MemoryStorer) Del ¶
func (m *MemoryStorer) Del(key string) error
Del the session pointed to by the session id key and remove it.
func (*MemoryStorer) Get ¶
func (m *MemoryStorer) Get(key string) (value string, err error)
Get returns the value string saved in the session pointed to by the session id key.
func (*MemoryStorer) ResetExpiry ¶
func (m *MemoryStorer) ResetExpiry(key string) error
ResetExpiry resets the expiry of the key
func (*MemoryStorer) Set ¶
func (m *MemoryStorer) Set(key, value string) error
Set saves the value string to the session pointed to by the session id key.
func (*MemoryStorer) StartCleaner ¶
func (m *MemoryStorer) StartCleaner()
StartCleaner starts the memory session cleaner go routine. This go routine will delete expired sessions from the memory map on the cleanInterval interval.
func (*MemoryStorer) StopCleaner ¶
func (m *MemoryStorer) StopCleaner()
StopCleaner stops the cleaner go routine
type Overseer ¶
type Overseer interface { Resetter // Get the value stored in a session Get(w http.ResponseWriter, r *http.Request) (value string, err error) // Set creates or updates a session with value Set(w http.ResponseWriter, r *http.Request, value string) error // Delete a session Del(w http.ResponseWriter, r *http.Request) error // Regenerate a new session id for your session Regenerate(w http.ResponseWriter, r *http.Request) error // SessionID returns the session id for your session SessionID(w http.ResponseWriter, r *http.Request) (id string, err error) }
Overseer of session cookies
type RedisStorer ¶
type RedisStorer struct {
// contains filtered or unexported fields
}
RedisStorer is a session storer implementation for saving sessions to a Redis database.
func NewDefaultRedisStorer ¶
func NewDefaultRedisStorer(addr, password string, db int) (*RedisStorer, error)
NewDefaultRedisStorer takes a bind address of the Redis server host:port and returns a RedisStorer object with default values. The default values are: Addr: localhost:6379 Password: no password DB: First database (0) to be selected after connecting to Redis maxAge: 2 days (clear session stored in Redis after 2 days)
func NewRedisStorer ¶
NewRedisStorer initializes and returns a new RedisStorer object. It takes a bind address of the Redis server host:port and the maxAge of how long each session should live in the Redis server. Persistent storage can be attained by setting maxAge to zero.
func (*RedisStorer) Del ¶
func (r *RedisStorer) Del(key string) error
Del the session pointed to by the session id key and remove it.
func (*RedisStorer) Get ¶
func (r *RedisStorer) Get(key string) (value string, err error)
Get returns the value string saved in the session pointed to by the session id key.
func (*RedisStorer) ResetExpiry ¶
func (r *RedisStorer) ResetExpiry(key string) error
ResetExpiry resets the expiry of the key
func (*RedisStorer) Set ¶
func (r *RedisStorer) Set(key, value string) error
Set saves the value string to the session pointed to by the session id key.
type Resetter ¶
type Resetter interface { // ResetExpiry resets the age of the session to time.Now(), so that // MaxAge calculations are renewed ResetExpiry(w http.ResponseWriter, r *http.Request) error // ResetMiddleware will reset the users session expiry on every request ResetMiddleware(next http.Handler) http.Handler // MiddlewareWithReset converts the writer to a sessionsResponseWriter // and will reset the users session expiry on every request MiddlewareWithReset(next http.Handler) http.Handler }
Resetter has reset functions
type StorageOverseer ¶
type StorageOverseer struct { Storer Storer // contains filtered or unexported fields }
StorageOverseer holds cookie related variables and a session storer
func NewStorageOverseer ¶
func NewStorageOverseer(opts CookieOptions, storer Storer) *StorageOverseer
NewStorageOverseer returns a new storage overseer
func (*StorageOverseer) Del ¶
func (s *StorageOverseer) Del(w http.ResponseWriter, r *http.Request) error
Del deletes the session if it exists and sets the session cookie to expire instantly.
func (*StorageOverseer) Get ¶
func (s *StorageOverseer) Get(w http.ResponseWriter, r *http.Request) (value string, err error)
Get looks in the cookie for the session ID and retrieves the value string stored in the session.
func (StorageOverseer) MiddlewareWithReset ¶
Middleware converts the ResponseWriter object to a sessionsResponseWriter for buffering cookies across session API requests. The sessionsResponseWriter implements cookieWriter.
MiddlewareWithReset also resets the users session expiry on each request. If you do not want this added functionality use Middleware instead.
func (*StorageOverseer) Regenerate ¶
func (s *StorageOverseer) Regenerate(w http.ResponseWriter, r *http.Request) error
Regenerate a new session ID for your current session
func (*StorageOverseer) ResetExpiry ¶
func (s *StorageOverseer) ResetExpiry(w http.ResponseWriter, r *http.Request) error
ResetExpiry resets the age of the session to time.Now(), so that MaxAge calculations are renewed
func (StorageOverseer) ResetMiddleware ¶
ResetMiddleware resets the users session expiry on each request.
Note: Generally you will want to use Middleware or MiddlewareWithReset instead of this middleware, but if you have a requirement to insert a middleware between the sessions Middleware and the sessions ResetMiddleware then you can use the Middleware first, and this one second, as a two-step process, instead of the combined MiddlewareWithReset middleware.
It's also important to note that the sessions Middleware must come BEFORE this middleware in the chain, or you will get a panic.
func (*StorageOverseer) SessionID ¶
func (s *StorageOverseer) SessionID(w http.ResponseWriter, r *http.Request) (string, error)
SessionID returns the session ID stored in the cookie's value field. It will return a errNoSession error if no session exists.
func (*StorageOverseer) Set ¶
func (s *StorageOverseer) Set(w http.ResponseWriter, r *http.Request, value string) error
Set looks in the cookie for the session ID and modifies the session with the new value. If the session does not exist it creates a new one.