session

package
v0.1.0-preview.4 Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2026 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package session provides typed, encrypted, stateless HTTP sessions with explicit key rotation and secure cookie defaults.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotFound means the request carries no configured session cookie.
	ErrNotFound = errors.New("session not found")
	// ErrInvalid means a cookie is malformed, unauthenticated, or violates the
	// configured bounded session contract.
	ErrInvalid = errors.New("session is invalid")
	// ErrExpired means an authenticated session reached its embedded expiry.
	ErrExpired = errors.New("session expired")
)

Functions

This section is empty.

Types

type Key

type Key struct {
	ID     string
	Secret []byte
}

Key is one AES-256-GCM key. The first configured key seals new sessions; remaining keys only decrypt sessions during bounded rotation.

type Manager

type Manager[T any] struct {
	// contains filtered or unexported fields
}

Manager seals and loads one exact session value type. It is safe for concurrent use.

Example
type claims struct {
	Subject string `json:"subject"`
}
manager, err := New[claims](Options{
	Name: "__Host-orders",
	Keys: []Key{{
		ID:     "2026-07",
		Secret: []byte("0123456789abcdef0123456789abcdef"),
	}},
})
if err != nil {
	panic(err)
}
recorder := httptest.NewRecorder()
if err := manager.Save(
	recorder,
	claims{Subject: "account-41"},
	sessionNow,
); err != nil {
	panic(err)
}
fmt.Println(len(recorder.Header().Values("Set-Cookie")))
Output:
1

func New

func New[T any](options Options) (*Manager[T], error)

New validates and freezes a typed session manager without reading process state or generating a token.

func (*Manager[T]) Clear

func (manager *Manager[T]) Clear(writer http.ResponseWriter) error

Clear appends a cookie deletion header with the manager's exact security and scope attributes.

func (*Manager[T]) Load

func (manager *Manager[T]) Load(
	request *http.Request,
	now time.Time,
) (Record[T], error)

Load authenticates and strictly decodes the configured request cookie.

func (*Manager[T]) Save

func (manager *Manager[T]) Save(
	writer http.ResponseWriter,
	value T,
	now time.Time,
) error

Save seals value and appends one Set-Cookie header. It never logs or returns plaintext session data.

type Options

type Options struct {
	Name          string
	Path          string
	Domain        string
	Lifetime      time.Duration
	ClockSkew     time.Duration
	SameSite      http.SameSite
	AllowInsecure bool
	Keys          []Key
}

Options configures one immutable session manager.

type Record

type Record[T any] struct {
	Value        T
	IssuedAt     time.Time
	ExpiresAt    time.Time
	NeedsRefresh bool
}

Record is one authenticated session value and its server-issued metadata.

Jump to

Keyboard shortcuts

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