session

package
v0.0.0-20260608 Latest Latest
Warning

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

Go to latest
Published: Jun 8, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenerateKey

func GenerateKey() ([]byte, error)

GenerateKey generates a random 32-byte encryption key.

Types

type ArrayDriver

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

ArrayDriver is an in-memory session driver for testing.

func NewArrayDriver

func NewArrayDriver() *ArrayDriver

NewArrayDriver creates a new in-memory session driver.

func (*ArrayDriver) Count

func (d *ArrayDriver) Count() int

Count returns the number of active sessions.

func (*ArrayDriver) Destroy

func (d *ArrayDriver) Destroy(id string) error

func (*ArrayDriver) Flush

func (d *ArrayDriver) Flush()

Flush removes all sessions (useful in testing).

func (*ArrayDriver) Read

func (d *ArrayDriver) Read(id string) (map[string]any, error)

func (*ArrayDriver) Write

func (d *ArrayDriver) Write(id string, data map[string]any) error

type CookieDriver

type CookieDriver struct {
}

CookieDriver implements the session.Store interface by storing payloads in cookies. Note: In a real implementation, this requires access to the HTTP Request/Response writers to set the cookie. For the architecture of GoW, the Store interface abstracts reading/writing, so a Cookie driver needs contextual access or relies on the Manager mapping it correctly.

func NewCookieDriver

func NewCookieDriver() *CookieDriver

NewCookieDriver creates a new CookieDriver.

func (*CookieDriver) Destroy

func (d *CookieDriver) Destroy(id string) error

func (*CookieDriver) Read

func (d *CookieDriver) Read(id string) (map[string]any, error)

func (*CookieDriver) Write

func (d *CookieDriver) Write(id string, data map[string]any) error

type DatabaseDriver

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

DatabaseDriver implements session storage using a database table.

func NewDatabaseDriver

func NewDatabaseDriver(db *sql.DB) *DatabaseDriver

NewDatabaseDriver creates a new database session driver.

func (*DatabaseDriver) Destroy

func (d *DatabaseDriver) Destroy(id string) error

Destroy removes a session from the database.

func (*DatabaseDriver) GarbageCollect

func (d *DatabaseDriver) GarbageCollect(maxLifetime time.Duration) error

GarbageCollect removes expired sessions.

func (*DatabaseDriver) Read

func (d *DatabaseDriver) Read(id string) (map[string]any, error)

Read reads session data by ID.

func (*DatabaseDriver) Write

func (d *DatabaseDriver) Write(id string, data map[string]any) error

Write writes session data to the database.

type EncryptedStore

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

EncryptedStore wraps a Store with AES-GCM encryption.

func NewEncryptedStore

func NewEncryptedStore(store Store, key []byte) (*EncryptedStore, error)

NewEncryptedStore creates a new encrypted session store. The key must be 16, 24, or 32 bytes for AES-128, AES-192, or AES-256.

func (*EncryptedStore) Destroy

func (e *EncryptedStore) Destroy(id string) error

func (*EncryptedStore) Read

func (e *EncryptedStore) Read(id string) (map[string]any, error)

func (*EncryptedStore) Write

func (e *EncryptedStore) Write(id string, data map[string]any) error

type EnhancedCookieDriver

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

EnhancedCookieDriver implements cookie-based session storage with encryption.

func NewEnhancedCookieDriver

func NewEnhancedCookieDriver(secret []byte) *EnhancedCookieDriver

NewEnhancedCookieDriver creates a new enhanced cookie driver.

func (*EnhancedCookieDriver) Destroy

func (d *EnhancedCookieDriver) Destroy(id string) error

Destroy removes a session.

func (*EnhancedCookieDriver) FromJSON

func (d *EnhancedCookieDriver) FromJSON(id, jsonStr string) error

FromJSON deserializes session data from JSON cookie value.

func (*EnhancedCookieDriver) GetSessionCount

func (d *EnhancedCookieDriver) GetSessionCount() int

GetSessionCount returns the number of active sessions.

func (*EnhancedCookieDriver) PurgeExpired

func (d *EnhancedCookieDriver) PurgeExpired(maxAge time.Duration) int

PurgeExpired removes sessions older than the given duration.

func (*EnhancedCookieDriver) Read

func (d *EnhancedCookieDriver) Read(id string) (map[string]any, error)

