sessions

package
v0.0.0-...-9ee207d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 15, 2020 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidID = errors.New("Invalid Session ID")

ErrInvalidID is returned when an invalid session id is passed to ValidateID()

View Source
var ErrInvalidScheme = errors.New("authorization scheme not supported")

ErrInvalidScheme is used when the authorization scheme is not supported

View Source
var ErrNoSessionID = errors.New("no session ID found in " + headerAuthorization + " header")

ErrNoSessionID is used when no session ID was found in the Authorization header

View Source
var ErrStateNotFound = errors.New("no session state was found in the session store")

ErrStateNotFound is returned from Store.Get() when the requested session id was not found in the store

Functions

This section is empty.

Types

type RedisStore

type RedisStore struct {
	//Redis client used to talk to redis server.
	Client *redis.Client
	//Used for key expiry time on redis.
	SessionDuration time.Duration
}

RedisStore represents a session.Store backed by redis.

func NewRedisStore

func NewRedisStore(client *redis.Client, sessionDuration time.Duration) *RedisStore

NewRedisStore constructs a new RedisStore

func (*RedisStore) Delete

func (rs *RedisStore) Delete(sid SessionID) error

Delete deletes all state data associated with the SessionID from the store.

func (*RedisStore) Get

func (rs *RedisStore) Get(sid SessionID, sessionState interface{}) error

Get populates `sessionState` with the data previously saved for the given SessionID

func (*RedisStore) Save

func (rs *RedisStore) Save(sid SessionID, sessionState interface{}) error

Save saves the provided `sessionState` and associated SessionID to the store. The `sessionState` parameter is typically a pointer to a struct containing all the data you want to associated with the given SessionID.

type SessionID

type SessionID string

SessionID represents a valid, digitally-signed session ID. This is a base64 URL encoded string created from a byte slice where the first `idLength` bytes are crytographically random bytes representing the unique session ID, and the remaining bytes are an HMAC hash of those ID bytes (i.e., a digital signature). The byte slice layout is like so: +-----------------------------------------------------+ |...32 crypto random bytes...|HMAC hash of those bytes| +-----------------------------------------------------+

const InvalidSessionID SessionID = ""

InvalidSessionID represents an empty, invalid session ID

func BeginSession

func BeginSession(signingKey string, store Store, sessionState interface{}, w http.ResponseWriter) (SessionID, error)

BeginSession creates a new SessionID, saves the `sessionState` to the store, adds an Authorization header to the response with the SessionID, and returns the new SessionID

func EndSession

func EndSession(r *http.Request, signingKey string, store Store) (SessionID, error)

EndSession extracts the SessionID from the request, and deletes the associated data in the provided store, returning the extracted SessionID.

func GetSessionID

func GetSessionID(r *http.Request, signingKey string) (SessionID, error)

GetSessionID extracts and validates the SessionID from the request headers

func GetState

func GetState(r *http.Request, signingKey string, store Store, sessionState interface{}) (SessionID, error)

GetState extracts the SessionID from the request, gets the associated state from the provided store into the `sessionState` parameter, and returns the SessionID

func NewSessionID

func NewSessionID(signingKey string) (SessionID, error)

NewSessionID creates and returns a new digitally-signed session ID, using `signingKey` as the HMAC signing key. An error is returned only if there was an error generating random bytes for the session ID

func ValidateID

func ValidateID(id string, signingKey string) (SessionID, error)

ValidateID validates the string in the `id` parameter using the `signingKey` as the HMAC signing key and returns an error if invalid, or a SessionID if valid

func (SessionID) String

func (sid SessionID) String() string

String returns a string representation of the sessionID

type Store

type Store interface {
	//Save saves the provided `sessionState` and associated SessionID to the store.
	//The `sessionState` parameter is typically a pointer to a struct containing
	//all the data you want to associated with the given SessionID.
	Save(sid SessionID, sessionState interface{}) error

	//Get populates `sessionState` with the data previously saved
	//for the given SessionID
	Get(sid SessionID, sessionState interface{}) error

	//Delete deletes all state data associated with the SessionID from the store.
	Delete(sid SessionID) error
}

Store represents a session data store. This is an abstract interface that can be implemented against several different types of data stores. For example, session data could be stored in memory in a concurrent map, or more typically in a shared key/value server store like redis.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL