session

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2026 License: Apache-2.0 Imports: 13 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package session manages durable, server-side login sessions with rotating refresh tokens.

A session has a random 256-bit ID ("sess_" + base64url) and a separate random 256-bit refresh token that is returned exactly once; only its SHA-256 digest is stored. Rotate compares the presented token's digest in constant time and replaces it under a distributed-coordination lease, writing with a compare-and-swap that is fenced by the lease token, so exactly one of several concurrent rotations with the same token succeeds and a reused (stolen) refresh token is detected as authentication.ErrReplay. Sessions and whole principals can be revoked through durable tombstones that Validate and Rotate always consult. Session lifetimes are bounded to 24 hours, and a session stops validating once its principal has expired.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ClientInfo

type ClientInfo struct {
	UserAgent string
	IPAddress string
	DeviceID  string
}

ClientInfo is caller-supplied, bounded session client metadata.

type Manager

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

Manager creates, validates, rotates, and revokes durable sessions. It is safe for concurrent use when its stores and coordinator are.

func NewManager

func NewManager(options Options) (*Manager, error)

NewManager validates options and returns a session manager. It returns authentication.ErrInvalidCredential for invalid options.

func (*Manager) Create

func (m *Manager) Create(ctx context.Context, principal authentication.Principal, client ClientInfo) (Session, string, error)

Create persists a new session and returns the refresh token exactly once.

func (*Manager) Revoke

func (m *Manager) Revoke(ctx context.Context, id string) error

Revoke persists a session tombstone.

func (*Manager) RevokePrincipal

func (m *Manager) RevokePrincipal(ctx context.Context, principalID string) error

RevokePrincipal persists a principal tombstone and revokes known sessions.

func (*Manager) Rotate

func (m *Manager) Rotate(ctx context.Context, id string, refresh string) (rotated Session, nextRefresh string, err error)

Rotate verifies the presented refresh token and atomically replaces it under a coordination lease. It returns the updated session and the new refresh token. A token that does not match the current one returns authentication.ErrReplay; a lost lease returns coordination.ErrLeaseLost.

func (*Manager) Validate

func (m *Manager) Validate(ctx context.Context, id string) (Session, error)

Validate returns the authoritative session if it remains valid and unrevoked. It returns authentication.ErrExpired once the session or its principal has expired and authentication.ErrUnauthenticated for unknown or revoked sessions.

type MemoryRevocationStore

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

MemoryRevocationStore is a concurrency-safe revocation tombstone store.

func NewMemoryRevocationStore

func NewMemoryRevocationStore() *MemoryRevocationStore

NewMemoryRevocationStore returns an empty memory-backed revocation store.

func (*MemoryRevocationStore) IsPrincipalRevoked

func (s *MemoryRevocationStore) IsPrincipalRevoked(ctx context.Context, id string) (bool, error)

IsPrincipalRevoked checks a principal tombstone.

func (*MemoryRevocationStore) IsSessionRevoked

func (s *MemoryRevocationStore) IsSessionRevoked(ctx context.Context, id string) (bool, error)

IsSessionRevoked checks a session tombstone.

func (*MemoryRevocationStore) RevokePrincipal

func (s *MemoryRevocationStore) RevokePrincipal(ctx context.Context, id string) error

RevokePrincipal persists a principal tombstone.

func (*MemoryRevocationStore) RevokeSession

func (s *MemoryRevocationStore) RevokeSession(ctx context.Context, id string) error

RevokeSession persists a session tombstone.

type MemoryStore

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

MemoryStore is a concurrency-safe, process-local Store for tests and single-process callers. Distributed deployments should provide a durable implementation.

func NewMemoryStore

func NewMemoryStore() *MemoryStore

NewMemoryStore returns an empty memory-backed session store.

func (*MemoryStore) CompareAndSwap

func (s *MemoryStore) CompareAndSwap(ctx context.Context, id string, expectedVersion uint64, fence uint64, next Session) error

CompareAndSwap updates a session only when the expected version and fencing token are current.

func (*MemoryStore) Create

func (s *MemoryStore) Create(ctx context.Context, session Session) error

Create stores a new session if its identifier is unused.

func (*MemoryStore) Get

func (s *MemoryStore) Get(ctx context.Context, id string) (Session, error)

Get returns the authoritative session by ID.

func (*MemoryStore) ListByPrincipal

func (s *MemoryStore) ListByPrincipal(ctx context.Context, principalID string) ([]Session, error)

ListByPrincipal returns all sessions for a principal.

type Options

type Options struct {
	// Store is the authoritative session store. Required.
	Store Store
	// Revocations persists session and principal tombstones. Required.
	Revocations RevocationStore
	// Coordinator provides the leases that serialize refresh-token
	// rotation per session. Required.
	Coordinator coordination.Backend
	// ManagerID identifies this manager as lease owner. Required.
	ManagerID string
	// Lifetime is the session lifetime: positive and at most 24 hours.
	Lifetime time.Duration
	// Random supplies session IDs and refresh tokens. Defaults to
	// crypto/rand.Reader; any replacement must be cryptographically secure.
	Random io.Reader
	// Clock returns the current time used for session issue, expiry,
	// rotation and revocation timestamps; its result is converted to UTC.
	// Defaults to time.Now. Lease timing is governed by Coordinator.
	Clock func() time.Time
}

Options configures a Manager.

type RevocationStore

type RevocationStore interface {
	RevokeSession(context.Context, string) error
	RevokePrincipal(context.Context, string) error
	IsSessionRevoked(context.Context, string) (bool, error)
	IsPrincipalRevoked(context.Context, string) (bool, error)
}

RevocationStore persists session and principal tombstones.

type Session

type Session struct {
	ID            string
	Version       uint64
	Principal     authentication.Principal
	CreatedAt     time.Time
	ExpiresAt     time.Time
	LastSeenAt    time.Time
	RefreshDigest [32]byte
	Rotation      uint64
	Client        ClientInfo
}

Session is the authoritative durable session record.

type Store

type Store interface {
	Create(context.Context, Session) error
	Get(context.Context, string) (Session, error)
	CompareAndSwap(context.Context, string, uint64, uint64, Session) error
	ListByPrincipal(context.Context, string) ([]Session, error)
}

Store is the durable, authoritative session store.

Create must fail with authentication.ErrReplay for an existing ID. CompareAndSwap(ctx, id, expectedVersion, fence, next) must replace the session only when its current version equals expectedVersion (otherwise authentication.ErrReplay) and fence is greater than the fence of the last successful swap (otherwise coordination.ErrLeaseLost), atomically.

Jump to

Keyboard shortcuts

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