session

package
v0.0.0-...-99e730b Latest Latest
Warning

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

Go to latest
Published: Mar 13, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrSessionNotFound    = errors.New("session not found")
	ErrSessionExpired     = errors.New("session expired")
	ErrSessionLockFailed  = errors.New("failed to acquire session lock")
	ErrSessionLockTimeout = errors.New("session lock timeout")
	ErrBackendUnavailable = errors.New("session backend unavailable")
	ErrInvalidSession     = errors.New("invalid session data")
)

Common errors for session operations

Functions

func SetGlobalSessionService

func SetGlobalSessionService(service *DistributedSessionService)

SetGlobalSessionService sets the global session service instance

Types

type DistributedSessionConfig

type DistributedSessionConfig struct {
	// Enabled determines if distributed sessions are active
	Enabled bool `json:"enabled"`

	// BackendType specifies the primary backend (redis, mongodb, memory)
	BackendType SessionBackendType `json:"backend_type"`

	// FallbackType specifies the fallback backend if primary fails
	FallbackType SessionBackendType `json:"fallback_type"`

	// SessionTTL is the default session duration
	SessionTTL time.Duration `json:"session_ttl"`

	// TokenTTL is the default token duration
	TokenTTL time.Duration `json:"token_ttl"`

	// LockTTL is the timeout for acquiring session locks
	LockTTL time.Duration `json:"lock_ttl"`

	// AutoExtend determines if sessions should be auto-extended on access
	AutoExtend bool `json:"auto_extend"`

	// ExtendThreshold is the remaining time before auto-extending
	ExtendThreshold time.Duration `json:"extend_threshold"`

	// InstanceID identifies this IAC instance
	InstanceID string `json:"instance_id"`

	// Backend-specific configuration
	BackendConfig *SessionBackendConfig `json:"backend_config"`
}

DistributedSessionConfig holds configuration for distributed session management

func DefaultDistributedSessionConfig

func DefaultDistributedSessionConfig() *DistributedSessionConfig

DefaultDistributedSessionConfig returns default configuration

type DistributedSessionService

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

DistributedSessionService manages sessions across distributed IAC instances

func GetGlobalSessionService

func GetGlobalSessionService() *DistributedSessionService

GetGlobalSessionService returns the global session service instance

func NewDistributedSessionService

func NewDistributedSessionService(config *DistributedSessionConfig) (*DistributedSessionService, error)

NewDistributedSessionService creates a new distributed session service

func (*DistributedSessionService) Close

func (s *DistributedSessionService) Close() error

Close shuts down the session service

func (*DistributedSessionService) CreateSession

func (s *DistributedSessionService) CreateSession(ctx context.Context, userID, username, loginName, clientID string) (*models.DistributedSession, error)

CreateSession creates a new session for a user

func (*DistributedSessionService) DeleteSession

func (s *DistributedSessionService) DeleteSession(ctx context.Context, sessionID string) error

DeleteSession removes a session

func (*DistributedSessionService) DeleteUserSessions

func (s *DistributedSessionService) DeleteUserSessions(ctx context.Context, userID string) error

DeleteUserSessions removes all sessions for a user (logout everywhere)

func (*DistributedSessionService) ExtendSession

func (s *DistributedSessionService) ExtendSession(ctx context.Context, sessionID string, duration time.Duration) error

ExtendSession extends a session's TTL

func (*DistributedSessionService) GetBackendName

func (s *DistributedSessionService) GetBackendName() string

GetBackendName returns the name of the current backend

func (*DistributedSessionService) GetInstanceID

func (s *DistributedSessionService) GetInstanceID() string

GetInstanceID returns the current instance ID

func (*DistributedSessionService) GetSession

GetSession retrieves a session by ID

func (*DistributedSessionService) GetSessionData

func (s *DistributedSessionService) GetSessionData(ctx context.Context, sessionID, key string) (interface{}, error)

GetSessionData retrieves a value from the session data

func (*DistributedSessionService) GetUserSessions

func (s *DistributedSessionService) GetUserSessions(ctx context.Context, userID string) ([]*models.DistributedSession, error)

GetUserSessions retrieves all sessions for a user

func (*DistributedSessionService) IsEnabled

func (s *DistributedSessionService) IsEnabled() bool

IsEnabled returns whether distributed sessions are enabled

