Authentication core utilities including JWT token management.
Project description
FastAPI Auth Core
A robust authentication package for FastAPI applications that provides JWT token management, user authentication, and middleware functionality.
Features
- JWT token generation and validation
- Password hashing and verification
- User authentication middleware
- Refresh token management
- SQLAlchemy integration
Installation
pip install fastapi-auth-core
Dependencies
- Python >= 3.13
- FastAPI >= 0.115.12
- PyJWT >= 2.10.1
- Passlib >= 1.7.4
Usage
Basic Setup
from fastapi import FastAPI, Depends
from fastapi_auth_core import AuthFunctions, JWTFunctions
from sqlalchemy.orm import Session
app = FastAPI()
# Initialize JWT functions
jwt_functions = JWTFunctions(
secret_key="your-secret-key",
algorithm="HS256",
expire_minutes=30
)
# Initialize Auth functions with your database session and User model
auth_functions = AuthFunctions(
db_session=lambda: Session(),
User=YourUserModel
)
Authentication Middleware
from fastapi_auth_core import auth_middleware
# Create the authentication middleware
auth = auth_middleware(
db_session=lambda: Session(),
Users=YourUserModel,
jwt_secret_key="your-secret-key",
jwt_algorithm="HS256"
)
# Use the middleware in your routes
@app.get("/protected")
async def protected_route(user = Depends(auth)):
return {"message": f"Hello, {user.username}!"}
Token Generation
# Generate access and refresh tokens
tokens = jwt_functions.generate_tokens(
data={"sub": "user123"},
user_id=1,
db=lambda: Session(),
User=YourUserModel
)
# The tokens dictionary contains:
# {
# "access_token": "...",
# "refresh_token": "...",
# "token_type": "bearer"
# }
Password Management
# Hash a password
hashed_password = jwt_functions.get_password_hash("user_password")
# Verify a password
is_valid = jwt_functions.verify_password("user_password", hashed_password)
User Management
# Get user by attribute
user = auth_functions.get_user_by_attribute("email", "user@example.com")
# Update user's refresh token
await auth_functions.update_user_refresh_token(user_id=1, refresh_token="new_token")
Security Features
- Secure password hashing using bcrypt
- JWT token expiration
- Refresh token rotation
- Type-safe database operations
- Exception handling for authentication failures
Best Practices
- Always use HTTPS in production
- Store sensitive configuration (secret keys, etc.) in environment variables
- Implement proper error handling
- Use appropriate token expiration times
- Implement rate limiting for authentication endpoints
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License - see the LICENSE file for details.
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 fastapi_auth_core-0.1.2.tar.gz.
File metadata
- Download URL: fastapi_auth_core-0.1.2.tar.gz
- Upload date:
- Size: 4.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.2 CPython/3.13.3 Darwin/23.6.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9eb7affaf6d4599dcf21faecd0a015674587e063cc70c115253471208caed053
|
|
| MD5 |
2a528f2f014ef5850b2bf71895a886a1
|
|
| BLAKE2b-256 |
bf65136f9ae7a1dcb9380ae5b8d07ee061786653335c41c8a5546c5e426dd413
|
File details
Details for the file fastapi_auth_core-0.1.2-py3-none-any.whl.
File metadata
- Download URL: fastapi_auth_core-0.1.2-py3-none-any.whl
- Upload date:
- Size: 6.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.2 CPython/3.13.3 Darwin/23.6.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a6285bb6e4e3f807ca4731ae9a7a0fce94f0fa418feca68e428841b61206b071
|
|
| MD5 |
b9668b60b74ba6e36ba18e255fd6d6ee
|
|
| BLAKE2b-256 |
b6966ff5b831a8266c09782a0cc9dbd80c266a4738e1c56fddfd4ecadd0af7ca
|