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 ¶
- type ClientInfo
- type Manager
- func (m *Manager) Create(ctx context.Context, principal authentication.Principal, client ClientInfo) (Session, string, error)
- func (m *Manager) Revoke(ctx context.Context, id string) error
- func (m *Manager) RevokePrincipal(ctx context.Context, principalID string) error
- func (m *Manager) Rotate(ctx context.Context, id string, refresh string) (rotated Session, nextRefresh string, err error)
- func (m *Manager) Validate(ctx context.Context, id string) (Session, error)
- type MemoryRevocationStore
- func (s *MemoryRevocationStore) IsPrincipalRevoked(ctx context.Context, id string) (bool, error)
- func (s *MemoryRevocationStore) IsSessionRevoked(ctx context.Context, id string) (bool, error)
- func (s *MemoryRevocationStore) RevokePrincipal(ctx context.Context, id string) error
- func (s *MemoryRevocationStore) RevokeSession(ctx context.Context, id string) error
- type MemoryStore
- func (s *MemoryStore) CompareAndSwap(ctx context.Context, id string, expectedVersion uint64, fence uint64, ...) error
- func (s *MemoryStore) Create(ctx context.Context, session Session) error
- func (s *MemoryStore) Get(ctx context.Context, id string) (Session, error)
- func (s *MemoryStore) ListByPrincipal(ctx context.Context, principalID string) ([]Session, error)
- type Options
- type RevocationStore
- type Session
- type Store
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ClientInfo ¶
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 ¶
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) RevokePrincipal ¶
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.
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 ¶
IsPrincipalRevoked checks a principal tombstone.
func (*MemoryRevocationStore) IsSessionRevoked ¶
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) ListByPrincipal ¶
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.