Documentation
¶
Index ¶
- Constants
- func GenerateSessionID() string
- type Config
- type Manager
- type ManagerOpt
- type SessionData
- func (s *SessionData) Delete(key string)
- func (s *SessionData) DeleteIdentity()
- func (s *SessionData) Flash(value any)
- func (s *SessionData) FlashString(value string)
- func (s *SessionData) Get(key string) (any, bool)
- func (s *SessionData) GetBool(key string) (bool, bool)
- func (s *SessionData) GetFlash() (interface{}, bool)
- func (s *SessionData) GetFlashString() (string, bool)
- func (s *SessionData) GetIdentity() (any, bool)
- func (s *SessionData) GetInt(key string) (int, bool)
- func (s *SessionData) GetString(key string) (string, bool)
- func (s *SessionData) Has(key string) bool
- func (s *SessionData) HasIdentity() bool
- func (s *SessionData) Set(key string, value any)
- func (s *SessionData) SetIdentity(identity any)
- type Store
Constants ¶
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") )
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
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) 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
Regenerate regenerates the session ID to prevent session fixation
type ManagerOpt ¶ added in v0.6.0
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 (*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 (*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) StartCleanup ¶
func (s *Store) StartCleanup()
StartCleanup is a no-op for Client as Client handles expiration