sessiontoken

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2026 License: Apache-2.0 Imports: 7 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 sessiontoken manages opaque server-side sessions.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrCacheMiss     = errors.New("session cache miss")
	ErrCacheSync     = errors.New("session cache synchronization failed")
	ErrConflict      = errors.New("session credential conflict")
	ErrInactive      = errors.New("session is inactive")
	ErrInvalidConfig = errors.New("invalid session configuration")
	ErrInvalidRecord = errors.New("invalid session record")
	ErrInvalidToken  = token.ErrInvalidToken
	ErrNotFound      = errors.New("session not found")
)

Functions

This section is empty.

Types

type Cache

type Cache interface {
	Get(ctx context.Context, tokenHash TokenHash) (Record, error)
	Set(ctx context.Context, record Record, ttl time.Duration) error
	Delete(ctx context.Context, tokenHash TokenHash) error
}

Cache is an optional bounded-TTL acceleration layer.

type Config

type Config struct {
	Lifetime  time.Duration
	CacheTTL  time.Duration
	Extension *ExtensionConfig
	Now       func() time.Time
}

type ExtensionConfig

type ExtensionConfig struct {
	ExtendAfter      time.Duration
	AbsoluteLifetime time.Duration
}

type Issued

type Issued struct {
	Token  string
	Record Record
}

Issued contains a new raw token and its server-side record.

type Manager

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

func NewManager

func NewManager(store Store, cache Cache, config Config) (*Manager, error)

func (*Manager) Create

func (manager *Manager) Create(ctx context.Context, subjectID string) (Issued, error)

Create commits a new session to durable storage before returning its raw credential. A cache population failure does not change durable success.

func (*Manager) List

func (manager *Manager) List(ctx context.Context, subjectID string) ([]Record, error)

List returns the durable sessions for a subject. Inactive sessions are included so callers can show session history and decide what to remove.

func (*Manager) Resolve

func (manager *Manager) Resolve(ctx context.Context, rawToken string) (Record, error)

Resolve validates a raw credential and resolves active server-side state. Cache failure or invalid cached state falls back to the durable authority.

func (*Manager) Revoke

func (manager *Manager) Revoke(ctx context.Context, rawToken string) error

Revoke durably invalidates a credential, then removes its cached state.

func (*Manager) RevokeAll

func (manager *Manager) RevokeAll(ctx context.Context, subjectID string) error

RevokeAll durably invalidates every session for a subject, then removes cached state for the affected sessions. Durable success is retained when cache invalidation fails and ErrCacheSync is returned for retry handling.

func (*Manager) RevokeByID

func (manager *Manager) RevokeByID(ctx context.Context, subjectID, sessionID string) error

RevokeByID revokes a session only when it belongs to the supplied subject.

func (*Manager) Rotate

func (manager *Manager) Rotate(ctx context.Context, rawToken string) (Issued, error)

Rotate atomically replaces an active durable credential. A returned Issued value remains usable when the accompanying error is ErrCacheSync; that error means the durable rotation succeeded but old cache state needs retryable invalidation.

type Record

type Record struct {
	ID         string
	SubjectID  string
	TokenHash  TokenHash
	CreatedAt  time.Time
	ExpiresAt  time.Time
	ExtendedAt *time.Time
	RevokedAt  *time.Time
}

Record is the server-side state associated with an opaque session token.

func (Record) ActiveAt

func (record Record) ActiveAt(at time.Time) bool

ActiveAt reports whether the record can authenticate at the supplied time. Expiry is exclusive: a record is inactive at its ExpiresAt boundary.

type Store

type Store interface {
	Create(ctx context.Context, record Record) error
	FindByTokenHash(ctx context.Context, tokenHash TokenHash) (Record, error)
	ListBySubject(ctx context.Context, subjectID string) ([]Record, error)
	Extend(ctx context.Context, tokenHash TokenHash, extendedAt, expiresAt time.Time) (Record, error)
	Rotate(ctx context.Context, current TokenHash, replacement Record, rotatedAt time.Time) error
	Revoke(ctx context.Context, tokenHash TokenHash, revokedAt time.Time) error
	RevokeAll(ctx context.Context, subjectID string, revokedAt time.Time) ([]Record, error)
}

Store is the durable authority for sessions.

Extend only increases the expiry of an active record. Rotate atomically invalidates current and creates replacement. Revoke is idempotent.

type TokenHash

type TokenHash = token.Hash

TokenHash is the durable representation of a session token.

func HashToken

func HashToken(rawToken string) (TokenHash, error)

HashToken validates and hashes a raw token for server-side lookup.

Jump to

Keyboard shortcuts

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