session

package
v0.1.7 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2024 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package session presents a lazy context that manages session state with native key rotation support.

Index

Constants

View Source
const MaximumCookieSize = 4096

A browser should be able to accept at least 300 cookies with a maximum size of 4096 bytes, as stipulated by RFC 2109 (#6.3), RFC 2965 (#5.3), and RFC 6265.

Variables

This section is empty.

Functions

func Address

func Address(ctx context.Context) string

func Expires

func Expires(ctx context.Context) time.Time

func ID

func ID(ctx context.Context) string

func New

func New(withOptions ...Option) (func(http.Handler) http.Handler, error)

func Read

func Read(ctx context.Context, view func(Session) error) (err error)

func Role

func Role(ctx context.Context) string

func SetRole

func SetRole(ctx context.Context, name string) error

func SetUserID

func SetUserID(ctx context.Context, id string) error

func SetValue

func SetValue(ctx context.Context, key string, value any) error

func Sign

func Sign(secret *secrets.Secret, b []byte) []byte

func TraceID

func TraceID(ctx context.Context) string

func UserID

func UserID(ctx context.Context) string

func Validate

func Validate(secret *secrets.Secret, b []byte) bool

Validate return true if given bytes begin with hex-encoded signature that matches HMAC sum of [Key.Label] followed by the rest of the data.

func Value

func Value(ctx context.Context, key string) any

func Write

func Write(ctx context.Context, update func(Session) error) (err error)

Types

type CookieCodec

type CookieCodec interface {
	WriteCookie(http.ResponseWriter, string, time.Time) error
	ReadCookie(*http.Request) string
}

func NewStrictCookieCodec

func NewStrictCookieCodec(name, path string) CookieCodec

type Error

type Error uint8
const (
	ErrNoSessionInContext Error = iota
	ErrLargeCookie
)

func (Error) Error

func (e Error) Error() string

func (Error) HyperTextStatusCode added in v0.1.6

func (e Error) HyperTextStatusCode() int

type Factory

type Factory func() map[string]any

type Option

type Option func(*options) error

func WithCookieCodec

func WithCookieCodec(c CookieCodec) Option

func WithDefaultCookieCodec

func WithDefaultCookieCodec() Option

func WithDefaultExpiry

func WithDefaultExpiry() Option

func WithDefaultFactory

func WithDefaultFactory() Option

func WithDefaultName

func WithDefaultName() Option

func WithDefaultRotationContext

func WithDefaultRotationContext() Option

func WithDefaultTokenizer

func WithDefaultTokenizer() Option

func WithExpiry

func WithExpiry(d time.Duration) Option

func WithFactory

func WithFactory(f Factory) Option

func WithName

func WithName(name string) Option

func WithRotationContext

func WithRotationContext(ctx context.Context) Option

func WithTokenizer

func WithTokenizer(t Tokenizer) Option

type Session

type Session interface {
	ID() string
	TraceID() string
	Address() string
	UserID() string
	SetUserID(string)
	Role() string
	SetRole(string)
	Expires() time.Time
	IsExpired() bool
	IsNew() bool
	Get(string) any
	Int(string) int
	Int64(string) int64
	Float32(string) float32
	Float64(string) float64
	Set(string, any)
	Reset()
}

type SlogHandler

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

func NewSlogHandler

func NewSlogHandler(h slog.Handler) *SlogHandler

func (*SlogHandler) Enabled

func (h *SlogHandler) Enabled(ctx context.Context, level slog.Level) bool

func (*SlogHandler) Handle

func (h *SlogHandler) Handle(ctx context.Context, r slog.Record) error

func (*SlogHandler) WithAttrs

func (h *SlogHandler) WithAttrs(attrs []slog.Attr) slog.Handler

func (*SlogHandler) WithGroup

func (h *SlogHandler) WithGroup(name string) slog.Handler

type Tokenizer

type Tokenizer interface {
	Encode(any) (string, error)
	Decode(any, string) error
}

func NewTokenizer

func NewTokenizer(withOptions ...secrets.Option) Tokenizer

Directories

Path Synopsis
Package secrets provides a synchronizable set of keys backed by a key-value store.
Package secrets provides a synchronizable set of keys backed by a key-value store.
Package token provides [Authority]s that issue and validate access tokens.
Package token provides [Authority]s that issue and validate access tokens.
gorilla
Package gorilla couples [securecookie.SecureCookie] token encoder with [secrets.Rotation].
Package gorilla couples [securecookie.SecureCookie] token encoder with [secrets.Rotation].
jwt
Package jwt provides [token.Authority] with secure JSON Web Tokens issuer defaults.
Package jwt provides [token.Authority] with secure JSON Web Tokens issuer defaults.

Jump to

Keyboard shortcuts

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