sapl-flask
Policy-based authorization for Flask. 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.
Built on sapl-base and the SAPL 4.1 enforcement model: planner-driven constraint handling, the SUSPEND decision verb, an optional RSocket transport, and transaction rollback on post-write denial. Data-layer query rewriting is available via sapl-sqlalchemy (SQL) and sapl-pymongo (MongoDB).
How It Works
Your application decorates view functions 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")
def get_patient(patient_id):
return {"id": patient_id, "name": "Jane Doe", "ssn": "123-45-6789"}
policy "permit doctors to read patient data"
permit
action == "read";
"DOCTOR" in subject.roles
If the PDP permits, the view 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, the single stream_enforce decorator maintains 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_provider on the SaplFlask extension.
Database Transactions
If you configure a transaction provider, a denial that lands after the view has written to the database rolls the transaction back. Three triggers cause a rollback: a post_enforce DENY, a post_enforce output-obligation failure, and a pre_enforce output-obligation failure (the pre-decision permits, but its output obligations run after the method writes). A clean permit commits. It is opt-in: with no provider set, the PEP owns no transaction.
set_transaction_provider is a method on the extension. Flask views are synchronous, so wrap a sync session or transaction.atomic with from_sync_context:
from sapl_base.pep import from_sync_context
sapl = SaplFlask(app)
sapl.set_transaction_provider(from_sync_context(lambda: get_current_session().begin()))
The factory should resolve the current request's session. With an async SQLAlchemy session you can pass the async scope directly: sapl.set_transaction_provider(lambda: get_current_session().begin()).
Getting Started
pip install sapl-flask
from flask import Flask
from sapl_flask.extension import SaplFlask
from sapl_flask.decorators import pre_enforce
app = Flask(__name__)
app.config["SAPL_BASE_URL"] = "https://localhost:8443"
sapl = SaplFlask(app)
For setup instructions, configuration options, the constraint handler reference, and the full API, see the Flask documentation.
Links
License
Apache-2.0
Metadata
Release files for sapl-flask 4.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| sapl_flask-4.1.0.tar.gz | 16.3 kB | Without provenance | Details for sapl_flask-4.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| sapl_flask-4.1.0-py3-none-any.whl | Python 3 | none | any | Details for sapl_flask-4.1.0-py3-none-any.whl |
Total release size: 25.6 kB
Release files / sapl_flask-4.1.0.tar.gz
| Download URL | sapl_flask-4.1.0.tar.gz |
|---|---|
| Size | 16.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
fc8ea01b35d9c9e9d9f8e2af1ace923633f5816e6346771e0969f043093f5fd4
|
|
BLAKE2b-256 checksum How to use checksums |
24034d77daee36111add4cadbe128464fd9e9677b102fb70d281db7a9834e3ae
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.13
|
Release files / sapl_flask-4.1.0-py3-none-any.whl
| Download URL | sapl_flask-4.1.0-py3-none-any.whl |
|---|---|
| Size | 9.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
7b1e1050f4d79c9227d3c9b57d30dbe1e477277d5656e71dfaee8d523797a06c
|
|
BLAKE2b-256 checksum How to use checksums |
c16a4c52de1a5e1ffeec6d50bb9130be40b579bd0ac68e01807c15979ffa63ae
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.13
|
Security for sapl-flask 4.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.