Read loads session data from the in-memory store (simulating cookie decoding).

func (*EnhancedCookieDriver) ToJSON

func (d *EnhancedCookieDriver) ToJSON(id string) (string, error)

ToJSON serializes session data to JSON for cookie storage.

func (*EnhancedCookieDriver) Write

func (d *EnhancedCookieDriver) Write(id string, data map[string]any) error

Write persists session data to the in-memory store (simulating cookie encoding).

type FileDriver

type FileDriver struct {
	Path string
}

FileDriver implements the session.Store interface using local files.

func NewFileDriver

func NewFileDriver(path string) *FileDriver

NewFileDriver creates a new FileDriver.

func (*FileDriver) Destroy

func (d *FileDriver) Destroy(id string) error

func (*FileDriver) Read

func (d *FileDriver) Read(id string) (map[string]any, error)

func (*FileDriver) Write

func (d *FileDriver) Write(id string, data map[string]any) error

type Manager

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

Manager manages the current session state.

func NewManager

func NewManager(store Store, id string) *Manager

NewManager creates a new Session Manager.

func (*Manager) All

func (m *Manager) All() map[string]any

All returns all session data.

func (*Manager) Flash

func (m *Manager) Flash(key string, value any)

Flash stores a value in the session that will be available for the next request.

func (*Manager) FlashInput

func (m *Manager) FlashInput(input map[string]any)

FlashInput flashes the current request input to the session.

func (*Manager) Flush

func (m *Manager) Flush()

Flush clears all session data.

func (*Manager) Forget

func (m *Manager) Forget(key string)

Forget removes a value from the session.

func (*Manager) Get

func (m *Manager) Get(key string) any

Get retrieves a value from the session.

func (*Manager) Has

func (m *Manager) Has(key string) bool

Has checks if a key exists in the session.

func (*Manager) ID

func (m *Manager) ID() string

ID returns the current session ID.

func (*Manager) Intended

func (m *Manager) Intended(defaultURL ...string) string

Intended stores the intended URL the user was trying to access. After login, call Intended() to get the stored URL and redirect there.

func (*Manager) Keep

func (m *Manager) Keep(keys ...string)

Keep keeps specific flash data for an additional request.

func (*Manager) Old

func (m *Manager) Old(key string, defaultValue any) any

Old returns an old input value flashed from the previous request.

func (*Manager) PreviousURL

func (m *Manager) PreviousURL() string

PreviousURL stores the URL the user came from (typically set by middleware).

func (*Manager) Pull

func (m *Manager) Pull(key string, defaultValue any) any

Pull removes a value from the session and returns it.

func (*Manager) Put

func (m *Manager) Put(key string, value any)

Put stores a value in the session.

func (*Manager) Reflash

func (m *Manager) Reflash()

Reflash keeps all current flash data for an additional request.

func (*Manager) Regenerate

func (m *Manager) Regenerate() error

Regenerate generates a new session ID and migrates the data.

func (*Manager) Save

func (m *Manager) Save() error

Save writes the session data to the store.

func (*Manager) SetIntendedURL

func (m *Manager) SetIntendedURL(url string)

SetIntendedURL stores the intended URL for post-login redirect.

func (*Manager) SetPreviousURL

func (m *Manager) SetPreviousURL(url string)

SetPreviousURL stores the previous URL in the session.

func (*Manager) Start

func (m *Manager) Start() error

Start loads the session data from the store and ages flash data.

type RedisStore

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

RedisStore is a session driver backed by Redis.

func NewRedisStore

func NewRedisStore(client *redis.Client, prefix string, ttl time.Duration) *RedisStore

NewRedisStore creates a new Redis session store.

func (*RedisStore) Destroy

func (s *RedisStore) Destroy(id string) error

Destroy removes the session data by ID.

func (*RedisStore) Read

func (s *RedisStore) Read(id string) (map[string]any, error)

Read retrieves the session data by ID.

func (*RedisStore) Write

func (s *RedisStore) Write(id string, data map[string]any) error

Write saves the session data by ID.

type Store

type Store interface {
	// Read retrieves the session data by ID.
	Read(id string) (map[string]any, error)
	// Write saves the session data by ID.
	Write(id string, data map[string]any) error
	// Destroy removes the session data by ID.
	Destroy(id string) error
}

Store represents a session storage driver.

Jump to

Keyboard shortcuts

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