Python module to add username and password authentication to specific FastAPI routes
Platform Supported
Deployments
pip install FastAPI-UI-Authimport logging
import uiauth
from fastapi import FastAPI
from fastapi.routing import APIRoute
app = FastAPI()
@app.get("/public")
async def public_route():
return {"message": "This is a public route"}
async def private_route():
return {"message": "This is a private route"}
uiauth.protect(
# ------ MANDATORY ARGS ------
app=app,
routes=APIRoute(
path="/private",
endpoint=private_route
),
username="admin",
password="password123",
# ------ OPTIONAL ARGS ------
totp_token="JBSWSECUREDK3PXP",
session_timeout=3600,
fallback=uiauth.Fallback(button="GO BACK", path="/public"),
custom_logger=logging.getLogger("my_custom_logger")
)
FastAPI-UI-Authsupports bothAPIRouteandAPIWebSocketRouteroutes.
Refer samples directory for different use-cases.
Note
Use the CLI command uiauth-totp to generate a TOTP token and a QR code to scan with an authenticator app
(e.g. Google Authenticator, Authy, etc.) for 2FA support.
Docstring format: Google
Styling conventions: PEP 8 and isort
Requirement
python -m pip install gitverseUsage
gitverse-release reverse -f release_notes.rst -t 'Release Notes'Requirement
python -m pip install sphinx==5.1.1 pre-commit recommonmarkUsage
pre-commit run --all-fileshttps://pypi.org/project/FastAPI-UI-Auth/
© Vignesh Rao
Licensed under the MIT License