session

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Dec 17, 2025 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
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

func NewSession(expiresAt time.Time, storage map[string]any) (*Session, error)

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

func (s *Session) Delete(key string)

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

func (s *Session) ExpiresAt() time.Time

ExpiresAt returns the time at which the session will expire and be considered invalid.

func (*Session) ExpiresSoon

func (s *Session) ExpiresSoon(delta time.Duration) bool

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

func (s *Session) Extend(expiresAt time.Time)

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

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

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

func (s *Session) HasChanged() bool

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

func (s *Session) HasExpired() bool

HasExpired checks whether the session has already expired by comparing the current time against the expiration time.

func (*Session) HasRegenerated

func (s *Session) HasRegenerated() bool

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

func (s *Session) OriginalSessionID() string

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

func (s *Session) Put(key string, value any)

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

func (s *Session) Regenerate(expiresAt time.Time) error

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.

func (*Session) SessionID

func (s *Session) SessionID() string

SessionID returns the current session identifier. This may differ from the original session ID if the session has been regenerated.

Directories

Path Synopsis
driver

Jump to

Keyboard shortcuts

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