Litestar Auth
Project description
litestar-auth
litestar-auth is a production-focused authentication and authorization library for
Litestar. It gives Litestar apps a native plugin for
registration, login, email verification, password reset, route guards, and optional
OAuth, Redis-backed features, and TOTP without forcing you to rebuild security-critical
flows from scratch.
Documentation: https://zylvext.github.io/litestar-auth/
Quick peek
This is the same app.py used in the
Quickstart. The quickstart page adds
the SQLite table bootstrap and the register/verify/login request flow. To run this exact
SQLite demo locally, install aiosqlite alongside litestar-auth.
"""Minimal Litestar auth quickstart app mirrored in docs/quickstart.md."""
from __future__ import annotations
from datetime import timedelta
from typing import Any
from uuid import UUID
from litestar import Litestar, Request, get
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
from litestar_auth import (
AuthenticationBackend,
BaseUserManager,
BearerTransport,
LitestarAuth,
LitestarAuthConfig,
UserManagerSecurity,
is_authenticated,
)
from litestar_auth.authentication.strategy import JWTStrategy
from litestar_auth.db.sqlalchemy import SQLAlchemyUserDatabase
from litestar_auth.models import User
DATABASE_URL = "sqlite+aiosqlite:///./quickstart.db"
engine = create_async_engine(DATABASE_URL, echo=False)
session_maker = async_sessionmaker(engine, class_=AsyncSession, expire_on_commit=False)
class UserManager(BaseUserManager[User, UUID]):
"""Print verification tokens so the quickstart can finish without email infrastructure."""
verification_tokens: dict[str, str] = {}
async def on_after_register(self, user: User, token: str) -> None:
self.verification_tokens[user.email] = token
print(f"verification token for {user.email}: {token}") # noqa: T201
@get("/protected", guards=[is_authenticated])
async def protected(request: Request[User, Any, Any]) -> dict[str, str]:
user = request.user
assert user is not None
return {"email": user.email}
backend = AuthenticationBackend[User, UUID](
name="bearer",
transport=BearerTransport(),
strategy=JWTStrategy[User, UUID](
secret="replace-with-32+-char-jwt-secret",
lifetime=timedelta(minutes=15),
subject_decoder=UUID,
allow_inmemory_denylist=True,
),
)
config = LitestarAuthConfig[User, UUID](
backends=(backend,),
session_maker=session_maker,
user_model=User,
user_manager_class=UserManager,
user_db_factory=lambda session: SQLAlchemyUserDatabase(session, user_model=User),
user_manager_security=UserManagerSecurity(
verification_token_secret="replace-with-32+-char-secret-for-verify",
reset_password_token_secret="replace-with-32+-char-secret-for-reset",
),
include_users=False,
)
app = Litestar(route_handlers=[protected], plugins=[LitestarAuth(config)])
Features
- Litestar-native plugin setup through
LitestarAuthConfig(...)andLitestarAuth(config). - Registration, login, email verification, password reset, and protected-route guards out of the box.
- Transport + strategy auth backends, including Bearer or Cookie transports and JWT, database, or Redis token strategies.
BaseUserManagerhooks for integrating email delivery, background jobs, and app-specific lifecycle logic.- Bundled SQLAlchemy user model plus
SQLAlchemyUserDatabasefor the default persistence path. - Normalized flat-role contract for responses and guards, with a matching
litestar rolesCLI for operator workflows. - Optional Redis denylist, rate limiting, OAuth login/account linking, and built-in TOTP support.
- Typed public APIs and docs aimed at application developers rather than framework internals.
Install
uv add litestar-auth
# or
pip install litestar-auth
For the SQLite quick peek and quickstart example, also add aiosqlite:
uv add litestar-auth aiosqlite
Install extras only when you need those features:
litestar-auth[redis]for Redis-backed token storage, JWT denylist support, and auth rate limiting.litestar-auth[oauth]for OAuth flows viahttpx-oauthand encrypted provider tokens.litestar-auth[totp]for built-in TOTP helpers.litestar-auth[all]forredis,oauth, andtotptogether.
Password hashing defaults are now Argon2-only. Unsupported stored password hashes fail closed under the library default, so rotate or reset those credentials before upgrading; see the Migration Guide.
Read more
- Quickstart: bootstrap SQLite, run the app, and walk through register/verify/login.
- Installation: requirements, extras, and typical deployment stacks.
- Configuration: user model, manager, backends, Redis, OAuth, TOTP, and security knobs.
- Security: secure defaults, migration-only flags, and production hardening notes.
- Role management CLI: operator commands for bundled relational roles.
- Testing plugin-backed apps: AsyncTestClient patterns and repo-aligned test advice.
- Python API overview: stable imports and where advanced submodules live.
Repository
Contributor setup, verification commands, and docs tooling live in Contributing.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file litestar_auth-2.1.0.tar.gz.
File metadata
- Download URL: litestar_auth-2.1.0.tar.gz
- Upload date:
- Size: 782.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6df2fb225ad8e0cdefa3b24e61e8d1f9ceb105d3480a7d5c668db7d1ea7fd487
|
|
| MD5 |
0bf9206ca5088ee7a185f5a85467e078
|
|
| BLAKE2b-256 |
22589af33828a79b2ab765ed133c871f5651f74c6a620b6d63a73095bc1e6ba1
|
Provenance
The following attestation bundles were made for litestar_auth-2.1.0.tar.gz:
Publisher:
3_release.yml on ZYLVEXT/litestar-auth
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
litestar_auth-2.1.0.tar.gz -
Subject digest:
6df2fb225ad8e0cdefa3b24e61e8d1f9ceb105d3480a7d5c668db7d1ea7fd487 - Sigstore transparency entry: 1365352005
- Sigstore integration time:
-
Permalink:
ZYLVEXT/litestar-auth@1966130e649af063b9aec8e877ddc6606548fae3 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/ZYLVEXT
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
3_release.yml@1966130e649af063b9aec8e877ddc6606548fae3 -
Trigger Event:
workflow_run
-
Statement type:
File details
Details for the file litestar_auth-2.1.0-py3-none-any.whl.
File metadata
- Download URL: litestar_auth-2.1.0-py3-none-any.whl
- Upload date:
- Size: 221.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3af496e12c7043c4359a0c8ea9d78e9c3590278789770f7299aa09c9d044fcd7
|
|
| MD5 |
f9f42fcb081be8adb91a71f74f60eec5
|
|
| BLAKE2b-256 |
73a54b33c95e685a7cfe0ac43db2a83ff5e6a0caa731bbae66bde7711c784de1
|
Provenance
The following attestation bundles were made for litestar_auth-2.1.0-py3-none-any.whl:
Publisher:
3_release.yml on ZYLVEXT/litestar-auth
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
litestar_auth-2.1.0-py3-none-any.whl -
Subject digest:
3af496e12c7043c4359a0c8ea9d78e9c3590278789770f7299aa09c9d044fcd7 - Sigstore transparency entry: 1365352070
- Sigstore integration time:
-
Permalink:
ZYLVEXT/litestar-auth@1966130e649af063b9aec8e877ddc6606548fae3 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/ZYLVEXT
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
3_release.yml@1966130e649af063b9aec8e877ddc6606548fae3 -
Trigger Event:
workflow_run
-
Statement type: