Documentation ¶
Index ¶
- func AddFlash(w http.ResponseWriter, key string, value string)
- func AddFlashObj(w http.ResponseWriter, key string, obj interface{}) error
- func Del(w http.ResponseWriter, key string)
- func DelAll(w http.ResponseWriter, whitelist []string)
- func Get(ctx context.Context, key string) (string, bool)
- func GetFlash(w http.ResponseWriter, ctx context.Context, key string) (string, bool)
- func GetFlashObj(w http.ResponseWriter, ctx context.Context, key string, obj interface{}) error
- func GetObj(ctx context.Context, key string, obj interface{}) error
- func IsNoMapKeyError(err error) bool
- func IsNoSessionError(err error) bool
- func Refresh(w http.ResponseWriter)
- func Set(w http.ResponseWriter, key, value string)
- func SetObj(w http.ResponseWriter, key string, obj interface{}) error
- type CTXKeyPossessions
- type CookieOptions
- type DiskStorer
- func (d *DiskStorer) All(ctx context.Context) ([]string, error)
- func (d *DiskStorer) Clean()
- func (d *DiskStorer) Del(ctx context.Context, key string) error
- func (d *DiskStorer) Get(ctx context.Context, key string) (value string, err error)
- func (d *DiskStorer) ResetExpiry(ctx context.Context, key string) error
- func (d *DiskStorer) Set(ctx context.Context, key, value string) error
- func (d *DiskStorer) StartCleaner()
- func (d *DiskStorer) StopCleaner()
- type Event
- type EventKind
- type MemoryStorer
- func (m *MemoryStorer) All(ctx context.Context) ([]string, error)
- func (m *MemoryStorer) Clean()
- func (m *MemoryStorer) Del(ctx context.Context, key string) error
- func (m *MemoryStorer) Get(ctx context.Context, key string) (value string, err error)
- func (m *MemoryStorer) ResetExpiry(ctx context.Context, key string) error
- func (m *MemoryStorer) Set(ctx context.Context, key, value string) error
- func (m *MemoryStorer) StartCleaner()
- func (m *MemoryStorer) StopCleaner()
- type OverseeingMiddleware
- type Overseer
- type RedisStorer
- func (r *RedisStorer) All(ctx context.Context) ([]string, error)
- func (r *RedisStorer) Del(ctx context.Context, key string) error
- func (r *RedisStorer) Get(ctx context.Context, key string) (value string, err error)
- func (r *RedisStorer) ResetExpiry(ctx context.Context, key string) error
- func (r *RedisStorer) Set(ctx context.Context, key, value string) error
- type RefreshMiddleware
- type Session
- type StorageOverseer
- type Storer
- type UnderlyingResponseWriter
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AddFlash ¶
func AddFlash(w http.ResponseWriter, key string, value string)
AddFlash adds a flash message to the session. Typically read and removed on the next request.
func AddFlashObj ¶
func AddFlashObj(w http.ResponseWriter, key string, obj interface{}) error
AddFlashObj adds a flash message to the session using an object that's marshalled into JSON
func DelAll ¶
func DelAll(w http.ResponseWriter, whitelist []string)
DelAll delete all keys except for a whitelist
func GetFlash ¶
GetFlash reads a flash message from the request and deletes it using the responsewriter.
func GetFlashObj ¶
GetFlashObj reads a json-encoded flash message from the session and unmarshals it into obj. Use IsNoMapKeyError to determine if the value was found or not.
func GetObj ¶
GetObj a session json encoded string and decode it into obj. Use the IsNoMapKeyError to determine if the value was found or not.
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
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 // SameSite allows you to declare if your cookie should be restricted to a first-party or same-site context SameSite http.SameSite }
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 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) All ¶
func (d *DiskStorer) All(ctx context.Context) ([]string, error)
All keys in the disk store
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(ctx context.Context, key string) error
Del the session pointed to by the session id key and remove it.
func (*DiskStorer) Get ¶
Get returns the value string saved in the session pointed to by the session id key.
func (*DiskStorer) ResetExpiry ¶
func (d *DiskStorer) ResetExpiry(ctx context.Context, key string) error
ResetExpiry resets the expiry of the key
func (*DiskStorer) Set ¶
func (d *DiskStorer) Set(ctx context.Context, 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 EventKind ¶
type EventKind int
EventKind of session mutation
const ( // EventSet sets a key-value pair EventSet EventKind = iota // EventDel removes a key EventDel // EventDelAll means you should delete EVERY key-value pair from // the client state - though a whitelist of keys that should not be deleted // will be set in Keys EventDelAll // EventRefresh should refresh the TTL if any on the session EventRefresh // Deletes the client state EventDelClientState )
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(ctx context.Context) ([]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(ctx context.Context, key string) error
Del the session pointed to by the session id key and remove it.
func (*MemoryStorer) Get ¶
Get returns the value string saved in the session pointed to by the session id key.
func (*MemoryStorer) ResetExpiry ¶
func (m *MemoryStorer) ResetExpiry(ctx context.Context, key string) error
ResetExpiry resets the expiry of the key
func (*MemoryStorer) Set ¶
func (m *MemoryStorer) Set(ctx context.Context, 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 OverseeingMiddleware ¶
type OverseeingMiddleware struct {
// contains filtered or unexported fields
}
OverseeingMiddleware enables the use of sessions in this package by allowing read and writes of client state during the request.
func NewOverseeingMiddleware ¶
func NewOverseeingMiddleware(overseer Overseer) OverseeingMiddleware
NewOverseeingMiddleware constructs a middleware
type Overseer ¶
type Overseer interface { // ReadState should return a map like structure allowing it to look up // any values in the current session, or any cookie in the request ReadState(*http.Request) (Session, error) // WriteState can sometimes be called with a nil ClientState in the event // that no ClientState was read in from LoadClientState WriteState(context.Context, http.ResponseWriter, Session, []Event) 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 ¶
func NewRedisStorer(opts redis.Options, maxAge time.Duration) (*RedisStorer, error)
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 NewRedisStorerClient ¶
func NewRedisStorerClient(client *redis.Client, maxAge time.Duration) (*RedisStorer, error)
NewRedisStorerClient behaves the same as NewRedisStorer but does not create a new connection pool.
func (*RedisStorer) All ¶
func (r *RedisStorer) All(ctx context.Context) ([]string, error)
All keys in the redis store
func (*RedisStorer) Del ¶
func (r *RedisStorer) Del(ctx context.Context, key string) error
Del the session pointed to by the session id key and remove it.
func (*RedisStorer) Get ¶
Get returns the value string saved in the session pointed to by the session id key.
func (*RedisStorer) ResetExpiry ¶
func (r *RedisStorer) ResetExpiry(ctx context.Context, key string) error
ResetExpiry resets the expiry of the key
type RefreshMiddleware ¶
type RefreshMiddleware struct{}
RefreshMiddleware refreshes sessions on each request
func NewRefreshMiddleware ¶
func NewRefreshMiddleware() RefreshMiddleware
NewRefreshMiddleware creates a refresh middleware
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) ReadState ¶
func (s StorageOverseer) ReadState(r *http.Request) (Session, error)
ReadState from the request
func (StorageOverseer) WriteState ¶
func (s StorageOverseer) WriteState(ctx context.Context, w http.ResponseWriter, sess Session, evs []Event) error
WriteState to the response
type Storer ¶
type Storer interface { // All returns all keys in the store All(ctx context.Context) (keys []string, err error) Get(ctx context.Context, key string) (value string, err error) Set(ctx context.Context, key, value string) error Del(ctx context.Context, key string) error ResetExpiry(ctx context.Context, key string) error }
Storer provides methods to retrieve, add and delete sessions.
type UnderlyingResponseWriter ¶
type UnderlyingResponseWriter interface {
UnderlyingResponseWriter() http.ResponseWriter
}
UnderlyingResponseWriter allows wrapping responsewriters to be able to hand the possessions.responseWriter back to possessions for session management while still being able to use their own custom one.