two-fast-auth is a FastAPI middleware that provides seamless two-factor authentication implementation. It integrates with FastAPI to offer robust 2FA protection for your application routes.
Documentation
📚 Full Documentation - Comprehensive technical documentation and API reference
Features
- QR Code Generation: Automatic QR code creation for authenticator apps
- TOTP Verification: Time-based one-time password validation
- Recovery Codes: Secure recovery code generation and management
- Optional Secret Encryption: Securely store and verify 2FA secrets
- Middleware Integration: Easy integration with FastAPI routes
Installation
To install two-fast-auth, use pip:
pip install two-fast-auth
Basic Usage
from fastapi import FastAPI
from two_fast_auth import TwoFactorMiddleware, TwoFactorAuth
app = FastAPI()
async def get_user_secret(user_id: str) -> str:
# Implement your logic to retrieve user's secret from database
return "user_stored_secret" # Replace with actual DB lookup
app.add_middleware(
TwoFactorMiddleware,
get_user_secret_callback=get_user_secret,
excluded_paths=["/docs", "/redoc"],
header_name="X-2FA-Code",
encryption_key="your-key-here" # Optional
)
@app.get("/protected-route")
async def protected_route():
return {"message": "2FA protected content"}
Configuration Options
TwoFactorAuth Parameters
| Parameter | Default | Description |
|---|---|---|
secret |
Auto-generated | Base32 secret for TOTP generation |
qr_fill_color |
"black" | QR code foreground color |
qr_back_color |
"white" | QR code background color |
issuer_name |
"2FastAuth" | Name displayed in authenticator apps |
TwoFactorMiddleware Parameters
| Parameter | Default | Description |
|---|---|---|
encryption_key |
None | Encryption key for securing 2FA secrets (Fernet-compatible key) |
excluded_paths |
["/login", "/setup-2fa"] | Paths that bypass 2FA verification |
header_name |
"X-2FA-Code" | Request header containing 2FA verification code |
Advanced Configuration
# Generate and encrypt secret
secret = TwoFactorAuth().secret
encrypted_secret = TwoFactorAuth.encrypt_secret(
secret,
encryption_key="your-key-here"
)
# Store encrypted secret in database
async def get_user_secret(user_id: str) -> str:
return await fetch_encrypted_secret_from_db(user_id)
# Middleware with encrypted secrets
app.add_middleware(
TwoFactorMiddleware,
get_user_secret_callback=get_user_secret,
encryption_key="your-key-here",
excluded_paths=["/healthcheck"]
)
Contributing
Contributions are welcome! Please open an issue or submit a pull request on GitHub.
License
MIT License - See LICENSE for details
Author
Renzo Franceschini
Acknowledgements
Metadata
Release files for two-fast-auth 1.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| two-fast-auth-1.1.0.tar.gz | 6.0 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| two_fast_auth-1.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 12.3 kB
Release files / two-fast-auth-1.1.0.tar.gz
| Download URL | two-fast-auth-1.1.0.tar.gz |
|---|---|
| Size | 6.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
66f50ea01c64a005738d0f39f741129f6da277c0dbb84594141e3bdbbe4bb24c
|
|
BLAKE2b-256 checksum How to use checksums |
d971a4786fb18d65f8ba590a28d080c69de54dd6435bf5a75333f0a13e8a7aed
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.10.16
|
Release files / two_fast_auth-1.1.0-py3-none-any.whl
| Download URL | two_fast_auth-1.1.0-py3-none-any.whl |
|---|---|
| Size | 6.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
f9080eb619bc03e703eac5e4f4b1b04d6bc4f42aa536bead05ed4618c1e7fb00
|
|
BLAKE2b-256 checksum How to use checksums |
67153557b4ac8ebf4773bcc58c1cb95f2e1e727990dcab473728018cb68d6c19
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.10.16
|
Security for two-fast-auth 1.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.