Skip to main content

python-json-rbac Logo

python-json-rbac

PyPI version PyPI Downloads License: LGPL v2.1 Python Version Buy Me A Coffee

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

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

  1. Add a .env file to your project root:

    JWT_SECRET=your_super_secret_key
    JWT_ALGORITHM=HS256
    JWT_EXPIRE_MINUTES=30
    
  2. Install dependencies (if not already):

    pip install python-json-rbac
    
  3. 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 .env file (recommended for development).
  • Supported variables:
    • JWT_SECRET (required)
    • JWT_ALGORITHM (HS256 or RS256)
    • 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

Testing

To run tests:

pip install pytest
pytest

Security & Logging

Contributing & Support

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)

Source distribution for python-json-rbac 0.3.0
File Size Uploaded
python_json_rbac-0.3.0.tar.gz 29.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for python-json-rbac 0.3.0
File Interpreter ABI Platform
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

Release history Release notifications | RSS feed

This release

0.3.0 This release

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page