rate

package
v0.5.0 Latest Latest
Warning

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

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

Documentation

Overview

Package rate provides internal primitives used to build Redis-backed rate limit keys, errors, and limiter behavior for security-sensitive authentication workflows.

Window semantics

Fixed-window counters: INCR + conditional EXPIRE on first hit. Key prefix:

  • rl:login:fail:{tenant}:{identifier} — login failure counter

What this package must NOT do

  • Implement domain-specific policies (those live in internal/limiters).
  • Be imported outside the goAuth module.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrRateLimited is an exported constant or variable used by the authentication engine.
	ErrRateLimited = errors.New("rate limited")
	// ErrRedisUnavailable is an exported constant or variable used by the authentication engine.
	ErrRedisUnavailable = errors.New("redis unavailable")
)

Functions

This section is empty.

Types

type Config

type Config struct {
	EnableLoginFailureLimiter bool
	MaxLoginAttempts          int
	LoginCooldownDuration     time.Duration
	// WindowMode selects the counting algorithm (zero value = fixed window).
	WindowMode window.Mode
}

Config holds rate limiter tuning parameters.

type Limiter

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

Limiter enforces identifier-scoped login failure limits using Redis counters.

func New

func New(redisClient redis.UniversalClient, cfg Config) *Limiter

New creates a rate Limiter backed by the given Redis client.

func (*Limiter) CheckLogin

func (l *Limiter) CheckLogin(ctx context.Context, tenantID, identifier string) error

CheckLogin checks whether the identifier is within the login attempt budget. Returns an error if rate-limited.

func (*Limiter) GetLoginAttempts

func (l *Limiter) GetLoginAttempts(ctx context.Context, tenantID, identifier string) (int, error)

GetLoginAttempts returns the current attempt counter for an identifier. Missing keys return zero and do not reveal account existence. In sliding mode this is the weighted count over the current window.

func (*Limiter) IncrementLogin

func (l *Limiter) IncrementLogin(ctx context.Context, tenantID, identifier string) error

IncrementLogin records a failed login attempt for the identifier.

func (*Limiter) ResetLogin

func (l *Limiter) ResetLogin(ctx context.Context, tenantID, identifier string) error

ResetLogin clears the failed-login counter for the identifier. Called after successful login or password change.

Jump to

Keyboard shortcuts

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