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.2.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.2.0-py3-none-any.whl (4.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: casbin_fastapi_decorator_file-1.2.0.tar.gz
  • Upload date:
  • Size: 2.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","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.2.0.tar.gz
Algorithm Hash digest
SHA256 598064322d15c6cd05df9d84852a4dce50d8b3c7f29d1164b524a2708451162b
MD5 6232469118151c94bf9fce82808030d2
BLAKE2b-256 7a8e55bfec2e25b90d8364001514cf9466f09bc0868da939246c85aa248c56a8

See more details on using hashes here.

File details

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

File metadata

  • Download URL: casbin_fastapi_decorator_file-1.2.0-py3-none-any.whl
  • Upload date:
  • Size: 4.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","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.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ee30e3e36a94e894f40b32cd30d4c805500e4f6a56813a57e5f6a79126d58dbe
MD5 4bfcacdf8a5e696bbd9debc136afae62
BLAKE2b-256 ecd7c17e3e01a2687f902297773b2b6ecfb44f286de979fa076aa5ac7c914157

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