Skip to content

Session, transaction and replay stores

rn_forge.auth.stores

Server-side records: login sessions and transactions, replayed token IDs and revoked sessions.

InMemoryReplayStore(*, clock=time.monotonic)

:class:ReplayStore held in process memory.

Thread-safe. The records are not shared across processes, so it suits tests and single-process deployments only.

Parameters:

Name Type Description Default
clock Callable[[], float]

Returns the current time in seconds, monotonic.

monotonic

is_sid_revoked(issuer, sid)

See :meth:ReplayStore.is_sid_revoked.

record_once(key, *, ttl)

See :meth:ReplayStore.record_once.

revoke_sid(issuer, sid, *, ttl)

See :meth:ReplayStore.revoke_sid.

InMemorySessionStore(*, clock=_utc_now)

:class:SessionStore held in process memory.

Thread-safe. The sessions are not shared across processes, so it suits tests and single-process deployments only.

Parameters:

Name Type Description Default
clock Callable[[], datetime]

Returns the current time as an aware datetime.

_utc_now

create(session, *, idle_ttl)

See :meth:SessionStore.create.

delete(session_id)

See :meth:SessionStore.delete.

get(session_id)

See :meth:SessionStore.get.

rotate(session_id, *, idle_ttl)

See :meth:SessionStore.rotate.

touch(session_id, *, idle_ttl)

See :meth:SessionStore.touch.

InMemoryTransactionStore(*, clock=time.monotonic)

:class:TransactionStore held in process memory.

Thread-safe. The records are not shared across processes, so it suits tests and single-process deployments only.

Parameters:

Name Type Description Default
clock Callable[[], float]

Returns the current time in seconds, monotonic.

monotonic

consume(transaction_id)

See :meth:TransactionStore.consume.

save(transaction, *, ttl)

See :meth:TransactionStore.save.

ReplayStore

Bases: Protocol

Records that a value was seen once, and which sessions are revoked.

Every record expires after its ttl. Implementations shared by several processes must make :meth:record_once atomic.

is_sid_revoked(issuer, sid)

Return whether the session sid of issuer is currently revoked.

record_once(key, *, ttl)

Record key for ttl unless it is already recorded.

Returns:

Type Description
bool

True when key was newly recorded, False when a record that

bool

has not expired was already present.

revoke_sid(issuer, sid, *, ttl)

Mark the session sid of issuer revoked for ttl.

SessionStore

Bases: Protocol

Holds login sessions under random, unguessable IDs.

A session ends at the first of two times: its idle expiry, which :meth:touch and :meth:rotate extend, and SessionInfo.expires_at, which nothing extends. A session past either is absent.

create(session, *, idle_ttl)

Store session and return its new session ID.

The session is idle-expired idle_ttl from now.

delete(session_id)

Remove the session; absent sessions are ignored.

get(session_id)

Return the session, or None when absent or expired.

Reading does not extend the idle expiry.

rotate(session_id, *, idle_ttl)

Move the session to a new ID and remove the old record.

The idle expiry restarts at idle_ttl from now, capped at expires_at.

Returns:

Type Description
str | None

The new session ID, or None when the session is absent or

str | None

expired.

touch(session_id, *, idle_ttl)

Move the idle expiry to idle_ttl from now, but not past expires_at.

Returns:

Type Description
bool

True when the session exists, False otherwise.

TransactionStore

Bases: Protocol

Holds login transactions between the login redirect and the callback.

Implementations shared by several processes must make :meth:consume atomic.

consume(transaction_id)

Remove and return the transaction with ID transaction_id.

When callers race, exactly one receives the transaction.

Returns:

Type Description
LoginTransaction | None

The transaction, or None when none is stored under that ID or it

LoginTransaction | None

has expired.

save(transaction, *, ttl)

Store transaction under its id for ttl.