Documentation
¶
Index ¶
- Constants
- Variables
- func Middleware(driver contract.SessionDriver) framework.Middleware
- func MiddlewareWith(driver contract.SessionDriver, options MiddlewareOptions) framework.Middleware
- type CacheDriver
- type CacheDriverOptions
- type MiddlewareOptions
- 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) MarkAsUnchanged()
- func (s *Session) OriginalSessionID() string
- func (s *Session) Put(key string, value any)
- func (s *Session) Regenerate() error
- func (s *Session) SessionID() string
Constants ¶
const DefaultCookie = "cosmos.session"
const DefaultExpirationDelta = 15 * time.Minute
const DefaultTTL = 2 * time.Hour
Variables ¶
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
type CacheDriverOptions ¶ added in v0.8.0
type CacheDriverOptions struct {
// contains filtered or unexported fields
}
type MiddlewareOptions ¶ added in v0.8.0
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 ¶
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) 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 ¶
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.