ratelimit

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package ratelimit defines the rate-limit integration point of Auth-All. Auth-All does not require a specific backend.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Decider added in v0.3.0

type Decider interface {
	Limiter
	// Decide counts one attempt and returns the decision.
	Decide(ctx context.Context, key Key) (Decision, error)
}

Decider is an optional limiter interface that names a retry time.

A limiter that implements it drives the Retry-After header of a refused request. A limiter that implements Limiter only keeps the v1 behavior, and Auth-All sends Retry-After: 60.

type Decision added in v0.3.0

type Decision struct {
	// Allowed reports whether the attempt can proceed.
	Allowed bool
	// RetryAfter is the time until the next attempt can succeed. It is zero
	// when the attempt is allowed.
	RetryAfter time.Duration
}

Decision is the answer of a Decider.

type Key

type Key struct {
	Operation Operation
	IP        string
	Email     string
	UserID    string
	Provider  string
}

Key identifies one rate-limited attempt. Fields are set when relevant.

type Limiter

type Limiter interface {
	Allow(ctx context.Context, key Key) (bool, error)
}

Limiter decides whether one attempt can proceed.

type LimiterFunc

type LimiterFunc func(ctx context.Context, key Key) (bool, error)

LimiterFunc adapts a function to the Limiter interface.

func (LimiterFunc) Allow

func (f LimiterFunc) Allow(ctx context.Context, k Key) (bool, error)

Allow implements Limiter.

type Memory

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

Memory is an in-process fixed-window limiter.

Memory is for local development and tests only. It is not sufficient for a distributed production deployment because each process keeps its own counters.

func NewMemory

func NewMemory(limit int, window time.Duration) *Memory

NewMemory returns an in-process limiter that allows limit attempts per window.

func (*Memory) Allow

func (m *Memory) Allow(_ context.Context, k Key) (bool, error)

Allow implements Limiter.

type Operation

type Operation string

Operation names a sensitive flow.

const (
	OpSignIn           Operation = "sign-in"
	OpSignUp           Operation = "sign-up"
	OpPasswordForgot   Operation = "password-forgot"
	OpEmailVerify      Operation = "email-verification-send"
	OpMagicLinkRequest Operation = "magic-link-request"
	OpPasswordChange   Operation = "password-change"
	OpEmailChange      Operation = "email-change"
	OpUserDelete       Operation = "user-delete"
	OpTOTP             Operation = "totp"
)

Sensitive operations.

type Rule added in v0.3.0

type Rule struct {
	// Operation names the flow that the rule counts.
	Operation Operation
	// Scope names the counted subject.
	Scope Scope
	// Limit is the number of accepted attempts in one window.
	Limit int
	// Window is the length of the counting window.
	Window time.Duration
}

Rule is one limit of one operation.

func DefaultSignInRules added in v0.3.0

func DefaultSignInRules() []Rule

DefaultSignInRules returns the default rules of the sign-in flow. They allow 5 attempts for each email in 15 minutes, and 20 attempts for each client address in 1 minute.

type Scope added in v0.3.0

type Scope string

Scope names the subject that one rule counts.

const (
	// ScopeIP counts the attempts of one client address. An IPv6 address
	// counts per /64 block.
	ScopeIP Scope = "ip"
	// ScopeEmail counts the attempts for one email address. The store keeps a
	// digest of the address and never the address.
	ScopeEmail Scope = "email"
)

Supported scopes.

Directories

Path Synopsis
Package storelimit is a rate limiter that keeps its counters in the Auth-All database.
Package storelimit is a rate limiter that keeps its counters in the Auth-All database.

Jump to

Keyboard shortcuts

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