Ushka

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 SessionRecordOne stored session.
class SessionStoreBase class of a session store. Every method is a coroutine.
class DatabaseStoreSessions 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

NameTypeDescription
keystr

SHA-256 of the session id. The id itself is never stored, so a leaked database or backup cannot be turned into working cookies.

datastr

The session contents as JSON text.

user_idstr or None

Who the session belongs to (the value of [session] user_key).

created_at, last_seenfloat

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

loadReturns the record stored under key, or None.
saveInserts or replaces the record.
touchUpdates last_seen of an existing record; never creates one.
deleteRemoves the record. Missing keys are not an error.
delete_userRemoves every record of user_id and returns how many there were.
closeReleases connections. Called when the app shuts down; the default does nothing.
purgeRemoves 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 | None

Returns the record stored under key, or None.

save

async def save(record: SessionRecord) -> None

Inserts or replaces the record.

touch

async def touch(key: str, last_seen: float) -> None

Updates last_seen of an existing record; never creates one.

delete

async def delete(key: str) -> None

Removes the record. Missing keys are not an error.

delete_user

async def delete_user(user_id: str) -> int

Removes every record of user_id and returns how many there were.

close

async def close() -> None

Releases connections. Called when the app shuts down; the default does nothing.

purge

async def purge(
    created_before: float | None,
    seen_before: float | None,
) -> int

Removes 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 | None

save

async def save(record: SessionRecord) -> None

touch

async def touch(key: str, last_seen: float) -> None

delete

async def delete(key: str) -> None

delete_user

async def delete_user(user_id: str) -> int

close

async def close() -> None

purge

async def purge(
    created_before: float | None,
    seen_before: float | None,
) -> int

def get_store

def get_store(app: Any) -> SessionStore | None

The 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) -> str

Why 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) -> int

Logs 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.