session

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2026 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const (
	SameSiteLax    = http.SameSiteLaxMode
	SameSiteStrict = http.SameSiteStrictMode
	SameSiteNone   = http.SameSiteNoneMode
)

SameSite values.

Variables

This section is empty.

Functions

func KeyFromString

func KeyFromString(s string) []byte

KeyFromString derives a 32-byte key from a string (e.g. APP_KEY) using HKDF-SHA256. Provide a high-entropy APP_KEY (32 random bytes); this is not a password-stretching KDF.

func Middleware

func Middleware(cfg Config) router.Middleware

Middleware returns middleware that loads the session from the store and sets it on the request context. If no session cookie exists, a new session is created on first write.

Types

type Config

type Config struct {
	Store      Store
	CookieName string
	MaxAge     time.Duration
	HttpOnly   bool
	Secure     bool
	SameSite   http.SameSite
}

Config holds session middleware options.

Cookie security attributes are secure-by-default: the session cookie is always written with HttpOnly, and with SameSite=Lax unless overridden. The Secure flag is set automatically whenever the request is served over HTTPS (TLS or X-Forwarded-Proto: https); set Secure=true to force it on even behind a proxy that does not forward the scheme.

type CookieStoreImpl

type CookieStoreImpl struct {
	// contains filtered or unexported fields
}

func NewCookieStore

func NewCookieStore(key []byte) *CookieStoreImpl

NewCookieStore creates a cookie-based session store. Session data is encrypted in the cookie. No server-side storage; suitable for small payloads (e.g. user_id). Key should be 32 random bytes for AES-256; any other length is run through HKDF-SHA256 to derive a 32-byte key. Use KeyFromString(APP_KEY) for strings.

func (*CookieStoreImpl) Destroy

func (s *CookieStoreImpl) Destroy(ctx context.Context, id string) error

func (*CookieStoreImpl) Get

func (s *CookieStoreImpl) Get(ctx context.Context, id string) (map[string]any, error)

func (*CookieStoreImpl) Set

func (s *CookieStoreImpl) Set(ctx context.Context, id string, data map[string]any, maxAge time.Duration) (string, error)

type DatabaseStore

type DatabaseStore struct {
	// contains filtered or unexported fields
}

DatabaseStore stores sessions in a database table. Create the table with: CREATE TABLE sessions (id VARCHAR(64) PRIMARY KEY, payload TEXT, expires_at DATETIME);

func NewDatabaseStore

func NewDatabaseStore(db *lucid.DB, table string) *DatabaseStore

NewDatabaseStore creates a database-backed session store.

func (*DatabaseStore) Destroy

func (s *DatabaseStore) Destroy(ctx context.Context, id string) error

func (*DatabaseStore) EnsureTable

func (s *DatabaseStore) EnsureTable() error

EnsureTable creates the sessions table if it doesn't exist.

func (*DatabaseStore) Get

func (s *DatabaseStore) Get(ctx context.Context, id string) (map[string]any, error)

func (*DatabaseStore) Set

func (s *DatabaseStore) Set(ctx context.Context, id string, data map[string]any, maxAge time.Duration) (string, error)

type MemoryStore

type MemoryStore struct {
	// contains filtered or unexported fields
}

MemoryStore stores sessions in memory. Sessions are lost on restart. Use for development or single-instance apps.

func NewMemoryStore

func NewMemoryStore() *MemoryStore

NewMemoryStore creates an in-memory session store.

func (*MemoryStore) Destroy

func (s *MemoryStore) Destroy(ctx context.Context, id string) error

func (*MemoryStore) Get

func (s *MemoryStore) Get(ctx context.Context, id string) (map[string]any, error)

func (*MemoryStore) Set

func (s *MemoryStore) Set(ctx context.Context, id string, data map[string]any, maxAge time.Duration) (string, error)

type RedisStore

type RedisStore struct {
	// contains filtered or unexported fields
}

RedisStore stores sessions in Redis. Suitable for multi-instance deployments. Values are JSON-encoded maps with a TTL based on maxAge.

func NewRedisStore

func NewRedisStore(client *redis.Client) *RedisStore

NewRedisStore creates a Redis-backed session store with the default prefix.

func NewRedisStoreWithPrefix

func NewRedisStoreWithPrefix(client *redis.Client, prefix string) *RedisStore

NewRedisStoreWithPrefix creates a Redis-backed session store with a custom key prefix.

func (*RedisStore) Destroy

func (s *RedisStore) Destroy(ctx context.Context, id string) error

Destroy removes the session.

func (*RedisStore) Get

func (s *RedisStore) Get(ctx context.Context, id string) (map[string]any, error)

Get retrieves session data by ID. Returns nil map if not found or on error.

func (*RedisStore) Set

func (s *RedisStore) Set(ctx context.Context, id string, data map[string]any, maxAge time.Duration) (string, error)

Set stores session data with the given maxAge. If id is empty, a new ID is generated.

type Session

type Session struct {
	// contains filtered or unexported fields
}

Session provides access to session data.

func FromContext

func FromContext(ctx context.Context) *Session

FromContext returns the session data from the request context. Returns nil if session middleware was not used.

func (*Session) Delete

func (s *Session) Delete(key string)

Delete removes a key from the session.

func (*Session) Get

func (s *Session) Get(key string) any

Get returns a value from the session.

func (*Session) GetFlash

func (s *Session) GetFlash(key string) any

GetFlash retrieves a value from the session and deletes it immediately.

func (*Session) Regenerate

func (s *Session) Regenerate()

Regenerate regenerates the session ID (for security after login).

func (*Session) Set

func (s *Session) Set(key string, val any)

Set stores a value in the session.

func (*Session) SetFlash

func (s *Session) SetFlash(key string, val any)

SetFlash stores a value in the session that will be deleted after the next GetFlash.

type SessionRecord

type SessionRecord struct {
	ID        string    `gorm:"primaryKey;size:64"`
	Payload   string    `gorm:"type:text"`
	ExpiresAt time.Time `gorm:"index"`
}

SessionRecord is the DB model for sessions.

type Store

type Store interface {
	// Get retrieves session data by ID. Returns nil map if not found.
	Get(ctx context.Context, id string) (map[string]any, error)
	// Set stores session data. id may be empty for new sessions; returns the session ID.
	Set(ctx context.Context, id string, data map[string]any, maxAge time.Duration) (string, error)
	// Destroy removes the session.
	Destroy(ctx context.Context, id string) error
}

Store persists session data. Implementations: MemoryStore, CookieStore, DatabaseStore, RedisStore.

Jump to

Keyboard shortcuts

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