Documentation
¶
Index ¶
- Variables
- type Session
- func (s *Session) Clear()
- func (s *Session) Delete(key string)
- func (s *Session) ExpiresAt() time.Time
- func (s *Session) ExpiresSoon(delta time.Duration) bool
- func (s *Session) Extend(expiresAt time.Time)
- func (s *Session) Get(key string) (any, bool)
- func (s *Session) HasChanged() bool
- func (s *Session) HasExpired() bool
- func (s *Session) HasRegenerated() bool
- func (s *Session) OriginalSessionID() string
- func (s *Session) Put(key string, value any)
- func (s *Session) Regenerate(expiresAt time.Time) error
- func (s *Session) SessionID() string
Constants ¶
This section is empty.
Variables ¶
var Key = sessionKey{}
Key is the context key used to store and retrieve the session from a context.Context.
Functions ¶
This section is empty.
Types ¶
type Session ¶
type Session struct {
// OriginalID is the session ID assigned when the session was first created.
// This value remains constant even if the session is regenerated.
OriginalID string `json:"original_id" yaml:"original_id" toml:"original_id"`
// ID is the current session identifier. This may differ from OriginalID if the session
// has been regenerated (e.g., for security purposes after authentication).
ID string `json:"id" yaml:"id" toml:"id"`
// Expiration is the time at which the session will expire and be considered invalid.
Expiration time.Time `json:"expires_at" yaml:"expires_at" toml:"expires_at"`
// Storage holds the session data as key-value pairs. Keys are strings and values
// can be of any type.
Storage map[string]any `json:"storage" yaml:"storage" toml:"storage"`
// contains filtered or unexported fields
}
Session represents an HTTP session with data storage, expiration management, and change tracking. It maintains the current and original session IDs, supports key-value storage, and tracks modifications for persistence decisions. Access to the session is protected by a mutex to ensure thread-safe operations.
func NewSession ¶
NewSession creates a new session with the specified expiration time and initial storage data. It generates a new UUID v7 for both the original and current session IDs. The session is marked as changed to ensure it is persisted on first save. It returns an error if the UUID generation fails.
func (*Session) Clear ¶
func (s *Session) Clear()
Clear removes all data from the session storage while keeping the session itself intact. The session ID and expiration time remain unchanged. This operation marks the session as changed.
func (*Session) Delete ¶
Delete removes a value from the session storage by key. If the key does not exist, this operation is a no-op. This operation marks the session as changed.
func (*Session) ExpiresAt ¶
ExpiresAt returns the time at which the session will expire and be considered invalid.
func (*Session) ExpiresSoon ¶
ExpiresSoon checks whether the session will expire within the specified duration from the current time. This is useful for triggering session renewal prompts or warnings before the session actually expires.
func (*Session) Extend ¶
Extend updates the session's expiration time to the specified time. This is useful for extending the session's lifetime during active use. This operation marks the session as changed.
func (*Session) Get ¶
Get retrieves a value from the session storage by key. It returns the value and a boolean indicating whether the key exists in the session.
func (*Session) HasChanged ¶
HasChanged returns true if the session data has been modified since it was loaded. This is useful for determining whether the session needs to be persisted to storage.
func (*Session) HasExpired ¶
HasExpired checks whether the session has already expired by comparing the current time against the expiration time.
func (*Session) HasRegenerated ¶
HasRegenerated returns true if the session has been regenerated, meaning the current session ID differs from the original session ID. This is useful for determining whether an updated session identifier should be sent to the client.
func (*Session) OriginalSessionID ¶
OriginalSessionID returns the session identifier that was assigned when the session was initially created. This value does not change even if the session is regenerated.
func (*Session) Put ¶
Put stores a value in the session storage associated with the given key. If the key already exists, its value is overwritten. This operation marks the session as changed.
func (*Session) Regenerate ¶
Regenerate generates a new session ID and updates the expiration time. This is commonly used for security purposes such as preventing session fixation attacks after user authentication. The original session ID is preserved and can be retrieved via OriginalSessionID. This operation marks the session as changed. It returns an error if ID generation fails.