func (*DistributedSessionService) LockSession

func (s *DistributedSessionService) LockSession(ctx context.Context, sessionID string) (func(), error)

LockSession acquires a distributed lock for a session

func (*DistributedSessionService) MigrateSession

func (s *DistributedSessionService) MigrateSession(ctx context.Context, sessionID, fromInstance, reason string) error

MigrateSession records a session migration between instances

func (*DistributedSessionService) Ping

Ping checks if the session service is healthy

func (*DistributedSessionService) SetSessionData

func (s *DistributedSessionService) SetSessionData(ctx context.Context, sessionID, key string, value interface{}) error

SetSessionData sets a key-value pair in the session data

func (*DistributedSessionService) SetToken

func (s *DistributedSessionService) SetToken(ctx context.Context, sessionID, token string) error

SetToken associates a token hash with a session

func (*DistributedSessionService) Stats

func (s *DistributedSessionService) Stats() map[string]interface{}

Stats returns statistics about the session service

func (*DistributedSessionService) UpdateSession

func (s *DistributedSessionService) UpdateSession(ctx context.Context, session *models.DistributedSession) error

UpdateSession updates a session

func (*DistributedSessionService) ValidateSession

func (s *DistributedSessionService) ValidateSession(ctx context.Context, sessionID string) (*models.DistributedSession, error)

ValidateSession validates a session and returns user info

func (*DistributedSessionService) ValidateToken

ValidateToken validates a token and returns the session

type FallbackSessionBackend

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

FallbackSessionBackend wraps a primary backend with automatic fallback

func NewFallbackSessionBackend

func NewFallbackSessionBackend(primary, fallback SessionBackend, config *SessionBackendConfig) *FallbackSessionBackend

NewFallbackSessionBackend creates a backend with fallback support

func (*FallbackSessionBackend) Close

func (f *FallbackSessionBackend) Close() error

func (*FallbackSessionBackend) Delete

func (f *FallbackSessionBackend) Delete(ctx context.Context, sessionID string) error

func (*FallbackSessionBackend) DeleteByUserID

func (f *FallbackSessionBackend) DeleteByUserID(ctx context.Context, userID string) error

func (*FallbackSessionBackend) Exists

func (f *FallbackSessionBackend) Exists(ctx context.Context, sessionID string) (bool, error)

func (*FallbackSessionBackend) Extend

func (f *FallbackSessionBackend) Extend(ctx context.Context, sessionID string, ttl time.Duration) error

func (*FallbackSessionBackend) Get

func (*FallbackSessionBackend) GetByToken

func (f *FallbackSessionBackend) GetByToken(ctx context.Context, tokenHash string) (*models.DistributedSession, error)

func (*FallbackSessionBackend) GetByUserID

func (f *FallbackSessionBackend) GetByUserID(ctx context.Context, userID string) ([]*models.DistributedSession, error)

func (*FallbackSessionBackend) Lock

func (f *FallbackSessionBackend) Lock(ctx context.Context, sessionID string, ttl time.Duration) (func(), error)

func (*FallbackSessionBackend) Name

func (f *FallbackSessionBackend) Name() string

func (*FallbackSessionBackend) Ping

func (*FallbackSessionBackend) Set

type MemorySessionBackend

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

MemorySessionBackend implements SessionBackend using in-memory storage This is suitable for single-instance deployments or development/testing

func NewMemorySessionBackend

func NewMemorySessionBackend(config *SessionBackendConfig) (*MemorySessionBackend, error)

NewMemorySessionBackend creates a new in-memory session backend

func (*MemorySessionBackend) Close

func (m *MemorySessionBackend) Close() error

Close stops the cleanup goroutine

func (*MemorySessionBackend) Delete

func (m *MemorySessionBackend) Delete(ctx context.Context, sessionID string) error

Delete removes a session

func (*MemorySessionBackend) DeleteByUserID

func (m *MemorySessionBackend) DeleteByUserID(ctx context.Context, userID string) error

DeleteByUserID deletes all sessions for a user

func (*MemorySessionBackend) Exists

func (m *MemorySessionBackend) Exists(ctx context.Context, sessionID string) (bool, error)

Exists checks if a session exists

func (*MemorySessionBackend) Extend

func (m *MemorySessionBackend) Extend(ctx context.Context, sessionID string, ttl time.Duration) error

