Help us shape the future of Go! Take the 2026 Go Developer Survey

lockout

package
v1.9.0 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package lockout provides account lockout tracking for AuthSome.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type KVTracker

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

KVTracker keeps lockout state in the shared KV store so a lockout holds across every replica.

Per key it keeps a failure counter that expires after the reset window and, once the threshold is crossed, a lock marker that expires with the lockout. Keys are grouped under a prefix (app:identifier) with the client's network as the suffix; an index row per prefix records the suffixes seen so ResetPrefix can clear them without scanning the store.

func NewKVTracker

func NewKVTracker(kv store.KV, opts ...MemoryOption) *KVTracker

NewKVTracker builds a tracker on kv. The options are the same ones the memory tracker takes.

func (*KVTracker) IsLocked

func (t *KVTracker) IsLocked(ctx context.Context, key string) (bool, time.Time, error)

IsLocked reports whether key is locked and until when.

func (*KVTracker) RecordFailure

func (t *KVTracker) RecordFailure(ctx context.Context, key string) (int, error)

RecordFailure counts a failed attempt and sets the lock once the threshold is reached.

func (*KVTracker) Reset

func (t *KVTracker) Reset(ctx context.Context, key string) error

Reset clears the failure count and the lock for a key.

func (*KVTracker) ResetPrefix

func (t *KVTracker) ResetPrefix(ctx context.Context, prefix string) error

ResetPrefix clears every key recorded under prefix.

type MemoryOption

type MemoryOption func(*MemoryTracker)

MemoryOption configures the memory tracker.

func WithLockoutDuration

func WithLockoutDuration(d time.Duration) MemoryOption

WithLockoutDuration sets how long the lockout lasts.

func WithMaxAttempts

func WithMaxAttempts(n int) MemoryOption

WithMaxAttempts sets the max failed attempts before lockout.

func WithResetAfter

func WithResetAfter(d time.Duration) MemoryOption

WithResetAfter sets the inactivity period after which the failure count resets.

type MemoryTracker

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

MemoryTracker is an in-memory account lockout tracker.

func NewMemoryTracker

func NewMemoryTracker(opts ...MemoryOption) *MemoryTracker

NewMemoryTracker creates an in-memory lockout tracker.

func (*MemoryTracker) IsLocked

func (t *MemoryTracker) IsLocked(_ context.Context, key string) (bool, time.Time, error)

IsLocked checks if the key is locked out.

func (*MemoryTracker) RecordFailure

func (t *MemoryTracker) RecordFailure(_ context.Context, key string) (int, error)

RecordFailure records a failed attempt.

func (*MemoryTracker) Reset

func (t *MemoryTracker) Reset(_ context.Context, key string) error

Reset clears the failure count for a key.

func (*MemoryTracker) ResetPrefix

func (t *MemoryTracker) ResetPrefix(_ context.Context, prefix string) error

ResetPrefix clears every key that starts with prefix.

type NoopTracker

type NoopTracker struct{}

NoopTracker is a lockout tracker that never locks out.

func NewNoopTracker

func NewNoopTracker() *NoopTracker

NewNoopTracker creates a no-op lockout tracker.

func (*NoopTracker) IsLocked

func (*NoopTracker) IsLocked(context.Context, string) (bool, time.Time, error)

IsLocked always returns false.

func (*NoopTracker) RecordFailure

func (*NoopTracker) RecordFailure(context.Context, string) (int, error)

RecordFailure always returns 0 attempts.

func (*NoopTracker) Reset

Reset is a no-op.

func (*NoopTracker) ResetPrefix

func (*NoopTracker) ResetPrefix(context.Context, string) error

ResetPrefix is a no-op.

type Tracker

type Tracker interface {
	// RecordFailure records a failed authentication attempt. Returns the
	// current failure count for the key.
	RecordFailure(ctx context.Context, key string) (attempts int, err error)

	// IsLocked checks if the key is currently locked out. Returns true if
	// locked, along with the time the lockout expires.
	IsLocked(ctx context.Context, key string) (locked bool, until time.Time, err error)

	// Reset clears the failure count for a key (called on successful auth).
	Reset(ctx context.Context, key string) error

	// ResetPrefix clears every key that starts with prefix. Lockout keys
	// end in the client's network, so an operator unlocking an account
	// clears all of that account's networks at once.
	ResetPrefix(ctx context.Context, prefix string) error
}

Tracker tracks failed authentication attempts and lockouts.

Jump to

Keyboard shortcuts

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