A simple and flexible Basic Authentication middleware for FastAPI applications
Project description
FastAPI Basic Auth
A simple and flexible Basic Authentication middleware for FastAPI applications.
Installation
pip install fastapi-basic-auth
Usage
Basic Usage with Dictionary
from fastapi import FastAPI
from fastapi_basic_auth import FastAPIBasicAuthMiddleware
app = FastAPI()
# Add middleware
auth = FastAPIBasicAuthMiddleware(
urls=["/protected"],
users={"admin": "password"}
)
app.add_middleware(auth.build)
@app.get("/protected")
def protected():
return {"message": "This route is protected"}
Using BasicAuthUser Model
You can also use the BasicAuthUser model directly for more control and validation:
from fastapi import FastAPI
from fastapi_basic_auth import FastAPIBasicAuthMiddleware, BasicAuthUser
app = FastAPI()
# Create users using BasicAuthUser model
users = [
BasicAuthUser(username="admin", password="password123"),
BasicAuthUser(username="user1", password="userpass")
]
# Add middleware with BasicAuthUser list
auth = FastAPIBasicAuthMiddleware(
urls=["/protected", "/admin"], # Protect multiple routes
users=users
)
app.add_middleware(auth.build)
@app.get("/protected")
def protected():
return {"message": "This route is protected"}
@app.get("/admin")
def admin():
return {"message": "Admin route is protected"}
The BasicAuthUser model includes built-in validation:
- Username must be alphanumeric (can include underscores and hyphens)
- Both username and password are required fields
Features
- Protect specific routes with Basic Authentication
- Support for multiple users
- Flexible configuration
- Use simple dictionary for quick setup
- Use BasicAuthUser model for additional validation
- Secure password comparison
Project details
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 fastapi_basic_auth-0.1.2.tar.gz.
File metadata
- Download URL: fastapi_basic_auth-0.1.2.tar.gz
- Upload date:
- Size: 3.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.12.5 Darwin/24.0.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2a981492d34adf0da8d59bcfecf845878b63aef8feb85defd7c3409390a2eb3b
|
|
| MD5 |
488c162619de57cbf909fe5abcd3d22c
|
|
| BLAKE2b-256 |
deab174d392d442e482e32e6ee47c929b7b3f56090823f6fe7379783eb375ecc
|
File details
Details for the file fastapi_basic_auth-0.1.2-py3-none-any.whl.
File metadata
- Download URL: fastapi_basic_auth-0.1.2-py3-none-any.whl
- Upload date:
- Size: 3.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.12.5 Darwin/24.0.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
47c12a82bb43a4db3a3264db4757df2b94250afcc293ba5c416d0ec579fa4bc2
|
|
| MD5 |
44e97325d6afa2d9432e8a09e428d252
|
|
| BLAKE2b-256 |
3801b1ce392b573fc8c70bb91a6ddceed3b5e6a69bfa679f7f16d0828d0aa84c
|