Module

middleware.auth_rate_limit

Auth-focused rate limiting middleware.

Provides a small in-memory limiter intended for authentication endpoints like login and password reset. Supports pluggable backends for Redis-backed rate limiting across workers.

Classes

RateLimitBackend 1
Protocol for rate limit storage backends.

Protocol for rate limit storage backends.

Methods

check_and_update 5 tuple[bool, int]
Check if allowed, increment, and optionally block. Returns (allowed, retry_afte…
async
async def check_and_update(self, key: str, now: float, *, requests: int, window_seconds: int, block_seconds: int) -> tuple[bool, int]

Check if allowed, increment, and optionally block. Returns (allowed, retry_after).

Parameters
Name Type Description
key
now
requests
window_seconds
block_seconds
Returns
tuple[bool, int]
AuthRateLimitConfig 7
Configuration for auth endpoint rate limiting.

Configuration for auth endpoint rate limiting.

Attributes

Name Type Description
requests int
window_seconds int
block_seconds int
methods tuple[str, ...]
paths tuple[str, ...]
key_header str | None
backend RateLimitBackend | None
_InMemoryRateLimitBackend 2
In-memory rate limit backend.

In-memory rate limit backend.

Methods

check_and_update 5 tuple[bool, int]
async
async def check_and_update(self, key: str, now: float, *, requests: int, window_seconds: int, block_seconds: int) -> tuple[bool, int]
Parameters
Name Type Description
key
now
requests
window_seconds
block_seconds
Returns
tuple[bool, int]
Internal Methods 1
__init__ 0
def __init__(self) -> None
AuthRateLimitMiddleware 2
Rate limiter for authentication-related endpoints.

Rate limiter for authentication-related endpoints.

Methods

Internal Methods 2
__init__ 1
def __init__(self, config: AuthRateLimitConfig | None = None) -> None
Parameters
Name Type Description
config Default:None
__call__ 2 AnyResponse
async
async def __call__(self, request: Request, next: Next) -> AnyResponse
Parameters
Name Type Description
request
next
Returns
AnyResponse

Functions

redis_rate_limit_backend 2 RateLimitBackend
Create a Redis-backed rate limit backend. Requires ``pip install chirp[redis]``.
def redis_rate_limit_backend(redis_url: str, key_prefix: str = 'chirp:ratelimit:') -> RateLimitBackend

Create a Redis-backed rate limit backend. Requirespip install chirp[redis].

Parameters
Name Type Description
redis_url str
key_prefix str Default:'chirp:ratelimit:'
Returns
RateLimitBackend