authlimit

package
v0.1.23 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2026 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Overview

Package authlimit throttles failed authentication attempts by source address.

It exists because two listeners needed the same thing and one of them had nothing. The FHIR server got a limiter when it got authentication; the console login had none at all, and a measurement showed twenty password guesses a second sustained against the administrator account with no delay and no lockout. At that rate a weak password is gone overnight and the only trace is a log full of identical warnings.

Deliberately not a general-purpose rate limiter. It throttles failures, not requests: a busy legitimate client is never affected, and the only thing that accumulates is being wrong.

Index

Constants

View Source
const (
	DefaultThreshold = 5
	DefaultMaxDelay  = 5 * time.Minute
	DefaultMemory    = 30 * time.Minute
)

Defaults. Five failures before any delay, doubling to a five minute maximum.

Five rather than three, because a person genuinely mistyping a password twice and then getting it right on the third attempt is ordinary, and locking them out would make the limiter the thing people complain about instead of the thing that protects them.

Variables

This section is empty.

Functions

This section is empty.

Types

type Limiter

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

Limiter tracks failures per key.

The key is a source address. It is never a username: throttling by username lets anybody lock out a named account by guessing at it deliberately, which turns a protection into a way to deny an administrator access at the moment they need it most.

func New

func New() *Limiter

New builds a limiter with the default thresholds.

func NewWith

func NewWith(threshold int, maxDelay, memory time.Duration, now func() time.Time) *Limiter

NewWith builds a limiter with explicit thresholds, for tests.

func (*Limiter) Blocked

func (l *Limiter) Blocked(key string) (time.Duration, bool)

Blocked reports whether this key must wait, and for how long.

func (*Limiter) Failed

func (l *Limiter) Failed(key string) time.Duration

Failed records a failed attempt and returns how long the key is now blocked for.

func (*Limiter) Failures

func (l *Limiter) Failures(key string) int

Failures reports the current count, for a log line or a test.

func (*Limiter) Succeeded

func (l *Limiter) Succeeded(key string)

Succeeded clears a key's history.

Cleared rather than decremented. Somebody who has just proved they hold the right credential is not who this is for, and leaving a count behind would eventually throttle a busy legitimate client that mistyped once an hour ago.

The tradeoff is real and worth naming: a guesser who happens to find a valid credential gets their slate wiped. That matters much less than it sounds, because by then they are in.

func (*Limiter) Tracked

func (l *Limiter) Tracked() int

Tracked reports how many keys are held, so a test can prove the map does not grow without bound.

Jump to

Keyboard shortcuts

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