Database adapters¶
The litestar_auth.db package exposes only the abstract persistence contracts and their lightweight data payloads: BaseUserStore, BaseOAuthAccountStore, BaseApiKeyStore, OAuthAccountData, and ApiKeyData. These protocols describe how the user manager talks to your storage layer without tying the library to a particular ORM.
The concrete SQLAlchemy implementations live in a dedicated submodule: import SQLAlchemyUserDatabase and SQLAlchemyApiKeyStore from litestar_auth.db.sqlalchemy. They are not re-exported from litestar_auth.db on purpose—eagerly importing the adapter would register SQLAlchemy mappers and break the lazy-import boundary described in the project guide. Use the submodule when you are ready to wire real tables.
For end-to-end ORM setup (session maker, models, plugin config), see User and manager and Backends; the Configuration index lists every split reference page. For customizing the user table while keeping OAuth accounts on the bundled model, see Custom user + OAuth.
from litestar_auth.db.sqlalchemy import SQLAlchemyUserDatabase
from litestar_auth.db.sqlalchemy import SQLAlchemyApiKeyStore
litestar_auth.db
¶
Database abstractions and implementations.
ApiKeyData(key_id, user_id, hashed_secret, encrypted_secret, name, scopes, prefix_env, signing_required, expires_at, created_via, client_metadata=None)
dataclass
¶
Persistence fields required to create an API-key row.
BaseApiKeyStore
¶
Bases: Protocol
Structural CRUD contract for API-key persistence backends.
create(data)
async
¶
create_for_user_with_limit(data, *, max_keys_per_user)
async
¶
Persist an API key only when the user is still below the active-key limit.
delete_for_user(user_id)
async
¶
Permanently delete all API-key rows for user_id.
Returns:
| Type | Description |
|---|---|
int
|
Number of rows deleted when the backend can report it, otherwise |
get_by_key_id(key_id, *, include_inactive=False)
async
¶
list_for_user(user_id, *, include_inactive=False)
async
¶
list_signing_keys_requiring_reencrypt(requires_reencrypt, *, include_inactive=False)
async
¶
Return signing API-key rows whose encrypted secret needs keyring rotation.
replace_signing_key_encrypted_secret(key_id, *, encrypted_secret)
async
¶
Replace one signing API-key row's encrypted secret without changing other fields.
revoke(key_id, *, revoked_at)
async
¶
update(key_id, *, name=None, scopes=None)
async
¶
update_last_used_at(key_id, *, last_used_at)
async
¶
BaseOAuthAccountStore
¶
BaseUserStore
¶
Bases: Protocol
Structural CRUD interface for user persistence backends.
create(user_dict)
async
¶
delete(user_id)
async
¶
get(user_id)
async
¶
get_by_email(email)
async
¶
get_by_field(field_name, value)
async
¶
Return the user where field_name equals value, if present.
field_name must be "email" or "username" (see
:data:~litestar_auth.types.LoginIdentifier). Implementations may perform a
direct column/attribute lookup. Values outside that set are a
programming error and may surface as backend-specific errors at
runtime when callers bypass static typing.
Source code in litestar_auth/db/base.py
list_users(*, offset, limit)
async
¶
OAuthAccountData(oauth_name, account_id, account_email, access_token, expires_at, refresh_token)
dataclass
¶
Provider account identity and token fields for OAuth-account persistence.