Skip to main content

Role-based access control (RBAC) and permission management extension for oauth2fast-fastapi

Project description

permissions2fast-fastapi

🔒 Role-Based Access Control (RBAC) extension for oauth2fast-fastapi.

Easily manage user roles and permissions in your FastAPI application with support for Multi-Tenancy and High-Performance Redis Caching.

Features

  • 👥 Role Management: Create, assign, and manage roles for users.
  • 🔑 Granular Permissions: Define specific permissions and assign them to roles or directly to users (polymorphic assignments).
  • 🏢 Multi-Tenancy (Optional): Isolate roles and permissions per tenant context.
  • Redis Caching (Optional): High-performance permission evaluation using Redis to minimize database lookups.
  • �🛡️ Route Protection: Dependencies to protect endpoints based on roles or permissions.
  • Async Support: Fully async database interactions via pgsqlasync2fast-fastapi.
  • 🔌 Seamless Integration: Built to extend oauth2fast-fastapi.

Installation

pip install permissions2fast-fastapi

Configuration

This package uses the same database connection logic as oauth2fast-fastapi. Configure your environment variables in .env.

Basic Settings

# Database Configuration
DB_CONNECTIONS__AUTH__USERNAME=db_user
DB_CONNECTIONS__AUTH__PASSWORD=db_password
DB_CONNECTIONS__AUTH__HOST=localhost
DB_CONNECTIONS__AUTH__DATABASE=db_name
DB_CONNECTIONS__AUTH__PORT=5432

Advanced Features (Multi-Tenancy & Redis)

You can enable multi-tenancy and Redis caching by setting the following environment variables:

PERMISSIONS_ENABLE_TENANCY=True
PERMISSIONS_REDIS_RBAC_ENABLED=True

# Redis connection details (if caching is enabled)
PERMISSIONS_REDIS__HOST=localhost
PERMISSIONS_REDIS__PORT=6379
PERMISSIONS_REDIS__DB=0
# PERMISSIONS_REDIS__PASSWORD=your_redis_password

Usage

1. Basic Integration

from fastapi import FastAPI
from permissions2fast_fastapi import permissions_router, roles_router
from oauth2fast_fastapi import router as auth_router

app = FastAPI()

app.include_router(auth_router)
app.include_router(permissions_router)
app.include_router(roles_router)

2. Protecting Routes

Use the provided dependencies to restrict access to endpoints. The system will automatically check Redis cache if enabled, and fallback to database queries if needed. Tenant context is automatically respected if Tenancy is enabled and a Tenant ID is injected in the request context.

from fastapi import Depends
from permissions2fast_fastapi.dependencies import has_permission, has_role
from oauth2fast_fastapi.models import User

# Require a specific role
@app.get("/admin-dashboard")
async def admin_dashboard(user: User = Depends(has_role("admin"))):
    return {"message": "Welcome Admin"}

# Require a specific permission
@app.get("/edit-post")
async def edit_post(user: User = Depends(has_permission("posts.edit"))):
    return {"message": "You can edit posts"}

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

permissions2fast_fastapi-0.2.0.tar.gz (21.9 kB view details)

Uploaded Source

Built Distribution

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

permissions2fast_fastapi-0.2.0-py3-none-any.whl (27.9 kB view details)

Uploaded Python 3

File details

Details for the file permissions2fast_fastapi-0.2.0.tar.gz.

File metadata

File hashes

Hashes for permissions2fast_fastapi-0.2.0.tar.gz
Algorithm Hash digest
SHA256 52eb85f562a586a8490b3bcd5719840ed383ea75ec5928e78011687903b23adc
MD5 c0ddf85d7d3026a2bd1fab6facecde4c
BLAKE2b-256 4ae794ae2bdebb4b320922b216dc2863b82896d755dc3518ab8a5cc7d295eace

See more details on using hashes here.

File details

Details for the file permissions2fast_fastapi-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for permissions2fast_fastapi-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6f17a71fd0823de6638498ad2f68ea77bffc7e660f762361cb7ae1e20c2246d7
MD5 95da14a6125c2a5b45b09ab84172930f
BLAKE2b-256 b877ffeb14a113b16ad69bb1cfcf5ad22ae19ee6651b94a750e09a7c5891acbc

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