Minimal, secure JWT/JWE + RBAC for FastAPI. Provides decorators and utilities for secure, role-based access control in modern Python web APIs.
Project description
python-json-rbac
Minimal, secure JWT/JWE + RBAC for FastAPI. Provides decorators and utilities for secure, role-based access control in modern Python web APIs.
Table of Contents
- Overview
- Features
- Installation
- Quickstart
- Configuration & .env Support
- Usage Example
- Advanced Usage & API
- Testing
- Security & Logging
- Contributing & Support
- License
Overview
python-json-rbac provides decorators and utilities for secure, role-based access control (RBAC) in modern Python web APIs. It supports JWT and JWE tokens, integrates with FastAPI, and is designed for modular, scalable, and secure backend architectures.
Features
- JWT and optional JWE (encrypted JWT) support
- Role-based access control (RBAC) decorators
- FastAPI dependency integration
- Modular, service-oriented design
- Secure defaults and environment-based configuration
- Support for multiple user roles
- Extensible for custom permission logic
- .env support for easy configuration
- Production-grade logging
Installation
pip install python-json-rbac
Quickstart
-
Add a
.envfile to your project root:JWT_SECRET=your_super_secret_key JWT_ALGORITHM=HS256 JWT_EXPIRE_MINUTES=30
-
Install dependencies (if not already):
pip install python-json-rbac
-
Create a FastAPI app:
from python_json_rbac.auth import create_access_token, get_current_user from python_json_rbac.decorators import rbac_protect from fastapi import FastAPI, Depends app = FastAPI() @app.get("/admin") @rbac_protect(role="admin") def admin_dashboard(user=Depends(get_current_user)): return {"message": f"Welcome, {user['sub']}!"}
Configuration & .env Support
- All configuration can be set via environment variables or a
.envfile (recommended for development). - Supported variables:
JWT_SECRET(required)JWT_ALGORITHM(HS256orRS256)JWT_PRIVATE_KEY_PATH,JWT_PUBLIC_KEY_PATH(for RS256)JWT_ENABLE_JWE(optional, default: false)JWT_EXPIRE_MINUTES(default: 30)- See documentation/docs/configuration/env-vars.md for full details.
Usage Example
Symmetric (HS256) Example
from python_json_rbac.auth import create_access_token, get_current_user
from python_json_rbac.decorators import rbac_protect
from fastapi import FastAPI, Depends
app = FastAPI()
@app.get("/admin")
@rbac_protect(role="admin")
def admin_dashboard(user=Depends(get_current_user)):
return {"message": f"Welcome, {user['sub']}!"}
Asymmetric (RS256) Example
# In your .env:
# JWT_ALGORITHM=RS256
# JWT_PRIVATE_KEY_PATH=path/to/private.pem
# JWT_PUBLIC_KEY_PATH=path/to/public.pem
from python_json_rbac.auth import create_access_token
# ...rest is the same as above
Advanced Usage & API
- See the documentation/docs/ directory for advanced RBAC, JWE, key rotation, and API reference.
- Example: documentation/docs/usage.md
Testing
To run tests:
pip install pytest
pytest
Security & Logging
- All warnings and errors use Python's
loggingmodule for production readiness. - Secrets are validated for length and entropy.
- JWE encryption and key rotation are supported.
- See documentation/docs/configuration/best-practices.md for best practices.
Contributing & Support
- Contributions are welcome! Please open issues or submit pull requests on GitHub.
- For questions, use GitHub Discussions or file an issue.
License
LGPL-2.1-only. See LICENSE 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 python_json_rbac-0.3.0.tar.gz.
File metadata
- Download URL: python_json_rbac-0.3.0.tar.gz
- Upload date:
- Size: 29.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7448b8f9448bfea3efe20d69e302d4c33267eff085a0c0702c7cdbca6cf7ba30
|
|
| MD5 |
1a86e52ff28f4444b6be6de200cad892
|
|
| BLAKE2b-256 |
0c1e585a3a0be0a7e6e2b6eff2825a7fcab3b8b881ca8f6fb3d471931bbdce8f
|
File details
Details for the file python_json_rbac-0.3.0-py3-none-any.whl.
File metadata
- Download URL: python_json_rbac-0.3.0-py3-none-any.whl
- Upload date:
- Size: 30.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0433a0dd029147c88a8d75321a0e072db542d9f358d4252af7e16a5aff9ba935
|
|
| MD5 |
fb9ee1d056bcf85e5248b401d19533b9
|
|
| BLAKE2b-256 |
dd7405d4cfd07f5b864afc8ddefd483c2c5ca845e46aba544af0ce42f82b2998
|