Skip to main content

SAPL Policy Enforcement Point (PEP) integration for FastAPI

Project description

sapl-fastapi

Policy-based authorization for FastAPI. Write access control rules as external SAPL policy files and enforce them at runtime through decorators like @pre_enforce and @post_enforce. Policies can be updated without code changes or redeployment.

How It Works

Your application decorates endpoints with enforcement decorators. SAPL intercepts the call, sends an authorization subscription to the Policy Decision Point (PDP), and enforces the decision, including any obligations or advice the policy attaches.

@app.get("/patient/{patient_id}")
@pre_enforce(action="read", resource="patient")
async def get_patient(request: Request, patient_id: str):
    return {"id": patient_id, "name": "Jane Doe", "ssn": "123-45-6789"}
policy "permit doctors to read patient data"
permit
  action == "read"
where
  "DOCTOR" in subject.roles;

If the PDP permits, the endpoint runs. If not, HTTP 403 is returned. If the decision carries obligations (like access logging or field redaction), they are enforced automatically through registered constraint handlers.

What You Get

SAPL goes beyond simple permit/deny. Decisions can carry obligations that must be fulfilled, advice that should be attempted, and resource transformations that modify return values before they reach the caller. The library handles all of this transparently.

For SSE endpoints, streaming decorators (@enforce_till_denied, @enforce_drop_while_denied, @enforce_recoverable_if_denied) maintain a live connection to the PDP, so access rights update in real time as policies, attributes, or the environment change. Built-in constraint handlers cover JSON field redaction and collection filtering. Writing custom handlers follows a simple registration pattern with register_constraint_handler.

Getting Started

pip install sapl-fastapi
from contextlib import asynccontextmanager
from fastapi import FastAPI
from sapl_fastapi.config import SaplConfig
from sapl_fastapi.dependencies import configure_sapl, cleanup_sapl

@asynccontextmanager
async def lifespan(app: FastAPI):
    configure_sapl(SaplConfig(base_url="https://localhost:8443"))
    yield
    await cleanup_sapl()

app = FastAPI(lifespan=lifespan)

For setup instructions, configuration options, the constraint handler reference, and the full API, see the FastAPI documentation.

Links

License

Apache-2.0

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

sapl_fastapi-4.0.1.tar.gz (13.0 kB view details)

Uploaded Source

Built Distribution

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

sapl_fastapi-4.0.1-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

File details

Details for the file sapl_fastapi-4.0.1.tar.gz.

File metadata

  • Download URL: sapl_fastapi-4.0.1.tar.gz
  • Upload date:
  • Size: 13.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for sapl_fastapi-4.0.1.tar.gz
Algorithm Hash digest
SHA256 2f0b9f70212c17c533e0af919caaa2f80d1c6ea3a14635f7481d9d535a4cd24e
MD5 6f364d11ed56374d94bb2dc82e8c38de
BLAKE2b-256 47214ed600f4916c015ec5d924477689b2431a188907e493750ff11d3174bf68

See more details on using hashes here.

File details

Details for the file sapl_fastapi-4.0.1-py3-none-any.whl.

File metadata

  • Download URL: sapl_fastapi-4.0.1-py3-none-any.whl
  • Upload date:
  • Size: 8.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for sapl_fastapi-4.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f293471ff54d53a77783ae6882356f4655469eb2be308522496aec2fdf86c3fc
MD5 bf36045d5a403dec37a881bd2f5f519e
BLAKE2b-256 b1337f3a20feabcc58dd76117296fd09174adb55e90d51e073e474a896b2ea32

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