Extend extends the session TTL

func (*MemorySessionBackend) Get

Get retrieves a session by ID

func (*MemorySessionBackend) GetByToken

func (m *MemorySessionBackend) GetByToken(ctx context.Context, tokenHash string) (*models.DistributedSession, error)

GetByToken retrieves a session by token hash

func (*MemorySessionBackend) GetByUserID

func (m *MemorySessionBackend) GetByUserID(ctx context.Context, userID string) ([]*models.DistributedSession, error)

GetByUserID retrieves all sessions for a user

func (*MemorySessionBackend) Lock

func (m *MemorySessionBackend) Lock(ctx context.Context, sessionID string, ttl time.Duration) (func(), error)

Lock acquires a lock for a session

func (*MemorySessionBackend) Name

func (m *MemorySessionBackend) Name() string

Name returns the backend name

func (*MemorySessionBackend) Ping

Ping checks if the backend is available (always true for memory)

func (*MemorySessionBackend) Set

Set stores or updates a session

func (*MemorySessionBackend) Stats

func (m *MemorySessionBackend) Stats() map[string]interface{}

Stats returns statistics about the session store (for monitoring)

type MongoSessionBackend

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

MongoSessionBackend implements SessionBackend using MongoDB

func NewMongoSessionBackend

func NewMongoSessionBackend(config *SessionBackendConfig) (*MongoSessionBackend, error)

NewMongoSessionBackend creates a new MongoDB session backend

func (*MongoSessionBackend) Close

func (m *MongoSessionBackend) Close() error

Close closes the backend (MongoDB connection is managed globally)

func (*MongoSessionBackend) Delete

func (m *MongoSessionBackend) Delete(ctx context.Context, sessionID string) error

Delete removes a session

func (*MongoSessionBackend) DeleteByUserID

func (m *MongoSessionBackend) DeleteByUserID(ctx context.Context, userID string) error

DeleteByUserID deletes all sessions for a user

func (*MongoSessionBackend) Exists

func (m *MongoSessionBackend) Exists(ctx context.Context, sessionID string) (bool, error)

Exists checks if a session exists

func (*MongoSessionBackend) Extend

func (m *MongoSessionBackend) Extend(ctx context.Context, sessionID string, ttl time.Duration) error

Extend extends the session TTL

func (*MongoSessionBackend) Get

Get retrieves a session by ID

func (*MongoSessionBackend) GetByToken

func (m *MongoSessionBackend) GetByToken(ctx context.Context, tokenHash string) (*models.DistributedSession, error)

GetByToken retrieves a session by token hash

func (*MongoSessionBackend) GetByUserID

func (m *MongoSessionBackend) GetByUserID(ctx context.Context, userID string) ([]*models.DistributedSession, error)

GetByUserID retrieves all sessions for a user

func (*MongoSessionBackend) Lock

func (m *MongoSessionBackend) Lock(ctx context.Context, sessionID string, ttl time.Duration) (func(), error)

Lock acquires a distributed lock for a session (using in-memory locks for MongoDB backend) Note: For true distributed locking with MongoDB, consider using a separate locks collection with findAndModify operations or a dedicated locking service

func (*MongoSessionBackend) Name

func (m *MongoSessionBackend) Name() string

Name returns the backend name

func (*MongoSessionBackend) Ping

Ping checks if MongoDB is available

func (*MongoSessionBackend) Set

Set stores or updates a session

type RedisSessionBackend

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

RedisSessionBackend implements SessionBackend using Redis

func NewRedisSessionBackend

func NewRedisSessionBackend(config *SessionBackendConfig) (*RedisSessionBackend, error)

NewRedisSessionBackend creates a new Redis session backend

func (*RedisSessionBackend) Close

func (r *RedisSessionBackend) Close() error

Close closes the Redis connection pool

func (*RedisSessionBackend) Delete

func (r *RedisSessionBackend) Delete(ctx context.Context, sessionID string) error

Delete removes a session

func (*RedisSessionBackend) DeleteByUserID

func (r *RedisSessionBackend) DeleteByUserID(ctx context.Context, userID string) error

DeleteByUserID deletes all sessions for a user

func (*RedisSessionBackend) Exists

func (r *RedisSessionBackend) Exists(ctx context.Context, sessionID string) (bool, error)

Exists checks if a session exists

func (*RedisSessionBackend) Extend

func (r *RedisSessionBackend) Extend(ctx context.Context, sessionID string, ttl time.Duration) error

Extend extends the session TTL

func (*RedisSessionBackend) Get

Get retrieves a session by ID

func (*RedisSessionBackend) GetByToken

func (r *RedisSessionBackend) GetByToken(ctx context.Context, tokenHash string) (*models.DistributedSession, error)

GetByToken retrieves a session by token hash

func (*RedisSessionBackend) GetByUserID

func (r *RedisSessionBackend) GetByUserID(ctx context.Context, userID string) ([]*models.DistributedSession, error)

GetByUserID retrieves all sessions for a user

func (*RedisSessionBackend) Lock

func (r *RedisSessionBackend) Lock(ctx context.Context, sessionID string, ttl time.Duration) (func(), error)

Lock acquires a distributed lock for a session

func (*RedisSessionBackend) Name

func (r *RedisSessionBackend) Name() string

Name returns the backend name

func (*RedisSessionBackend) Ping

Ping checks if Redis is available

func (*RedisSessionBackend) Set

Set stores or updates a session

type SessionBackend

type SessionBackend interface {
	// Get retrieves a session by ID
	Get(ctx context.Context, sessionID string) (*models.DistributedSession, error)

	// Set stores or updates a session
	Set(ctx context.Context, session *models.DistributedSession) error

	// Delete removes a session
	Delete(ctx context.Context, sessionID string) error

	// Exists checks if a session exists
	Exists(ctx context.Context, sessionID string) (bool, error)

	// Extend extends the session TTL
	Extend(ctx context.Context, sessionID string, ttl time.Duration) error

	// GetByUserID retrieves all sessions for a user
	GetByUserID(ctx context.Context, userID string) ([]*models.DistributedSession, error)

	// DeleteByUserID deletes all sessions for a user
	DeleteByUserID(ctx context.Context, userID string) error

	// GetByToken retrieves a session by token hash
	GetByToken(ctx context.Context, tokenHash string) (*models.DistributedSession, error)

	// Lock acquires a distributed lock for a session (returns unlock function)
	Lock(ctx context.Context, sessionID string, ttl time.Duration) (func(), error)

	// Ping checks if the backend is available
	Ping(ctx context.Context) error

	// Close closes the backend connection
	Close() error

	// Name returns the backend name for logging
	Name() string
}

SessionBackend defines the interface for distributed session storage

type SessionBackendConfig

type SessionBackendConfig struct {
	// Common settings
	TTL     time.Duration `json:"ttl"`
	LockTTL time.Duration `json:"lock_ttl"`

	// Redis settings
	RedisURL       string `json:"redis_url"`
	RedisDB        int    `json:"redis_db"`
	RedisPassword  string `json:"redis_password"`
	RedisPoolSize  int    `json:"redis_pool_size"`
	RedisKeyPrefix string `json:"redis_key_prefix"`

	// MongoDB settings
	MongoCollection string `json:"mongo_collection"`
	MongoDatabase   string `json:"mongo_database"`

	// Instance identification
	InstanceID string `json:"instance_id"`
}

SessionBackendConfig holds configuration for session backends

func DefaultSessionBackendConfig

func DefaultSessionBackendConfig() *SessionBackendConfig

DefaultSessionBackendConfig returns default configuration

type SessionBackendFactory

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

SessionBackendFactory creates session backends based on type

func NewSessionBackendFactory

func NewSessionBackendFactory(config *SessionBackendConfig) *SessionBackendFactory

NewSessionBackendFactory creates a new factory

func (*SessionBackendFactory) Create

Create creates a session backend of the specified type

func (*SessionBackendFactory) CreateWithFallback

func (f *SessionBackendFactory) CreateWithFallback(primary, fallback SessionBackendType) (SessionBackend, error)

CreateWithFallback creates a primary backend with a fallback

type SessionBackendType

type SessionBackendType string

SessionBackendType represents the type of session backend

const (
	BackendTypeRedis   SessionBackendType = "redis"
	BackendTypeMongoDB SessionBackendType = "mongodb"
	BackendTypeMemory  SessionBackendType = "memory"
)

Jump to

Keyboard shortcuts

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