A *heavily* opinionated library for simplifying the boilerplate of FastAPI authentication.
Project description
# app.py
from fastapi_auth.auth import setup_auth, build_auth_router, restrict, protect_router
from services.users import authenticate_user, create_user
from other_router import other_router
from fastapi import FastAPI
import uvicorn, os
# Configure auth library
auth_manager = setup_auth(
auth_endpoint="/auth/login",
jwt_secret_key=os.getenv("JWT_SECRET_KEY", "A-Secure-Key"),
access_token_expire_minutes=int(os.getenv("ACCESS_TOKEN_EXPIRE_MINUTES", "1440")),
token_renewal_minutes=int(os.getenv("TOKEN_RENEWAL_MINUTES", "30")),
database_path=os.getenv("AUTH_DB_PATH", "auth.db") # Minimal SQLite for Auth/Tokens
)
app = FastAPI()
# Build and Register the Auth Router
app.include_router(
build_auth_router(
authenticate_user=authenticate_user,
create_user=create_user
)
)
@app.get("/restricted_ep")
@restrict(roles_allowed=["admin"], inject_user=True)
def root(current_user: dict):
return {"message": f"Welcome {current_user['username']} - ID {current_user['user_id']}, to the API !"}
@app.get("/unrestricted_ep")
def root():
return {"message": "Welcome to the API!"}
# Protect other routes
protect_router(other_router)
app.include_router(other_router)
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8000)
# services/users.py
from fastapi import HTTPException, status
from fastapi_auth.auth import get_auth_manager
def authenticate_user(username: str, password: str):
# 1 - Get user from database by username, fetch password
user = {} # dummy just for the example
# 2 - Verify password using the manager helper fn
if not get_auth_manager().verify_password(password, user["password"]):
return None
return {"user_id": username, "role": user["role"]}
def create_user(user_dict: dict):
# 1 - Validate if username already exists
user_already_exists = user_dict['username'] in [] # dummy just for the example
if user_already_exists:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Username already exists"
)
# 2 - Create user in database
# user_dict contains username, password, role, and any extra metadata
pass
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 simple_fastapi_auth-1.1.tar.gz.
File metadata
- Download URL: simple_fastapi_auth-1.1.tar.gz
- Upload date:
- Size: 8.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
07ac0a49c54e24244b1780e3c037c1ef59cf9ea19a3cc82a5efbbd2f129416a7
|
|
| MD5 |
3f9a36f705b7cc6d7002ab44a38fc5e8
|
|
| BLAKE2b-256 |
95e1fe55d432a63d46e3386480195105300b62431dec8bd8f989c717dd94ae7c
|
File details
Details for the file simple_fastapi_auth-1.1-py3-none-any.whl.
File metadata
- Download URL: simple_fastapi_auth-1.1-py3-none-any.whl
- Upload date:
- Size: 8.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
194940717d7d0c76b609c89e4f903ba35e6d01e64d9eb015c8f7f31249a417af
|
|
| MD5 |
a699b04ae2fea73cc9c04e910200f280
|
|
| BLAKE2b-256 |
630514773901829e04893fc8d156a12c29ff6aa781b93955c745dd99abd65d2f
|