Documentation
¶
Overview ¶
session/memory.go
session/middleware.go
session/redis.go
session/session.go
Index ¶
- Variables
- func Flash(session *Session, key string, value any)
- func GetFlash(session *Session, key string) (any, bool)
- func GetFlashString(session *Session, key string) string
- func Middleware(m *Manager) func(http.Handler) http.Handler
- func RequireKey(key string) func(http.Handler) http.Handler
- func RequireSession(m *Manager) func(http.Handler) http.Handler
- type Config
- type Manager
- func (m *Manager) Close() error
- func (m *Manager) Destroy(w http.ResponseWriter, r *http.Request, session *Session) error
- func (m *Manager) Get(r *http.Request) (*Session, error)
- func (m *Manager) New() (*Session, error)
- func (m *Manager) Refresh(w http.ResponseWriter, r *http.Request, session *Session) error
- func (m *Manager) Regenerate(w http.ResponseWriter, r *http.Request, session *Session) error
- func (m *Manager) Save(w http.ResponseWriter, r *http.Request, session *Session) error
- func (m *Manager) Store() Store
- type MemoryStore
- type MemoryStoreConfig
- type RedisStore
- func (s *RedisStore) Client() redis.UniversalClient
- func (s *RedisStore) Close() error
- func (s *RedisStore) Delete(ctx context.Context, id string) error
- func (s *RedisStore) Load(ctx context.Context, id string) (*SessionData, error)
- func (s *RedisStore) Save(ctx context.Context, data *SessionData) error
- type RedisStoreConfig
- type Session
- func (s *Session) Clear()
- func (s *Session) Delete(key string)
- func (s *Session) ExpiresAt() time.Time
- func (s *Session) Get(key string) (any, bool)
- func (s *Session) GetBool(key string) bool
- func (s *Session) GetInt(key string) int
- func (s *Session) GetString(key string) string
- func (s *Session) GetTime(key string) time.Time
- func (s *Session) ID() string
- func (s *Session) IsNew() bool
- func (s *Session) Keys() []string
- func (s *Session) Modified() bool
- func (s *Session) Set(key string, value any)
- func (s *Session) Values() map[string]any
- type SessionData
- type Store
Constants ¶
This section is empty.
Variables ¶
var ( ErrNotFound = errors.New("session: not found") ErrExpired = errors.New("session: expired") ErrInvalidSession = errors.New("session: invalid session") )
Common errors
Functions ¶
func Flash ¶
Flash stores a message that will be deleted after being read. Useful for one-time messages like "Login successful".
func GetFlashString ¶
GetFlashString retrieves and removes a flash message as a string.
func Middleware ¶
Middleware returns HTTP middleware that loads sessions automatically. The session is available via FromContext(r.Context()).
func RequireKey ¶
RequireKey middleware returns 401 if the session doesn't have a specific key.
Types ¶
type Config ¶
type Config struct {
// CookieName is the name of the session cookie.
// Default: "session_id".
CookieName string
// MaxAge is the session lifetime.
// Default: 24 hours.
MaxAge time.Duration
// Path is the cookie path.
// Default: "/".
Path string
// Domain is the cookie domain.
// Default: "" (current domain).
Domain string
// Secure sets the Secure flag on the cookie.
// Default: true.
Secure bool
// HttpOnly sets the HttpOnly flag on the cookie.
// Default: true.
HttpOnly bool
// SameSite sets the SameSite attribute.
// Default: http.SameSiteLaxMode.
SameSite http.SameSite
// IDGenerator generates session IDs.
// Default: cryptographically secure random ID.
IDGenerator func() (string, error)
}
Config configures the session manager.
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager handles session creation, retrieval, and persistence.
func NewManager ¶
NewManager creates a session manager with the given store and config.
func (*Manager) Regenerate ¶
Regenerate creates a new session ID while preserving data. Use this after authentication to prevent session fixation attacks.
type MemoryStore ¶
type MemoryStore struct {
// contains filtered or unexported fields
}
MemoryStore implements in-memory session storage.
func NewMemoryStore ¶
func NewMemoryStore() *MemoryStore
NewMemoryStore creates a new in-memory session store.
func NewMemoryStoreWithConfig ¶
func NewMemoryStoreWithConfig(cfg MemoryStoreConfig) *MemoryStore
NewMemoryStoreWithConfig creates a memory store with custom configuration.
func (*MemoryStore) Delete ¶
func (s *MemoryStore) Delete(ctx context.Context, id string) error
Delete removes a session by ID.
func (*MemoryStore) Load ¶
func (s *MemoryStore) Load(ctx context.Context, id string) (*SessionData, error)
Load retrieves session data by ID.
func (*MemoryStore) Save ¶
func (s *MemoryStore) Save(ctx context.Context, data *SessionData) error
Save stores session data.
type MemoryStoreConfig ¶
type MemoryStoreConfig struct {
// CleanupInterval is how often to remove expired sessions.
// Default: 10 minutes.
CleanupInterval time.Duration
}
MemoryStoreConfig configures the memory store.
type RedisStore ¶
type RedisStore struct {
// contains filtered or unexported fields
}
RedisStore implements Redis-backed session storage.
func ConnectRedis ¶
func ConnectRedis(addr, password string, db int) (*RedisStore, error)
ConnectRedis creates a Redis store with simple connection parameters.
func NewRedisStore ¶
func NewRedisStore(client redis.UniversalClient) *RedisStore
NewRedisStore creates a Redis store with an existing client.
func NewRedisStoreWithConfig ¶
func NewRedisStoreWithConfig(cfg RedisStoreConfig) (*RedisStore, error)
NewRedisStoreWithConfig creates a Redis store with custom configuration.
func (*RedisStore) Client ¶
func (s *RedisStore) Client() redis.UniversalClient
Client returns the underlying Redis client.
func (*RedisStore) Delete ¶
func (s *RedisStore) Delete(ctx context.Context, id string) error
Delete removes a session by ID.
func (*RedisStore) Load ¶
func (s *RedisStore) Load(ctx context.Context, id string) (*SessionData, error)
Load retrieves session data by ID.
func (*RedisStore) Save ¶
func (s *RedisStore) Save(ctx context.Context, data *SessionData) error
Save stores session data.
type RedisStoreConfig ¶
type RedisStoreConfig struct {
// Client is an existing Redis client.
// If provided, other connection options are ignored.
Client redis.UniversalClient
// Address is the Redis server address.
Address string
// Password for Redis authentication.
Password string
// DB is the database number.
DB int
// KeyPrefix is prepended to session keys.
// Default: "session:".
KeyPrefix string
// PoolSize is the connection pool size.
// Default: 10.
PoolSize int
}
RedisStoreConfig configures the Redis store.
type Session ¶
type Session struct {
// contains filtered or unexported fields
}
Session represents a user session with key-value data.
func FromContext ¶
FromContext retrieves the session from the request context. Returns nil if no session is in context (middleware not used).
func MustFromContext ¶
MustFromContext retrieves the session from context, panicking if not found.
type SessionData ¶
type SessionData struct {
ID string `json:"id"`
Data map[string]any `json:"data"`
ExpiresAt time.Time `json:"expires_at"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
SessionData is the serializable session data stored in backends.
func (*SessionData) MarshalBinary ¶
func (s *SessionData) MarshalBinary() ([]byte, error)
MarshalBinary implements encoding.BinaryMarshaler.
func (*SessionData) UnmarshalBinary ¶
func (s *SessionData) UnmarshalBinary(data []byte) error
UnmarshalBinary implements encoding.BinaryUnmarshaler.
type Store ¶
type Store interface {
// Load retrieves session data by ID.
// Returns ErrNotFound if the session doesn't exist.
Load(ctx context.Context, id string) (*SessionData, error)
// Save stores session data.
Save(ctx context.Context, data *SessionData) error
// Delete removes a session by ID.
Delete(ctx context.Context, id string) error
// Close releases any resources.
Close() error
}
Store defines the interface for session storage backends.