Skip to main content

fastapi-rbac

PyPI version License: MIT Python 3.10+ FastAPI

Drop-in role-based access control for FastAPI. Define roles and permissions, protect endpoints with dependency injection.

Installation

pip install fastapi-rbac

Quick Start

from fastapi import FastAPI, Depends
from fastapi_rbac import RBAC, Permission, require_role, require_permission

app = FastAPI()
rbac = RBAC()  # comes with admin, editor, viewer roles

# Add a custom role
rbac.add_role("moderator", ["read", "write", "delete"])

Protecting Endpoints

By Role

Restrict access to specific roles:

from fastapi_rbac import require_role, set_default_rbac

set_default_rbac(rbac)

# Your auth dependency that returns a user object with a .role attribute
def get_current_user():
    ...

@app.get(
    "/admin/dashboard",
    dependencies=[require_role("admin", get_user=get_current_user)],
)
def admin_dashboard():
    return {"message": "Welcome, admin!"}


@app.get(
    "/content",
    dependencies=[require_role("admin", "editor", get_user=get_current_user)],
)
def manage_content():
    return {"message": "Content management"}

By Permission

Check if the user's role has a specific permission:

from fastapi_rbac import require_permission

@app.get(
    "/articles",
    dependencies=[require_permission("read", rbac=rbac, get_user=get_current_user)],
)
def list_articles():
    return {"articles": []}


@app.post(
    "/articles",
    dependencies=[require_permission("write", rbac=rbac, get_user=get_current_user)],
)
def create_article():
    return {"created": True}


@app.delete(
    "/articles/{id}",
    dependencies=[require_permission("delete", rbac=rbac, get_user=get_current_user)],
)
def delete_article(id: int):
    return {"deleted": id}

Built-in Roles

Role Permissions
admin All (wildcard)
editor read, write
viewer read

User Object Contract

Your authentication dependency must return an object with a .role attribute:

from dataclasses import dataclass

@dataclass
class User:
    id: int
    username: str
    role: str  # must match a registered role name

Custom Roles

rbac = RBAC()

# Add roles
rbac.add_role("moderator", ["read", "write", "delete"])
rbac.add_role("analyst", ["read", "export"])

# Check programmatically
rbac.check_permission("moderator", "delete")  # True
rbac.check_permission("analyst", "write")      # False

# List roles
rbac.roles  # ['admin', 'editor', 'viewer', 'moderator', 'analyst']

# Remove a role
rbac.remove_role("analyst")

Using request.state.user

If you prefer middleware-based auth, require_role and require_permission can read the user from request.state.user automatically when no get_user dependency is provided:

@app.middleware("http")
async def auth_middleware(request, call_next):
    request.state.user = authenticate(request)
    return await call_next(request)

# No get_user needed — reads from request.state.user
@app.get("/protected", dependencies=[require_role("admin")])
def protected():
    return {"ok": True}

Permission Constants

Use the Permission class for cleaner code:

from fastapi_rbac import Permission

rbac.add_role("support", [Permission.READ, Permission.WRITE])
rbac.check_permission("support", Permission.DELETE)  # False

License

MIT License. See LICENSE for details.

Release files for fastapi-rbac-kit 0.1.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 fastapi-rbac-kit 0.1.0
File Size Uploaded
fastapi_rbac_kit-0.1.0.tar.gz 7.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for fastapi-rbac-kit 0.1.0
File Interpreter ABI Platform
fastapi_rbac_kit-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 13.5 kB

Release files / fastapi_rbac_kit-0.1.0.tar.gz

Download URL fastapi_rbac_kit-0.1.0.tar.gz
Size 7.0 kB
Tags Source
SHA-256 checksum
How to use checksums
ca3e0f2ac1f16cc33fc7e3f95434b29945c42987c8d5278264fbee2b1255de3f
BLAKE2b-256 checksum
How to use checksums
9523bd1ede5351a4811f824085fa3eb9d466637329a71754fe7dcea0bb56ac80
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.5

Release files / fastapi_rbac_kit-0.1.0-py3-none-any.whl

Download URL fastapi_rbac_kit-0.1.0-py3-none-any.whl
Size 6.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
19e81a43a41cfbd739101f08f8844599be14f067713adb27f36cd679c33eaea4
BLAKE2b-256 checksum
How to use checksums
941c23a9256bb82394a18f2ee94804f307101e0a4367793bbbf5ae5db1d57ac7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.5

Release history Release notifications | RSS feed

This release

0.1.0 This release

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