Skip to main content

fastapi-rbac-lite

Pluggable RBAC (role/permission) checking for FastAPI. Bring your own token verifier and your own permission store — get a clean require_any_permission dependency for your routes.

Most FastAPI auth libraries assume one specific setup: permissions baked into the JWT itself, or one specific database schema. fastapi-rbac-lite instead defines two small interfaces — how to verify a token and how to resolve a group into permissions — and lets you plug in whatever fits your infrastructure, with a ready-to-use default for the common case (JWT + JWKS, HTTP-based RBAC service).

Install

pip install fastapi-rbac-lite

Quickstart

from fastapi import FastAPI, Depends
from fastapi_rbac_lite import RBAC, JWKSVerifier, HTTPPermissionResolver

app = FastAPI()

rbac = RBAC(
    verifier=JWKSVerifier(
        trusted_issuers=["https://auth.example.com/application/o/myapp"],
        audiences=["myapp-backend"],
    ),
    resolver=HTTPPermissionResolver(
        base_url="http://rbac-service:8001",
        endpoint="/roles/permissions-bulk",
    ),
    bypass=lambda payload: payload.get("preferred_username") == "internal-service-account",
)

@app.get(
    "/invoices",
    dependencies=[Depends(rbac.require_any_permission(["invoice:read", "invoice:approve"]))],
)
def get_invoices():
    ...

require_any_permission([...]) passes if the caller's groups resolve to at least one of the listed permissions. Call it with no arguments (or an empty list) to require only a valid token, with no permission check.

Use rbac.get_current_user as a dependency when you just need "any authenticated user," with no permission check at all.

Why two interfaces instead of one library-shaped solution

  • TokenVerifier — how do you know who's calling? The built-in JWKSVerifier verifies RS256 JWTs against one or more trusted issuers via their published JWKS, with per-issuer key caching. Using a non-JWT scheme? Implement your own class with an async verify(token) -> dict method — no inheritance required.

  • PermissionResolver — where do group → permission mappings live? Ship your own resolver, or use one of:

    • HTTPPermissionResolver — calls an external RBAC microservice
    • StaticPermissionResolver — in-memory dict, useful for tests/local dev

Both are typing.Protocols, so any object with the right method signature works — you don't need to import or subclass anything from this package.

Writing your own resolver

class RedisPermissionResolver:
    def __init__(self, redis_client):
        self.redis = redis_client

    async def resolve(self, groups: list[str]) -> set[str]:
        perms = set()
        for g in groups:
            perms |= set(await self.redis.smembers(f"group:{g}:permissions"))
        return perms

Pass it straight into RBAC(resolver=RedisPermissionResolver(...)).

License

MIT

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_lite-0.1.1.tar.gz (7.1 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_lite-0.1.1-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_rbac_lite-0.1.1.tar.gz.

File metadata

  • Download URL: fastapi_rbac_lite-0.1.1.tar.gz
  • Upload date:
  • Size: 7.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.14.3

File hashes

Hashes for fastapi_rbac_lite-0.1.1.tar.gz
Algorithm Hash digest
SHA256 3a4acb392347d9b64504692de34e1ee124c7d6c07c1be17c2b5b40201cd6c2f8
MD5 63b4b6db5056621ac0715c0c88053ba7
BLAKE2b-256 f255b6e357ace5ab41c2d96ed540c749943a28bdeca642533d1a2eac5de4c3e0

See more details on using hashes here.

File details

Details for the file fastapi_rbac_lite-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_rbac_lite-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a15df141b820c2554d9d98db71696e7a6b9f41809c30ae9d30fabcb88bcde87f
MD5 26b73d970ce62c5574c194afe81a548d
BLAKE2b-256 2a910fe8c57a03f588755283392dd42117a75bed426d7867276ec3046ecbdfa9

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 files

0.1.0

2 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