tortoise-auth¶
Async authentication and user management for Tortoise ORM. Framework-agnostic, extensible, secure by default.
tortoise-auth is a pure-async authentication library built on top of
Tortoise ORM. It provides a complete user
authentication stack -- password hashing, token issuance, session management,
HMAC signing, and lifecycle events -- without coupling you to any particular web
framework. Define your user model, call configure(), and you have a
production-ready auth layer that works with FastAPI, Starlette, Sanic, or any
other async Python framework.
What You Can Build¶
tortoise-auth covers the most common authentication scenarios out of the box:
| Use Case | Features Used |
|---|---|
| User registration & login | AbstractUser, AuthService.login |
| Multi-step onboarding | OnboardingService — server-driven register → verify → TOTP flow |
| Protected API endpoints | AuthService.authenticate, Starlette TokenAuthBackend |
| Server-to-server auth | S2SService, Starlette S2SAuthBackend |
| Token refresh rotation | AuthService.refresh |
| Email verification | make_token / verify_token (HMAC signing) |
| Password reset | make_token / verify_token with max_age |
| Invite links & unsubscribe URLs | Signer / TimestampSigner |
| Force logout everywhere | AuthService.logout_all |
| Audit logging & notifications | Event system (on, emit) |
| Account lockout on failed logins | user_login_failed event |
| Migrating password hashes from Django | Auto-migration (Argon2id, bcrypt, PBKDF2) |
| OAuth / social login users | set_unusable_password() |
See the Use Cases & Cookbook for complete, copy-pasteable examples.
Key Features¶
- Abstract User model -- extend
AbstractUserto get email-based authentication, password hashing,is_active/is_verifiedflags, and timestamp tracking out of the box. - AuthService -- high-level async API for
login,authenticate,refresh,logout, andlogout_all. - Pluggable token backends -- choose between stateless JWT tokens or
server-side database tokens. Both implement the
TokenBackendProtocol for easy swapping or custom backends. - Multi-algorithm password hashing -- Argon2id (primary), Bcrypt, and PBKDF2-SHA256 with transparent auto-migration to the strongest hasher.
- Password validation -- four built-in validators (minimum length, common
password list, numeric-only, user-attribute similarity) plus custom validators
via the
PasswordValidatorProtocol. - HMAC signing --
Signer,TimestampSigner, and convenience helpersmake_token/verify_tokenfor email-confirmation links, password-reset URLs, and other signed payloads. - Onboarding flow engine -- server-driven, multi-step onboarding with
built-in steps for registration, email verification, TOTP setup, and profile
completion. Pluggable via the
OnboardingStepProtocol. - Event system -- subscribe to
user_login,user_login_failed,user_logout, andpassword_changedevents with async handlers. - Server-to-server authentication --
S2SServicefor service-to-service communication using env-var-based tokens with constant-time comparison. - Starlette integration --
TokenAuthBackend,S2SAuthBackend,login_requireddecorator, andrequire_auth/require_s2shelpers for Starlette and FastAPI apps. - Fully async -- every I/O operation uses
await; no hidden synchronous calls.
Quick Example¶
from tortoise_auth import AbstractUser, AuthConfig, AuthService, configure
class User(AbstractUser):
"""Application user model."""
class Meta:
table = "users"
# Configure the library
configure(AuthConfig(
user_model="models.User",
signing_secret="your-secret-key",
))
# Usage (inside an async context)
auth = AuthService()
result = await auth.login("user@example.com", "password123")
user = await auth.authenticate(result.access_token)
Do not hardcode secrets
The snippet above uses a literal signing_secret for brevity. In production,
always load secrets from environment variables or a dedicated secrets
manager.
Installation¶
Optional dependencies
Argon2 hashing requires the argon2-cffi package, which is installed
automatically as a dependency of tortoise-auth.
Quick Navigation¶
Next Steps¶
Ready to build? Head over to the Quick Start guide to set up your first project with tortoise-auth in under five minutes.
Version 0.4.0