session

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2025 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DefaultSessionCookieName is the default cookie name for storing sessions
	DefaultSessionCookieName = "blueprint_session"

	// DefaultSessionExpiration is the default expiration time for sessions (30 minutes)
	DefaultSessionExpiration = 1800

	// DefaultSessionIdleTimeout is the default idle timeout for sessions (15 minutes)
	DefaultSessionIdleTimeout = 900

	// DefaultSecure sets the Secure flag on session cookies
	DefaultSecure = true

	// DefaultHttpOnly sets the HttpOnly flag on session cookies
	DefaultHttpOnly = true

	// DefaultSameSite sets the SameSite policy for session cookies
	DefaultSameSite = int(http.SameSiteStrictMode)

	// DefaultCleanupInterval sets how often the session cleanup runs
	DefaultCleanupInterval = 300 // 5 min

	// ContextSessionKey is the key used to store the session in the gin.Context
	ContextSessionKey = "session"

	ErrInvalidExpirationSeconds      = utils.Error("session expiration seconds must be a positive integer")
	ErrInvalidIdleTimeoutSeconds     = utils.Error("session idle timeout seconds must be a positive integer")
	ErrInvalidSameSite               = utils.Error("invalid sameSite value")
	ErrInvalidCleanupIntervalSeconds = utils.Error("session cleanup interval seconds must be a positive integer")
	ErrSessionNotFound               = utils.Error("session not found")
	ErrSessionExpired                = utils.Error("session expired")
)
View Source
const SessionIdentityKey = "_identity"

Variables

This section is empty.

Functions

func GenerateSessionID added in v0.5.0

func GenerateSessionID() string

GenerateSessionID creates a random session ID (public version for external use)

Types

type Config

type Config struct {
	CookieName             string                         `json:"cookieName"`             // CookieName is the name of the cookie used to store the session ID
	ExpirationSeconds      int                            `json:"expirationSeconds"`      // expiration is the maximum lifetime of a session
	IdleTimeoutSeconds     int                            `json:"idleTimeoutSeconds"`     // IdleTimeoutSeconds is the maximum time a session can be inactive
	Secure                 bool                           `json:"secure"`                 // Secure sets the Secure flag on cookies (should be true in production)
	HttpOnly               bool                           `json:"httpOnly"`               // HttpOnly sets the HttpOnly flag on cookies (should be true)
	SameSite               int                            `json:"sameSite"`               // SameSite sets the SameSite policy for cookies
	Domain                 string                         `json:"domain"`                 // Domain sets the domain for the cookie
	Path                   string                         `json:"path"`                   // Path sets the path for the cookie
	EncryptionKey          secure.DefaultCredentialConfig `json:"encryptionKey"`          // Optional encryption key to encrypt cookie data; if defined, cookie data is encrypted
	CleanupIntervalSeconds int                            `json:"cleanupIntervalSeconds"` // CleanupIntervalSeconds sets how often the session cleanup runs
}

Config holds configuration for the session store

func NewConfig

func NewConfig() *Config

NewConfig returns a default session configuration

func (*Config) Validate

func (c *Config) Validate() error

type Manager added in v0.6.0

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

Manager manages sessions and provides middleware for Gin

func NewManager

func NewManager(config *Config, opts ...ManagerOpt) (*Manager, error)

NewManager creates a new session manager

func (*Manager) Clear added in v0.6.0

func (m *Manager) Clear(c *gin.Context)

Clear removes the session

func (*Manager) Middleware added in v0.6.0

func (m *Manager) Middleware() gin.HandlerFunc

Middleware returns a Gin middleware for session management

func (*Manager) Regenerate added in v0.6.0

func (m *Manager) Regenerate(c *gin.Context)

Regenerate regenerates the session ID to prevent session fixation

func (*Manager) Shutdown added in v0.6.0

func (m *Manager) Shutdown()

Shutdown gracefully stops the session Manager

type ManagerOpt added in v0.6.0

type ManagerOpt func(*Manager) error

func ManagerWithLogger added in v0.6.0

func ManagerWithLogger(log *log.Logger) ManagerOpt

func ManagerWithStore added in v0.6.0

func ManagerWithStore(store *Store) ManagerOpt

type SessionData

type SessionData struct {
	Values       map[string]any
	LastAccessed time.Time
	Created      time.Time
	ID           string
}

SessionData represents the session data stored in a backend

func Get

func Get(c *gin.Context) *SessionData

Get returns the session from the context

func (*SessionData) Delete added in v0.5.0

func (s *SessionData) Delete(key string)

Delete removes a value from the session

func (*SessionData) DeleteIdentity added in v0.6.0

func (s *SessionData) DeleteIdentity()

DeleteIdentity delete user identity

func (*SessionData) Flash added in v0.5.0

func (s *SessionData) Flash(value any)

Flash sets a one-time message in the session The message will be available for the current request and the next request

func (*SessionData) FlashString added in v0.5.0

func (s *SessionData) FlashString(value string)

FlashString sets a one-time string message in the session

func (*SessionData) Get added in v0.5.0

func (s *SessionData) Get(key string) (any, bool)

Get retrieves a value from the session

func (*SessionData) GetBool added in v0.5.0

func (s *SessionData) GetBool(key string) (bool, bool)

GetBool retrieves a bool value from the session

func (*SessionData) GetFlash added in v0.5.0

func (s *SessionData) GetFlash() (interface{}, bool)

GetFlash gets a flash message from the session and removes it

func (*SessionData) GetFlashString added in v0.5.0

func (s *SessionData) GetFlashString() (string, bool)

GetFlashString gets a flash string message from the session and removes it

func (*SessionData) GetIdentity added in v0.6.0

func (s *SessionData) GetIdentity() (any, bool)

GetIdentity fetch user identity, if any

func (*SessionData) GetInt added in v0.5.0

func (s *SessionData) GetInt(key string) (int, bool)

GetInt retrieves an int value from the session

func (*SessionData) GetString added in v0.5.0

func (s *SessionData) GetString(key string) (string, bool)

GetString retrieves a string value from the session

func (*SessionData) Has added in v0.5.0

func (s *SessionData) Has(key string) bool

Has checks if a key exists in the session

func (*SessionData) HasIdentity added in v0.6.0

func (s *SessionData) HasIdentity() bool

HasIdentity verify if user has identity

func (*SessionData) Set added in v0.5.0

func (s *SessionData) Set(key string, value any)

Set stores a value in the session

func (*SessionData) SetIdentity added in v0.6.0

func (s *SessionData) SetIdentity(identity any)

SetIdentity set user identity

type Store

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

func NewStore

func NewStore(config *Config, backend kv.KV, logger *log.Logger) (*Store, error)

NewStore creates session store if no backend is specified, a memory KV is used

func (*Store) Close

func (s *Store) Close()

Close closes the store

func (*Store) Delete

func (s *Store) Delete(id string) error

Delete removes a session from Client

func (*Store) Generate

func (s *Store) Generate() (*SessionData, string)

Generate creates a new session and returns its ID

func (*Store) Get

func (s *Store) Get(id string) (*SessionData, error)

Get retrieves a session from Client

func (*Store) Set

func (s *Store) Set(id string, session *SessionData) error

Set saves a session

func (*Store) StartCleanup

func (s *Store) StartCleanup()

StartCleanup is a no-op for Client as Client handles expiration

func (*Store) StopCleanup

func (s *Store) StopCleanup()

StopCleanup stops the cleanup goroutine

Jump to

Keyboard shortcuts

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