session

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultCookie = "cosmos.session"
View Source
const DefaultExpirationDelta = 15 * time.Minute
View Source
const DefaultTTL = 2 * time.Hour

Variables

View Source
var ErrCacheDriverInvalidType = errors.New("invalid cache type")

Functions

func Middleware added in v0.8.0

func Middleware(driver contract.SessionDriver) framework.Middleware

func MiddlewareWith added in v0.8.0

func MiddlewareWith(driver contract.SessionDriver, options MiddlewareOptions) framework.Middleware

Types

type CacheDriver added in v0.8.0

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

func NewCacheDriver added in v0.8.0

func NewCacheDriver(cache contract.Cache) *CacheDriver

func NewCacheDriverWith added in v0.8.0

func NewCacheDriverWith(cache contract.Cache, options CacheDriverOptions) *CacheDriver

func (*CacheDriver) Delete added in v0.8.0

func (d *CacheDriver) Delete(ctx context.Context, id string) error

func (*CacheDriver) Get added in v0.8.0

func (d *CacheDriver) Get(ctx context.Context, id string) (contract.Session, error)

func (*CacheDriver) Save added in v0.8.0

func (d *CacheDriver) Save(ctx context.Context, session contract.Session, ttl time.Duration) error

type CacheDriverOptions added in v0.8.0

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

type MiddlewareOptions added in v0.8.0

type MiddlewareOptions struct {
	Name            string
	Path            string
	Domain          string
	Secure          bool
	SameSite        http.SameSite
	Partitioned     bool
	TTL             time.Duration
	ExpirationDelta time.Duration
	Key             any
}

type Session

type Session struct {
	// 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) MarkAsUnchanged added in v0.7.0

func (s *Session) MarkAsUnchanged()

MarkAsUnchanged sets the session as if nothing has changed, therefore avoiding saving the session when the request finishes.

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() 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.

Jump to

Keyboard shortcuts

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