session

package
v0.0.0-...-5c4fa2e Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2025 License: BSD-3-Clause Imports: 20 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultIdleTimeout = 24 * time.Hour

Functions

This section is empty.

Types

type AEAD

type AEAD interface {
	// Encrypt the plaintext
	Encrypt(plaintext, associatedData []byte) ([]byte, error)

	// Decrypt the cipertext
	Decrypt(ciphertext, associatedData []byte) ([]byte, error)
}

AEAD defines the interface used for securing cookies. It matches the github.com/tink-crypto/tink-go/v2/tink.AEAD interface, and it is reccomended that tink is used to implement this.

func NewXChaPolyAEAD

func NewXChaPolyAEAD(encryptionKey []byte, additionalDecryptionKeys [][]byte) (AEAD, error)

NewXChaPolyAEAD constructs an XChaCha20-Poly1305 AEAD. The keys must be 32 bytes. The encryption key is used as the primary encrypt/decrypt key. Additional decryption-only keys can be provided, to enable key rotation.

type KV

type KV interface {
	Get(_ context.Context, key string) (_ []byte, found bool, _ error)
	Set(_ context.Context, key string, expiresAt time.Time, value []byte) error
	Delete(_ context.Context, key string) error
}

func NewMemoryKV

func NewMemoryKV() KV

type Manager

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

Manager handles both session data and storage.

func NewCookieManager

func NewCookieManager(aead AEAD, opts *ManagerOpts) (*Manager, error)

NewCookieManager creates a new Manager that stores session data in cookies

func NewKVManager

func NewKVManager(kv KV, opts *ManagerOpts) (*Manager, error)

NewKVManager creates a new Manager that stores session data in a KV store

func (*Manager) Wrap

func (m *Manager) Wrap(next http.Handler) http.Handler

Wrap creates middleware that handles session management for each request

type ManagerOpts

type ManagerOpts struct {
	MaxLifetime time.Duration
	IdleTimeout time.Duration
	// Onload is called when a session is retrieved from storage
	Onload func(map[string]any) map[string]any
	// Cookie settings
	CookieOpts *SessionCookieOpts
}

ManagerOpts configures the session manager

type Session

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

Session represents a tracked web session.

func FromContext

func FromContext(ctx context.Context) (*Session, bool)

FromContext returns the Session from the given context. It panics if no session exists in the context.

func MustFromContext

func MustFromContext(ctx context.Context) *Session

MustFromContext returns the Session from the given context. It panics if no session exists in the context.

func (*Session) Delete

func (s *Session) Delete()

Delete marks the session for deletion at the end of the request.

func (*Session) FlashIsError

func (s *Session) FlashIsError() bool

FlashIsError indicates that the flash message is an error.

func (*Session) FlashMessage

func (s *Session) FlashMessage() string

FlashMessage returns the current flash message and clears it.

func (*Session) Get

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

Get returns the value for the given key from the session. If the key doesn't exist, it returns nil.

func (*Session) GetAll

func (s *Session) GetAll() map[string]any

GetAll returns a copy of the session data map.

func (*Session) HasFlash

func (s *Session) HasFlash() bool

HasFlash indicates if there is a flash message.

func (*Session) Reset

func (s *Session) Reset()

Reset rotates the session ID to avoid session fixation.

func (*Session) Set

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

Set sets a single key-value pair in the session and marks it to be saved.

func (*Session) SetAll

func (s *Session) SetAll(data map[string]any)

SetAll sets the entire session data map and marks it to be saved.

func (*Session) SetFlashError

func (s *Session) SetFlashError(message string)

func (*Session) SetFlashMessage

func (s *Session) SetFlashMessage(message string)

type SessionCookieOpts

type SessionCookieOpts struct {
	Name     string
	Path     string
	Insecure bool
	Persist  bool
}

SessionCookieOpts configures cookie behavior for sessions

type TestResult

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

func TestContext

func TestContext(ctx context.Context, s *Session) (context.Context, *TestResult)

TestContext attaches a session to a context, to be used for testing. The returned TestResult can be used to verify the actions against the session. The session is optional, if omitted a new session is created.

func (*TestResult) Deleted

func (t *TestResult) Deleted() bool

func (*TestResult) Reset

func (t *TestResult) Reset() bool

func (*TestResult) Result

func (t *TestResult) Result() map[string]any

func (*TestResult) Saved

func (t *TestResult) Saved() bool

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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