session

package
v0.6.1 Latest Latest
Warning

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

Go to latest
Published: Mar 13, 2026 License: MIT Imports: 19 Imported by: 0

Documentation

Overview

Package session provides session management functionality, including secure cookies, session storage, and session encoding/decoding using CBOR and secure cookies.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DecodeMulti

func DecodeMulti(name, value string, dst any, codecs ...Codec) error

func EncodeMulti

func EncodeMulti(name string, value any, codecs ...Codec) (string, error)

func GenerateRandomKey

func GenerateRandomKey(length int) []byte

func NewCookie

func NewCookie(name, value string, options *Options, now func() time.Time) *http.Cookie

NewCookie returns an http.Cookie with the options set. It also sets the Expires field calculated based on the MaxAge value, for Internet Explorer compatibility.

Types

type CBOREncoder

type CBOREncoder struct{}

CBOREncoder encodes and decodes values using CBOR.

func (CBOREncoder) Deserialize

func (e CBOREncoder) Deserialize(src []byte, dst any) error

Deserialize decodes a value using CBOR.

func (CBOREncoder) Serialize

func (e CBOREncoder) Serialize(src any) ([]byte, error)

Serialize encodes a value using CBOR.

type Codec

type Codec interface {
	Encode(name string, value any) (string, error)
	Decode(name, value string, dst any) error
}

func CodecsFromPairs

func CodecsFromPairs(now func() time.Time, keyPairs ...[]byte) []Codec

type CookieStore

type CookieStore struct {
	Now     func() time.Time
	Options *Options
	Codecs  []Codec
}

CookieStore stores sessions using secure cookies.

func NewCookieStore

func NewCookieStore(now func() time.Time, keyPairs ...[]byte) *CookieStore

func (*CookieStore) Get

func (s *CookieStore) Get(r *http.Request, name string) (*Session, error)

func (*CookieStore) MaxAge

func (s *CookieStore) MaxAge(age int)

func (*CookieStore) New

func (s *CookieStore) New(r *http.Request, name string) (*Session, error)

New returns a session for the given name without adding it to the registry.

The difference between New() and Get() is that calling New() twice will decode the session data twice, while Get() registers and reuses the same decoded session after the first call.

func (*CookieStore) Save

func (s *CookieStore) Save(r *http.Request, w http.ResponseWriter, session *Session) error

Save adds a single session to the response.

type Options

type Options struct {
	Path        string
	Domain      string
	MaxAge      int
	Secure      bool
	HTTPOnly    bool
	Partitioned bool
	SameSite    http.SameSite
}

type Registry

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

Registry stores sessions used during a request.

func GetRegistry

func GetRegistry(r *http.Request) (*Registry, error)

func (*Registry) Get

func (s *Registry) Get(store Store, name string) (session *Session, err error)

Get registers and returns a session for the given name and session store.

It returns a new session if there are no sessions registered for the name.

type SecureCookie

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

SecureCookie encodes and decodes authenticated and optionally encrypted cookie values.

func NewSecureCookie

func NewSecureCookie(hashKey, blockKey []byte, now func() time.Time) *SecureCookie

func (*SecureCookie) BlockFunc

func (s *SecureCookie) BlockFunc(f func([]byte) (cipher.Block, error)) *SecureCookie

func (*SecureCookie) Decode

func (s *SecureCookie) Decode(name, value string, dst any) error

func (*SecureCookie) Encode

func (s *SecureCookie) Encode(name string, value any) (string, error)

func (*SecureCookie) MaxAge

func (s *SecureCookie) MaxAge(value int) *SecureCookie

MaxAge restricts the maximum age, in seconds, for the cookie value.

Default is 86400 * 30. Set it to 0 for no restriction.

type Serializer

type Serializer interface {
	Serialize(src any) ([]byte, error)
	Deserialize(src []byte, dst any) error
}

Serializer provides an interface for providing custom serializers for cookie values.

type Session

type Session struct {
	Options *Options
	ID      string

	Values Values
	IsNew  bool
	// contains filtered or unexported fields
}

func NewSession

func NewSession(store Store, name string) *Session

func (*Session) Name

func (s *Session) Name() string

Name returns the name used to register the session.

type Store

type Store interface {
	Get(r *http.Request, name string) (*Session, error)
	New(r *http.Request, name string) (*Session, error)
	Save(r *http.Request, w http.ResponseWriter, s *Session) error
}

type Values

type Values struct {
	User      warnly.User      `cbor:"user"`
	OIDCState warnly.OIDCState `cbor:"oidc"`
}

Jump to

Keyboard shortcuts

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