Skip to main content

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 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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python_json_rbac-0.3.0.tar.gz (29.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

python_json_rbac-0.3.0-py3-none-any.whl (30.9 kB view details)

Uploaded Python 3

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

Hashes for python_json_rbac-0.3.0.tar.gz
Algorithm Hash digest
SHA256 7448b8f9448bfea3efe20d69e302d4c33267eff085a0c0702c7cdbca6cf7ba30
MD5 1a86e52ff28f4444b6be6de200cad892
BLAKE2b-256 0c1e585a3a0be0a7e6e2b6eff2825a7fcab3b8b881ca8f6fb3d471931bbdce8f

See more details on using hashes here.

File details

Details for the file python_json_rbac-0.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for python_json_rbac-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0433a0dd029147c88a8d75321a0e072db542d9f358d4252af7e16a5aff9ba935
MD5 fb9ee1d056bcf85e5248b401d19533b9
BLAKE2b-256 dd7405d4cfd07f5b864afc8ddefd483c2c5ca845e46aba544af0ce42f82b2998

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page