ushka.sessions.stores
Where server-side sessions live.
With [session] store = "database" (or an object you assign to
app.session_store) the session cookie carries only an opaque,
signed id and the data stays here. Deleting a record revokes the session for
real: a stolen cookie stops working.
The database store needs a database: [database] url (or DATABASE_URL)
must be set and SQLAlchemy installed (ushka[orm]). Without one the app
keeps using signed cookies and says so in the log. There is no in-process store
on purpose: it would log users out at random as soon as there is more than one
worker.
A store is a small class, SessionStore. Subclass it to keep sessions in
Redis or anywhere else; the framework only calls the methods below.
Nothing in a store is ever logged.
In this module
class SessionRecord | One stored session. |
class SessionStore | Base class of a session store. Every method is a coroutine. |
class DatabaseStore | Sessions in a table of the [database] database (SQLAlchemy, async). |
def get_store() | The store the app uses, or None when sessions are signed cookies. |
def no_database_reason() | Why server-side sessions cannot use a database, or an empty string when they can. |
def revoke_user_sessions() | Logs user_id out everywhere: deletes all their server-side sessions. |
class SessionRecord
One stored session.
Attributes
| Name | Type | Description |
|---|---|---|
key | str | SHA-256 of the session id. The id itself is never stored, so a leaked database or backup cannot be turned into working cookies. |
data | str | The session contents as JSON text. |
user_id | str or None | Who the session belongs to (the value of |
created_at, last_seen | float | Unix times, used for the absolute and the idle expiry. |
class SessionStore
Base class of a session store. Every method is a coroutine.
Implement load, save, touch, delete and delete_user; purge is
optional (stores with their own expiry, such as Redis, can skip it).
Methods
load | Returns the record stored under key, or None. |
save | Inserts or replaces the record. |
touch | Updates last_seen of an existing record; never creates one. |
delete | Removes the record. Missing keys are not an error. |
delete_user | Removes every record of user_id and returns how many there were. |
close | Releases connections. Called when the app shuts down; the default does nothing. |
purge | Removes expired records (created before created_before or idle since seen_before; None means no such limit). Returns the count. |
load
async def load(key: str) -> SessionRecord | NoneReturns the record stored under key, or None.
save
async def save(record: SessionRecord) -> NoneInserts or replaces the record.
touch
async def touch(key: str, last_seen: float) -> NoneUpdates last_seen of an existing record; never creates one.
delete
async def delete(key: str) -> NoneRemoves the record. Missing keys are not an error.
delete_user
async def delete_user(user_id: str) -> intRemoves every record of user_id and returns how many there were.
close
async def close() -> NoneReleases connections. Called when the app shuts down; the default does nothing.
purge
async def purge(
created_before: float | None,
seen_before: float | None,
) -> intRemoves expired records (created before created_before or idle
since seen_before; None means no such limit). Returns the count.
class DatabaseStore (SessionStore)
Sessions in a table of the [database] database (SQLAlchemy, async).
The table is created on first use; its name is [session] table. It
needs the orm extra (pip install "ushka[orm]").
load
async def load(key: str) -> SessionRecord | Nonesave
async def save(record: SessionRecord) -> Nonetouch
async def touch(key: str, last_seen: float) -> Nonedelete
async def delete(key: str) -> Nonedelete_user
async def delete_user(user_id: str) -> intclose
async def close() -> Nonepurge
async def purge(
created_before: float | None,
seen_before: float | None,
) -> intdef get_store
def get_store(app: Any) -> SessionStore | NoneThe store the app uses, or None when sessions are signed cookies.
app.session_store wins when it is set; otherwise the store named by
[session] store is built once and kept on the app.
def no_database_reason
def no_database_reason(config: Any) -> strWhy server-side sessions cannot use a database, or an empty string when they can.
def revoke_user_sessions
async def revoke_user_sessions(target: Any, user_id: Any) -> intLogs user_id out everywhere: deletes all their server-side sessions.
target is the app or the current request. Returns how many sessions were
removed. When it is a request whose own session belongs to that user, that
session is cleared too, so this response does not write it back.