Skip to main content

Two-layer RBAC (system + company) helpers for FastAPI / Flask / any Python backend — paired with the snipe-auth-rbac TS sibling.

Project description

snipe-auth-rbac (Python)

PyPI License: MIT

Python sibling of the snipe-auth-rbac npm package. Two-layer RBAC (system + per-company roles) for FastAPI, Flask, or any Python backend that talks to a Supabase / Postgres database.

The full picture — including the SQL schema, the TypeScript / React side, and the admin UI building blocks — lives in the top-level repo.

Install

pip install "snipe-auth-rbac[fastapi]"

Optional extras:

Extra What it pulls in
fastapi fastapi, starlette — needed for auth_rbac.deps
supabase supabase-py
dev ruff, pytest

Apply the SQL once

The tables and resolver functions live in a dedicated rbac schema. The simplest path on Supabase is the bundled npm CLI (a plain Node script — no JS knowledge needed):

npx snipe-auth-rbac install
supabase db push
# Then in Studio → Settings → API → Exposed schemas → add `rbac`

For other Postgres tooling, the canonical SQL files live at sql/0001_initial.sql and 0002_seed_defaults.sql in the repo.

FastAPI integration

from fastapi import FastAPI, Depends
from auth_rbac import PermissionsService, ResourceDescriptor
from auth_rbac.deps import (
    configure_auth_rbac,
    active_company_middleware,
    require_permission,
    require_system_permission,
)
from app.integrations.supabase import get_supabase_client

RESOURCES = [
    ResourceDescriptor("system_audit", "system",  "Audit-Log",      group="Plattform"),
    ResourceDescriptor("properties",   "company", "Liegenschaften", group="Stammdaten"),
    ResourceDescriptor("payments",     "company", "Zahlungen",      group="Finanzen"),
]

app = FastAPI()
service = PermissionsService(supabase=get_supabase_client(), resources=RESOURCES)

# Fail loudly on boot if the migration hasn't been applied or
# `rbac` isn't in the project's PostgREST exposed-schemas list.
service.require_schema()

# Upsert the typed registry into rbac.resources on every boot.
# Adding a new entry to RESOURCES + redeploying makes the matrix
# UI grow a new column — no manual migration step.
service.sync_resources()

async def resolve_user_id(request):
    return request.state.user_id   # adapter to your existing auth dep

configure_auth_rbac(service=service, user_id_resolver=resolve_user_id)
app.middleware("http")(active_company_middleware)

@app.get("/payments")
async def list_payments(_=Depends(require_permission("payments", "read"))):
    ...

@app.post("/system/companies")
async def create_company(
    _=Depends(require_system_permission("system_companies", "write")),
):
    ...

Other backends

auth_rbac.deps is FastAPI-specific, but PermissionsService itself is plain Python. For Flask / Litestar / starlette-bare:

from auth_rbac import PermissionsService

# Where you'd normally check role:
allowed = service.user_can(
    user_id=current_user.id,
    resource="payments",
    action="update",
    company_id=request.state.company_id,
)
if not allowed:
    abort(403)

Hydrating the full profile

For a /me/profile endpoint that returns every role + permission the user holds in one round-trip:

profile = service.hydrate_profile(user_id)
# profile.system_roles, profile.system_permissions,
# profile.memberships -> [CompanyMembership(roles, permissions, …)]

The result is cached for 30 seconds per user. Swap _ProfileCache for a Redis implementation if you need cross-process invalidation.

Pre-filling templates

service.apply_template_defaults(role_id="…", only_missing=True)
# Reads default_permissions JSONB on the role and inserts
# rbac.role_permissions rows for every resource that matches.

License

MIT.

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

snipe_auth_rbac-0.3.1.tar.gz (10.2 kB view details)

Uploaded Source

Built Distribution

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

snipe_auth_rbac-0.3.1-py3-none-any.whl (12.4 kB view details)

Uploaded Python 3

File details

Details for the file snipe_auth_rbac-0.3.1.tar.gz.

File metadata

  • Download URL: snipe_auth_rbac-0.3.1.tar.gz
  • Upload date:
  • Size: 10.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for snipe_auth_rbac-0.3.1.tar.gz
Algorithm Hash digest
SHA256 19645363c6db2f562ec25655d1dee618a4f4cb48bcf3f2d1f7600d65f43dda0b
MD5 0843e7acc2dd79a55986761a3828bc30
BLAKE2b-256 96f2c727f331622289cd664c44da9101e02cf672b2899093f8802fc20c0c2ddf

See more details on using hashes here.

File details

Details for the file snipe_auth_rbac-0.3.1-py3-none-any.whl.

File metadata

File hashes

Hashes for snipe_auth_rbac-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 222530e06c6de597903f43ec93f74727130d66fa37516061b8b08e0086a79137
MD5 6c56612672fd2ec04b8a2feb3cd36447
BLAKE2b-256 44bcbd56012f87769a97dd59e54b8208df18d13b27b69aebe47d1b2fd54af296

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