Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/deepmancer/fastapi-auth-jwt
Simple to use FastAPI JWT auth middleware
https://github.com/deepmancer/fastapi-auth-jwt
authentication authorization bearer-authentication fastapi fastapi-extension fastapi-middleware jwt jwt-authentication python3 starlette starlette-middleware token-based-authentication
Last synced: 4 months ago
JSON representation
Simple to use FastAPI JWT auth middleware
- Host: GitHub
- URL: https://github.com/deepmancer/fastapi-auth-jwt
- Owner: deepmancer
- License: mit
- Created: 2024-08-07T09:53:33.000Z (6 months ago)
- Default Branch: main
- Last Pushed: 2024-09-01T18:22:26.000Z (5 months ago)
- Last Synced: 2024-09-28T16:41:26.093Z (4 months ago)
- Topics: authentication, authorization, bearer-authentication, fastapi, fastapi-extension, fastapi-middleware, jwt, jwt-authentication, python3, starlette, starlette-middleware, token-based-authentication
- Language: Python
- Homepage:
- Size: 959 KB
- Stars: 6
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# FastAPI Auth JWT
Highly-customizable and ready-to-use session authentication for FastAPI applications
---
**Source Code**: https://github.com/deepmancer/fastapi-auth-jwt
---
## **✨ Features**
- 🚀 **Effortless Integration**: Seamlessly add JWT authentication to your FastAPI application with just a few lines of code.
- 🛠️ **Highly Customizable**: Tailor the authentication process to fit your specific needs, including custom user models and storage options.
- 🔄 **Sync and Async Support**: Works out of the box with both synchronous and asynchronous FastAPI applications.
- 💾 **Flexible Token Storage**: Supports in-memory token storage for simple applications and Redis for real-world, distributed backends.## **📦 Installation**
To install the basic package:
```bash
pip install fastapi-auth-jwt
```If you want to use Redis for token storage, install the package with Redis support:
```bash
pip install fastapi-auth-jwt[redis]
```## **🚀 Quick Start**
### **🛠️ Basic Setup**
1. **🧑💻 Define Your User Schema**: Create a Pydantic model representing the user.
```python
from pydantic import BaseModel, Fieldclass User(BaseModel):
username: str
password: str
token: Optional[str] = Field(None)
```2. **⚙️ Configure Authentication Settings**: Set up your authentication configuration.
```python
from pydantic import BaseModelclass AuthenticationSettings(BaseModel):
secret: str = "your-secret-key"
jwt_algorithm: str = "HS256"
expiration_seconds: int = 3600 # 1 hour
```3. **🔧 Initialize the Authentication Backend**: Create an instance of the `JWTAuthBackend`.
```python
from fastapi_auth_jwt import JWTAuthBackendauth_backend = JWTAuthBackend(
authentication_config=AuthenticationSettings(),
user_schema=User
)
```4. **🔌 Add Middleware to Your FastAPI Application**:
```python
from fastapi import FastAPI
from fastapi_auth_jwt import JWTAuthenticationMiddlewareapp = FastAPI()
app.add_middleware(
JWTAuthenticationMiddleware,
backend=auth_backend,
exclude_urls=["/sign-up", "/login"],
)
```5. **📚 Create Routes**:
```python
@app.post("/sign-up")
async def sign_up(request_data: RegisterSchema):
return {"message": "User created"}@app.post("/login")
async def login(request_data: LoginSchema):
token = await auth_backend.create_token(
username=request_data.username,
password=request_data.password,
)
return {"token": token}@app.get("/profile-info")
async def get_profile_info(request: Request):
user: User = request.state.user
return {"username": user.username}@app.post("/logout")
async def logout(request: Request):
user: User = request.state.user
await auth_backend.invalidate_token(user.token)
return {"message": "Logged out"}
```### **🧰 Redis Extension**
To enable Redis as the storage backend:
```python
from fastapi_auth_jwt import RedisConfig, JWTAuthBackendredis_config = RedisConfig(
host="localhost",
port=6379,
db=0,
)auth_backend_redis = JWTAuthBackend(
authentication_config=AuthenticationSettings(),
user_schema=User,
storage_config=redis_config,
)app.add_middleware(
JWTAuthenticationMiddleware,
backend=auth_backend_redis,
exclude_urls=["/sign-up", "/login"],
)
```## **⚙️ Configuration Options**
### `AuthConfig`
- 🛡️ `secret` (str): Secret key for signing JWT tokens.
- 🧮 `jwt_algorithm` (str): Algorithm used for token encoding (default: `HS256`).
- ⏲️ `expiration_seconds` (int): Token expiration time in seconds (default: `3600`).### `StorageConfig`
- 🗄️ `storage_type` (StorageTypes): Type of storage backend (`MEMORY` or `REDIS`).
### `RedisConfig`
- 🌐 `host` (str): Redis server hostname (default: `localhost`).
- 🛠️ `port` (int): Redis server port (default: `6379`).
- 🗃️ `db` (int): Redis database index (default: `0`).
- 🔑 `password` (Optional[str]): Redis server password (default: `None`).## **📂 Example Projects**
For fully working examples, refer to the [examples directory](https://github.com/deepmancer/fastapi-auth-jwt/tree/main/examples) in the repository.
## **📚 Documentation**
Complete documentation is available in the [docs directory](https://github.com/deepmancer/fastapi-auth-jwt/blob/main/docs/README.md).
## **📝 License**
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.
## **📬 Contact**
For any questions, suggestions, or issues, please feel free to open an issue or reach out via [GitHub Issues](https://github.com/deepmancer/fastapi-auth-jwt/issues).
---
With `fastapi-auth-jwt`, adding secure, flexible JWT-based authentication to your FastAPI applications is easier than ever. Get started today and enjoy a streamlined authentication experience!