Skip to content

Auth Redis

Requires the redis extra; not imported by rn_forge.fastapi.

rn_forge.fastapi.auth.redis

Redis implementations of the CSRF token store and the rate limiter.

Requires the redis extra and Redis 6.2 or later. Not imported by rn_forge.fastapi.

RedisCsrfStore(client, *, prefix=DEFAULT_PREFIX)

:class:~rn_forge.fastapi.auth.csrf.CsrfStore over Redis.

Each token is one key written with SET ... EX.

Parameters:

Name Type Description Default
client Redis

The Redis client.

required
prefix str

Prepended to every key.

DEFAULT_PREFIX

delete(key)

Remove the token under key.

get(key)

Return the token under key.

save(key, token, *, ttl)

Store token under key with SET ... EX.

RedisRateLimiter(client, *, prefix=DEFAULT_PREFIX, clock=time.time)

:class:~rn_forge.fastapi.auth.rate_limit.RateLimiter over Redis.

Each window is one key, counted with INCR and given a lifetime of one window with EXPIRE, in one transaction.

Parameters:

Name Type Description Default
client Redis

The Redis client.

required
prefix str

Prepended to every key.

DEFAULT_PREFIX
clock Callable[[], float]

Returns the current time in seconds.

time

hit(scope, subject, rate)

Count one hit of subject in scope.