Skip to main content

Drop-in role-based access control for FastAPI — roles, permissions, decorators.

Project description

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.

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

fastapi_rbac_kit-0.1.0.tar.gz (7.0 kB view details)

Uploaded Source

Built Distribution

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

fastapi_rbac_kit-0.1.0-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_rbac_kit-0.1.0.tar.gz.

File metadata

  • Download URL: fastapi_rbac_kit-0.1.0.tar.gz
  • Upload date:
  • Size: 7.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for fastapi_rbac_kit-0.1.0.tar.gz
Algorithm Hash digest
SHA256 ca3e0f2ac1f16cc33fc7e3f95434b29945c42987c8d5278264fbee2b1255de3f
MD5 ea714a49108d1e943bef7898360d1dd9
BLAKE2b-256 9523bd1ede5351a4811f824085fa3eb9d466637329a71754fe7dcea0bb56ac80

See more details on using hashes here.

File details

Details for the file fastapi_rbac_kit-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_rbac_kit-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 19e81a43a41cfbd739101f08f8844599be14f067713adb27f36cd679c33eaea4
MD5 ad2e9d717a129fbe4fdcba73ea5a4737
BLAKE2b-256 941c23a9256bb82394a18f2ee94804f307101e0a4367793bbbf5ae5db1d57ac7

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