Sessions
Fenrir provides multiple session backends for storing user data across requests.
Session Usage with Context Locals
Access the current session via the session context local, imported from fenrir:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | |
The session object is a dict-like object that automatically tracks modifications and is saved to the response at the end of each request.
SessionMixin
Base class for all session objects. A dict subclass that tracks whether the session has been modified or accessed:
1 2 3 4 5 6 7 8 9 | |
Tracking behavior:
| Operation | Sets modified |
|---|---|
session["key"] = value |
Yes |
del session["key"] |
Yes |
session.clear() |
Yes |
session.pop("key") |
Yes |
session.update(...) |
Yes |
session["key"] (read only) |
No (only sets accessed) |
SecureCookieSession
Cookie-based session (the default). Session data is serialized, signed, and stored in a cookie on the client:
1 2 3 4 5 6 7 8 9 10 | |
ServerSideSession
Server-side session with a sid (session ID) attribute. The session data is stored server-side; only the session ID is sent to the client as a cookie:
1 2 3 4 5 6 7 8 | |
SessionInterface (Abstract Base Class)
Base class for all session backends. Subclass this to implement custom storage:
1 2 3 4 5 6 7 8 9 10 | |
SecureCookieSessionInterface
The default session interface. Serializes session data with itsdangerous.URLSafeTimedSerializer, signs it, and stores it in a cookie:
1 2 3 4 5 6 | |
How it works:
open_session()reads the cookie, verifies the signature, and returns aSecureCookieSession.save_session()serializes the session, signs it, and sets the cookie on the response.- If the session is empty and modified, the cookie is deleted.
InMemorySessionBackend
A simple in-memory session store for testing and single-process deployments. Sessions are stored in a Python dict with TTL-based expiration:
1 2 3 4 5 6 7 8 9 10 11 12 13 | |
Expired sessions are automatically cleaned up on access.
In-Memory Sessions
Server-side sessions backed by InMemorySessionBackend:
1 2 3 4 5 6 | |
Parameters:
backend: CustomInMemorySessionBackendinstancettl: Session time-to-live in seconds (default:86400)
Redis Sessions
Server-side sessions backed by Redis. Supports both sync and async Redis clients:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
Parameters:
redis_client: Redis client instance (required)prefix: Key prefix (default:"session:")ttl: Session time-to-live in seconds (default:86400)
Install Redis support: pip install fenrir-framework[redis]
Configuration Keys
| Key | Default | Description |
|---|---|---|
SECRET_KEY |
(required for cookie sessions) | Secret key for signing cookies via itsdangerous |
SESSION_COOKIE_NAME |
"session" |
Cookie name |
SESSION_COOKIE_DOMAIN |
None |
Cookie domain |
SESSION_COOKIE_PATH |
"/" |
Cookie path |
SESSION_COOKIE_SECURE |
True |
Only send cookie over HTTPS |
SESSION_COOKIE_HTTPONLY |
True |
Prevent JavaScript access to cookie |
SESSION_COOKIE_SAMESITE |
None |
SameSite cookie attribute ("Strict", "Lax", "None") |
SESSION_TTL |
86400 |
Session TTL in seconds (server-side backends) |
Session Classes Reference
| Class | Description |
|---|---|
SessionMixin |
Base dict subclass with modified/accessed tracking |
SecureCookieSession |
Cookie-based session (default) |
ServerSideSession |
Server-side session with sid attribute |
SessionInterface |
Abstract base class for session backends |
SecureCookieSessionInterface |
Default cookie-based session interface |
InMemorySessionBackend |
In-memory storage backend |
InMemorySessionInterface |
In-memory session interface |
RedisSessionInterface |
Redis-backed session interface |
Custom Session Backend
Implement SessionInterface for custom backends:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | |
Session Lifecycle
- Request arrives —
SessionInterface.open_session()reads the session from the cookie/storage and returns a session object. - During request handling — Handlers read/write
session["key"]. Themodifiedflag is set automatically. - Response sent —
SessionInterface.save_session()checks if the session was modified, and if so, persists it and sets/updates the cookie.