Session middleware — signed cookie and Redis-backed sessions.
Session data is stored via a pluggableSessionStore. Default is
CookieSessionStore(signed cookie with itsdangerous). For
horizontal scaling, useRedisSessionStore.
Cookies are signed with HMAC-SHA-256 by default (configurable via
SessionConfig.signer_digest). A SHA-1 fallback signer keeps cookies
issued by older releases (itsdangerous' historical default) readable, so
upgrading does not log every existing user out.
The session object is stored in a ContextVar, accessible via
get_session()from any handler or middleware.
itsdangerous is required for cookie store. redisis required
for RedisSessionStore (pip install chirp[redis]).
middleware.sessions
| Name | Type | Default | Description |
|---|---|---|---|
type
|
|
— | |
qualified_name
|
|
— | |
element_type
|
|
— | |
description
|
|
— | |
source_file
|
|
— | |
line_number
|
|
— | |
is_autodoc
|
|
— | |
autodoc_element
|
|
— | |
_autodoc_template
|
|
— | |
_autodoc_url_path
|
|
— | |
_autodoc_page_type
|
|
— | |
title
|
|
— | |
doc_content_hash
|
|
— |
Symbols on this page
Protocol for session storage backends.
Return the current session dict.
RaisesLookupErrorif called outside a request with
SessionMiddlewareactive.
Return the current session for templates — never raises.
Template-friendly counterpart toget_session(). Returns the live
session dict whenSessionMiddlewareis active, or…
Mutable request-local state shared with sync handler context copies.
Clear the session and return a fresh empty dict.
Prevents session fixation by discarding all data from the previous session. For cookie store, re-signs…
Session middleware configuration.
secret_keyis required for cookie signing. When using
RedisSessionStore, the cookie stores only the session ID.
Resolve a (possibly"auto") SessionConfig.secureto a concrete bool.
An explicitboolis returned unchanged — the app author opted in or out…
Narrow a (resolved)secure to bool for with_cookie.
resolve_secure runs at freeze, so by request time secureis always a
concretebool.…
Signed cookie session store. Session data stored in cookie.
Redis-backed session store. Cookie stores session ID only.
Session middleware with pluggable store (cookie or Redis).
UsesCookieSessionStoreby default. For horizontal scaling,
passRedisSessionStore via SessionConfig.store.
Usage::
from chirp.middleware.sessions import…
SessionStore
class
Protocol for session storage backends.
get_session
function
def get_session() -> dict[str, Any]
Return the current session dict.
RaisesLookupErrorif called outside a request with
SessionMiddlewareactive.
No parameters.
session
function
def session() -> Mapping[str, Any]
Return the current session for templates — never raises.
Template-friendly counterpart toget_session(). Returns the live
session dict whenSessionMiddlewareis active, or an empty read-only
mapping otherwise (mirroringcurrent_user()'s never-raise contract, so
a template rendered without a session does not blow up)::
{% if session().get("flash") %}
<div class="flash">{{ session()["flash"] }}</div>
{% endif %}
Use the imperativeget_session() (which raises LookupErrorwithout
SessionMiddleware) from handlers where the session is required.
No parameters.
_RegenerationState
class
Mutable request-local state shared with sync handler context copies.
regenerate_session
function
def regenerate_session() -> dict[str, Any]
Clear the session and return a fresh empty dict.
Prevents session fixation by discarding all data from the previous session. For cookie store, re-signs empty dict. For Redis store, creates new session ID and deletes old.
Called automatically bylogin() and logout(). Can also
be called directly when you need to rotate the session::
from chirp.middleware.sessions import regenerate_session
regenerate_session() # old data gone, new cookie on response
RaisesLookupErrorif called outside a request with
SessionMiddlewareactive.
No parameters.
SessionConfig
class
Session middleware configuration.
secret_keyis required for cookie signing. When using
RedisSessionStore, the cookie stores only the session ID.
resolve_cookie_secure
function
_secure_for_cookie
function
CookieSessionStore
class
Signed cookie session store. Session data stored in cookie.
RedisSessionStore
class
Redis-backed session store. Cookie stores session ID only.
SessionMiddleware
class
Session middleware with pluggable store (cookie or Redis).
UsesCookieSessionStoreby default. For horizontal scaling,
passRedisSessionStore via SessionConfig.store.
Usage::
from chirp.middleware.sessions import SessionConfig, SessionMiddleware
app.add_middleware(SessionMiddleware(SessionConfig(
secret_key="my-secret-key",
)))
# Redis-backed (pip install chirp[redis]):
from chirp.middleware.sessions import RedisSessionStore, SessionConfig
app.add_middleware(SessionMiddleware(SessionConfig(
secret_key="my-secret-key",
store=RedisSessionStore(SessionConfig(secret_key="x"), "redis://localhost"),
)))
View source · /home/runner/work/chirp/chirp/site/../src/chirp/middleware/sessions.py:1