Sessions¶
The session mapping and the server-side stores that back it.
Session
¶
Bases: dict[str, Any]
The request session - a dict that knows when it has changed.
permanent
property
writable
¶
Whether the session cookie should use the longer lifetime.
Backed by the reserved _permanent key, so the flag persists in the
cookie across requests and toggling it counts as a session mutation.
regenerate_id
¶
Request a fresh server-side session id on the next response.
Call this at a privilege boundary - login, role change - so an attacker-planted id cannot be replayed against the now-elevated session: the session-fixation defence. Harmless with cookie-only sessions.
pop
¶
Remove key and return its value, marking the session modified on removal.
popitem
¶
Remove and return the last (key, value) pair, marking the session modified.
setdefault
¶
Insert default under key when absent, marking the session modified on insert.
update
¶
Merge keys into the session, marking it modified unless the input is empty.
SessionStore
¶
Server-side session backend interface.
A concrete store persists session payloads keyed by an opaque session id;
ServerSessionMiddleware drives it. The methods are async so a
network-backed store can implement them without blocking the event loop.
read
async
¶
Return the stored payload for session_id, or None.
None when the session is absent, expired, or has been revoked.
write
async
¶
Persist data under session_id, to expire after max_age seconds.
delete
async
¶
Revoke session_id - a later read of it must return None.
replace
async
¶
Write data for session_id only if it still exists.
False when the id is absent - revoked or expired. This is the race-safe
write, so a request in flight cannot resurrect a session a concurrent
delete removed. The default is a non-atomic read-then-write; a store
with an atomic conditional write should override it.
touch
async
¶
Extend the expiry of an existing entry without rewriting its payload.
False when the id was absent (revoked or expired). The default reads
then rewrites the payload; a store with a native TTL-refresh primitive
should override it.
InMemorySessionStore
¶
Bases: SessionStore
A process-local SessionStore - a dict with per-entry expiry.
It does not share state across workers, so a multi-worker deployment needs a
shared backend implementing the SessionStore interface.
Sized, iterable and containment-testable, so it is falsy when it holds no
live session. Test store is not None to ask whether one is configured.
read
async
¶
Return a copy of the stored payload, or None when absent or expired.
write
async
¶
Store a copy of data under session_id, expiring after max_age seconds.
delete
async
¶
Drop session_id from the store. No-op if not present.
replace
async
¶
Write data only when session_id still exists and is unexpired.
touch
async
¶
Refresh the expiry of an existing, unexpired entry without copying its payload.
sweep_expired
¶
Drop every expired entry and return how many were removed.
For deterministic eviction, instead of the probabilistic sweep that
fires from write / replace.
clear
¶
Drop every session and return how many were removed.
The sync counterpart to delete for the whole store. Expired entries no
sweep has reached count as removed too.
expires_at
¶
Return when session_id expires as a Unix timestamp, or None.
None means absent or already past its expiry, matching what read
would say. This is what sliding expiry is observable through.