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.
Release files for python-json-rbac 0.3.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 | |
|---|---|---|---|
| python_json_rbac-0.3.0.tar.gz | 29.8 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| python_json_rbac-0.3.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 60.8 kB
Release files / python_json_rbac-0.3.0.tar.gz
| Download URL | python_json_rbac-0.3.0.tar.gz |
|---|---|
| Size | 29.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7448b8f9448bfea3efe20d69e302d4c33267eff085a0c0702c7cdbca6cf7ba30
|
|
BLAKE2b-256 checksum How to use checksums |
0c1e585a3a0be0a7e6e2b6eff2825a7fcab3b8b881ca8f6fb3d471931bbdce8f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.5
|
Release files / python_json_rbac-0.3.0-py3-none-any.whl
| Download URL | python_json_rbac-0.3.0-py3-none-any.whl |
|---|---|
| Size | 30.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
0433a0dd029147c88a8d75321a0e072db542d9f358d4252af7e16a5aff9ba935
|
|
BLAKE2b-256 checksum How to use checksums |
dd7405d4cfd07f5b864afc8ddefd483c2c5ca845e46aba544af0ce42f82b2998
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.5
|