Documentation
¶
Index ¶
- Variables
- type AEAD
- type KV
- type Manager
- type ManagerOpts
- type Session
- func (s *Session) Delete()
- func (s *Session) FlashIsError() bool
- func (s *Session) FlashMessage() string
- func (s *Session) Get(key string) any
- func (s *Session) GetAll() map[string]any
- func (s *Session) HasFlash() bool
- func (s *Session) Reset()
- func (s *Session) Set(key string, value any)
- func (s *Session) SetAll(data map[string]any)
- func (s *Session) SetFlashError(message string)
- func (s *Session) SetFlashMessage(message string)
- type SessionCookieOpts
- type TestResult
Constants ¶
This section is empty.
Variables ¶
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 ¶
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
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 ¶
FromContext returns the Session from the given context. It panics if no session exists in the context.
func MustFromContext ¶
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 ¶
FlashIsError indicates that the flash message is an error.
func (*Session) FlashMessage ¶
FlashMessage returns the current flash message and clears it.
func (*Session) Get ¶
Get returns the value for the given key from the session. If the key doesn't exist, it returns nil.
func (*Session) Reset ¶
func (s *Session) Reset()
Reset rotates the session ID to avoid session fixation.
func (*Session) SetFlashError ¶
func (*Session) SetFlashMessage ¶
type SessionCookieOpts ¶
SessionCookieOpts configures cookie behavior for sessions
type TestResult ¶
type TestResult struct {
// contains filtered or unexported fields
}
func TestContext ¶
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