Assem
A flexible and secure JWT authentication framework for FastAPI applications.
Features
- 🔒 JWT-based authentication with refresh tokens
- 🛡️ CSRF protection
- 🍪 Secure cookie handling
- 🔄 Token refresh mechanism
- ⏲️ Configurable token expiration
- 🔍 Easy token validation and user identification
- 📦 Simple integration with FastAPI
Installation
pip install assem
Quick Start
from fastapi import FastAPI, Depends, Request, Response
from assem import AssemAUTH
app = FastAPI()
# Initialize authentication with default settings
auth = AssemAUTH(secret_key="your-secret-key")
# Login endpoint
@app.post("/login")
async def login(username: str, password: str, response: Response):
# Validate user credentials (replace with your authentication logic)
if username == "demo" and password == "password":
user_id = "user123"
# Create tokens (access, refresh, csrf)
access_token, refresh_token, csrf_token = auth.create_tokens(
user_id,
additional_data={"role": "admin", "name": "Demo User"}
)
# Set tokens in cookies
auth.set_tokens_in_cookies(response, access_token, refresh_token, csrf_token)
return {"message": "Login successful"}
else:
return {"message": "Invalid credentials"}
# Protected endpoint using dependency
@app.get("/me")
async def get_current_user(user_data = Depends(auth.get_user_data_dependency())):
return {
"user_id": user_data["sub"],
"role": user_data.get("role"),
"name": user_data.get("name")
}
# Refresh token endpoint
@app.post("/refresh")
async def refresh_token(request: Request, response: Response):
result = auth.refresh_access_token(request, response)
return result
# Logout endpoint
@app.post("/logout")
async def logout(request: Request, response: Response):
result = auth.logout(request, response)
return result
Advanced Usage
Custom Token Configuration
auth = AssemAUTH(
secret_key="your-secret-key",
algo="HS256",
access_token_expire_minutes=15,
refresh_token_expire_days=7,
token_issuer="your-api",
token_audience=["your-frontend"],
secure_cookies=True,
cookie_domain="yourdomain.com",
enable_csrf_protection=True,
enable_jti=True
)
Verification-Only Mode
You can create an instance that only verifies tokens (useful for microservices):
from assem import AssemAUTH, ServiceMode
auth_verifier = AssemAUTH(
secret_key="your-secret-key",
service_mode=ServiceMode.VERIFY_ONLY
)
Working with User Data
The framework provides multiple ways to access user data:
# Get just the user ID
@app.get("/user-id")
async def get_user_id(request: Request):
user_id = auth.get_current_user(request)
return {"user_id": user_id}
# Get a specific field from the token
@app.get("/user-role")
async def get_user_role(request: Request):
role = auth.get_user_data(request, key="role")
return {"role": role}
# Get all user data from token
@app.get("/user-all")
async def get_all_user_data(request: Request):
all_data = auth.get_user_data(request)
return all_data
License
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Metadata
Release files for assem 0.3.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| assem-0.3.1.tar.gz | 12.1 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| assem-0.3.1-py2.py3-none-any.whl | Python 2, Python 3 | none | any | Details |
Total release size: 22.9 kB
Release files / assem-0.3.1.tar.gz
| Download URL | assem-0.3.1.tar.gz |
|---|---|
| Size | 12.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e864ff374529b3f9915d48ba002078aba3c5b8258381f4f3f9aa254de545db3d
|
|
BLAKE2b-256 checksum How to use checksums |
a035f0887fadba49a092fec038dc087617955ceb8d93101bcf5062b209888b5f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.2
|
Release files / assem-0.3.1-py2.py3-none-any.whl
| Download URL | assem-0.3.1-py2.py3-none-any.whl |
|---|---|
| Size | 10.8 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
ed4c0b7b3cba188c7e0da9c1d50fea78cde0061b42dd59ed4fddc494249af109
|
|
BLAKE2b-256 checksum How to use checksums |
9d3bf53f66b6b9987071c08e2bb3f9f7cda8003dc1c1661745a54f193ed525c7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.2
|