Documentation
¶
Overview ¶
Package sessions manages users sessions
Index ¶
- Constants
- Variables
- func GenerateSessionID() string
- func LoadAndSave(sessionManager CookieStore, client *redis.Client, logger *zap.SugaredLogger) echo.MiddlewareFunc
- func LoadAndSaveWithConfig(config *SessionConfig) echo.MiddlewareFunc
- func NewDebugSessionCookie(session string) *http.Cookie
- func NewSessionCookie(session string) *http.Cookie
- func Token(ctx context.Context) map[string]string
- type Config
- type ContextKey
- type CookieStore
- type PersistentStore
- type Session
- type SessionConfig
Constants ¶
const (
DefaultSessionName = "__session_id"
)
Variables ¶
var DefaultSessionConfig = SessionConfig{ Skipper: middleware.DefaultSkipper, }
var ( // ErrInvalidSession is returned when the session is invalid ErrInvalidSession = errors.New("invalid session provided") )
var SessionContextKey = &ContextKey{"SessionContextKey"}
SessionContextKey is the context key for the session-context
Functions ¶
func LoadAndSave ¶
func LoadAndSave(sessionManager CookieStore, client *redis.Client, logger *zap.SugaredLogger) echo.MiddlewareFunc
LoadAndSave is a middleware function that loads and saves session data using a provided session manager. It takes a `SessionManager` as input and returns a middleware function that can be used with an Echo framework application
func LoadAndSaveWithConfig ¶
func LoadAndSaveWithConfig(config *SessionConfig) echo.MiddlewareFunc
LoadAndSaveWithConfig is a middleware that loads and saves session data using a provided session manager configuration. It takes a `SessionConfig` struct as input, which contains the skipper function and the session manager
func NewDebugSessionCookie ¶
NewDebugSessionCookie creates a debug cookie from a session id
func NewSessionCookie ¶
NewSessionCookie creates a cookie from a session id
Types ¶
type Config ¶
type Config struct {
// SigningKey must be a 16, 32, or 64 character string used to encode the cookie
SigningKey string `yaml:"signingKey" split_words:"true" default:"my-signing-secret"` // $DATUM_SESSIONS_SIGNING_KEY
// EncryptionKey must be a 16, 32, or 64 character string used to encode the cookie
EncryptionKey string `yaml:"encryptionKey" split_words:"true" default:"encryptionsecret"` // $DATUM_SESSIONS_ENCRYPTION_KEY
}
type ContextKey ¶
type ContextKey struct {
// contains filtered or unexported fields
}
ContextKey is the key name for the additional context
type CookieStore ¶
type CookieStore interface {
// New returns a new named Session
New(name string) *Session
// Get a named Session from the request
Get(req *http.Request, name string) (*Session, error)
// Save writes a Session to the ResponseWriter
Save(w http.ResponseWriter, session *Session) error
// Destroy removes (expires) a named Session
Destroy(w http.ResponseWriter, name string)
// GetUserFromSession with the provided cookie name
GetUserFromSession(req *http.Request, name string) (string, error)
}
A CookieStore manages creating, accessing, writing, and expiring Sessions.
func NewCookieStore ¶
func NewCookieStore(config *cookies.CookieConfig, keyPairs ...[]byte) CookieStore
NewCookieStore returns a new Store that signs and optionally encrypts session state in http cookies.
type PersistentStore ¶
type PersistentStore interface {
Exists(ctx context.Context, userID string) (int64, error)
GetSession(ctx context.Context, userID string) (string, error)
StoreSession(ctx context.Context, sessionID string, userID string) error
DeleteSession(ctx context.Context, userID string) error
}
PersistentStore is defining an interface for session store
func NewStore ¶
func NewStore(client *redis.Client) PersistentStore
NewStore returns a new Store that stores to a persistent backend (redis)
type Session ¶
type Session struct {
// contains filtered or unexported fields
}
Session represents state values maintained in a sessions Store.
func NewWithName ¶
func NewWithName(store CookieStore, name string) *Session
NewWithName returns a new Session with the provided name
func (*Session) Destroy ¶
func (s *Session) Destroy(w http.ResponseWriter)
Destroy destroys the session. Identical to calling store.Destroy(w, session.name).
func (*Session) GetOk ¶
GetOk returns the state value for the given key and whether they key exists.
type SessionConfig ¶
type SessionConfig struct {
// Skipper is a function that determines whether a particular request should be skipped or not
Skipper middleware.Skipper
// SessionManager is responsible for managing the session cookies. It handles the creation, retrieval, and deletion of
// session cookies for each user session
SessionManager CookieStore
// Store is used to store and retrieve session data in a persistent manner such as to a redis backend
Store PersistentStore
// RedisClient establishes a connection to a Redis server and perform operations such as storing and retrieving data
RedisClient *redis.Client
// Logger is used to log errors in the middleware
Logger *zap.SugaredLogger
}
SessionConfig is used to configure session management
func NewSessionConfig ¶
func NewSessionConfig(sm CookieStore, rc *redis.Client, logger *zap.SugaredLogger) *SessionConfig
NewSessionConfig creates a new session config
func (*SessionConfig) SaveAndStoreSession ¶
func (sc *SessionConfig) SaveAndStoreSession(ctx echo.Context, userID string) error
SaveAndStoreSession saves the session to the cookie and to the persistent store (redis)