FastAPI Auth Middleware
FastAPI Auth Middlewawre is a middleware for securing FastAPI application. It provides a convenient way to secure your FastAPI routes and endpoints using jwt tokens.
Features
- Seamless integration with FastAPI applications.
- Easily protect routes and endpoints with JWT authentication.
- Lightweight and designed for simplicity.
Requirements
Python 3.8
Installation
$ pip install fastapi_auth_middlewares
Example
Create it
- Create a file
main.pywith:
from fastapi import FastAPI, Request
from fastapi_auth_middlewares import JwtAuthMiddleware
app = FastAPI(
title="Secured Project",
version="1.0",
)
app.add_middleware(
JwtAuthMiddleware,
secret_key="your_secret_key",
algorithms=["HS256"],
# Excluding Documentation (OpenAPI and favicon routes) and health check routes from authentication
public_paths=["/docs", "/favicon.ico", "/openapi.json", "/api/health"],
)
@app.get("/api/health")
async def health():
return {"message": "server is up and running."}, 200
@app.get("/protected")
async def protected_route(request: Request):
# Access the decoded_token from the middleware
decoded_token = request.state.user
# Your logic using the decoded_token
user_id = decoded_token.get("user_id")
username = decoded_token.get("username")
return {
"message": "This is a protected route",
"user_id": user_id,
"username": username,
}
Run it
- Run the server with:
$ uvicorn main:app --reload
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO: Started reloader process [28720]
INFO: Started server process [28722]
INFO: Waiting for application startup.
INFO: Application startup complete.
Contributing
Feel free to contribute to this project.
License
This project is licensed under the terms of the MIT license.
Metadata
Release files for fastapi-auth-middlewares 0.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 | |
|---|---|---|---|
| fastapi_auth_middlewares-0.1.0.tar.gz | 4.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| fastapi_auth_middlewares-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 8.6 kB
Release files / fastapi_auth_middlewares-0.1.0.tar.gz
| Download URL | fastapi_auth_middlewares-0.1.0.tar.gz |
|---|---|
| Size | 4.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a6e1df7148a07c2e00ed81e03a5b24d4f5662db06bc01ebe8c01f9a0bb70dbee
|
|
BLAKE2b-256 checksum How to use checksums |
9fae60fc411e937909a4343034760223de22465ff79d57ce906fb21d567584ce
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.4
|
Release files / fastapi_auth_middlewares-0.1.0-py3-none-any.whl
| Download URL | fastapi_auth_middlewares-0.1.0-py3-none-any.whl |
|---|---|
| Size | 4.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
e7ad5bc6e08d7cfa78a2cb5945e21d1c0f6e5c418f6e82b5c64e7a2cd7818bba
|
|
BLAKE2b-256 checksum How to use checksums |
aa237af570da5fa71fb363f7e8da27fe9f95e506b66a48e316188a57fbca33b4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.4
|