Skip to main content

File-based enforcer provider with hot-reload for casbin-fastapi-decorator

Project description

casbin-fastapi-decorator-file

File-based enforcer provider with hot-reload for casbin-fastapi-decorator.

Provides CachedFileEnforcerProvider — loads the casbin Enforcer once from model.conf + policy.csv and automatically reloads when either file changes on disk (via watchdog).

Install

pip install casbin-fastapi-decorator-file

Usage

from contextlib import asynccontextmanager
from fastapi import FastAPI, HTTPException
from casbin_fastapi_decorator import PermissionGuard
from casbin_fastapi_decorator_file import CachedFileEnforcerProvider

provider = CachedFileEnforcerProvider(
    model_path="casbin/model.conf",
    policy_path="casbin/policy.csv",
)

@asynccontextmanager
async def lifespan(app: FastAPI):
    async with provider:   # starts file watcher
        yield              # stops file watcher on shutdown

guard = PermissionGuard(
    user_provider=get_current_user,
    enforcer_provider=provider,
    error_factory=lambda *_: HTTPException(403, "Forbidden"),
)

app = FastAPI(lifespan=lifespan)

@app.get("/articles")
@guard.require_permission("articles", "read")
async def list_articles(): ...

Any change to model.conf or policy.csv is detected automatically — the enforcer reloads on the next request with zero downtime.

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

casbin_fastapi_decorator_file-1.1.0.tar.gz (2.8 kB view details)

Uploaded Source

Built Distribution

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

casbin_fastapi_decorator_file-1.1.0-py3-none-any.whl (4.0 kB view details)

Uploaded Python 3

File details

Details for the file casbin_fastapi_decorator_file-1.1.0.tar.gz.

File metadata

  • Download URL: casbin_fastapi_decorator_file-1.1.0.tar.gz
  • Upload date:
  • Size: 2.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.6 {"installer":{"name":"uv","version":"0.11.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for casbin_fastapi_decorator_file-1.1.0.tar.gz
Algorithm Hash digest
SHA256 17ee227a789921df469f327c1aceb6fe97e1ebd7bdb9fca06e4e8f86030d5216
MD5 d4fc389cc6dff861af6a816231ef97df
BLAKE2b-256 e5fb82d9a56a9dab88ab8c2b19227eb6115926baf74bfc8a1bc6aa7581a37b6f

See more details on using hashes here.

File details

Details for the file casbin_fastapi_decorator_file-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: casbin_fastapi_decorator_file-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.6 {"installer":{"name":"uv","version":"0.11.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for casbin_fastapi_decorator_file-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4fff42f23a123c1a1bba2273d5238a1289cfffcbf521e97867e20964b37eb271
MD5 b77d63e6f5ee1f8769425a6c5432cb91
BLAKE2b-256 73f20364ef8faee1fd679915ca1090144210481fea8d28fd7cf71864e8732a6a

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