Skip to main content

fastapi-auth-saml-federated

Federated SAML2 Service Provider for FastAPI — Shibboleth / eduGAIN / NREN AAIs (DFN-AAI, SWITCHaai, SURFconext, SWAMID). Part of the fastapi_auth package family.

Install

The SP shells out to the xmlsec1 command-line tool for XML signing/verification (via pysaml2) — install it via your system package manager first:

sudo apt-get install --no-install-recommends xmlsec1   # Debian/Ubuntu

Then the package:

uv pip install fastapi-auth-saml-federated

The session store is pluggable; the default in-memory store needs nothing extra, Redis- or Postgres-backed stores need their extra (see Backends):

uv pip install "fastapi-auth-saml-federated[redis]"
uv pip install "fastapi-auth-saml-federated[postgres]"

Quickstart

from fastapi import Depends, FastAPI

from fastapi_auth import saml

app = FastAPI()

sp = saml.SamlSP(
    saml.SamlSettings(
        entity_id="https://sp.example.org/saml/metadata",
        base_url="https://sp.example.org",
        key_file="sp-key.pem",
        cert_file="sp-cert.pem",
        idp_metadata_file="idp-metadata.xml",
        fixed_idp_entity_id="https://idp.example.org/idp/shibboleth",
        session_secret="change-me-to-a-long-random-value",
    )
)
sp.mount(app)
# equivalent to: app.include_router(sp.router, prefix=sp.settings.mount_path)
# -- only use include_router() directly if prefix matches settings.mount_path exactly.

current_user = sp.current_user()


@app.get("/whoami")
async def whoami(identity: saml.FederatedIdentity = Depends(current_user)):
    return {"subject": sp.identifier(identity)}

sp.current_user() is a dependency raising 401 without an active session; sp.optional_user() returns the identity or None instead.

Development

uv venv && source .venv/bin/activate
make install          # uv pip install -U -e ".[dev]"
make test-local

Docker

A multi-stage Dockerfile (slim runtime image, xmlsec1 baked in) and a compose.yml (Redis + Postgres, for exercising the store backends against the real thing) are provided:

docker build -t fastapi-auth-saml-federated .
make test-integration   # docker compose up -d --wait, run integration tests, teardown

See docs/howto/docker.md for details, including local port overrides via compose.override.yml.

Backends

Session backend (cookie default, or jwt) and session store (memory default, or redis/postgres) are chosen independently via SamlSettings(backend=..., store=..., redis_url=..., db_url=...). The jwt backend requires a signing secret of at least 32 bytes. See docs/howto/stores.md for the full trade-offs and extras.

Federation

Metadata can come from a single bilateral IdP (metadata_source="direct") or from a federation ("aggregate" / "mdq"); the latter two require a trust_anchor_cert to verify signatures on metadata fetched over the network. Discovery of the right IdP is "passthrough" (single fixed IdP), "external" (redirect to a federation Discovery Service via ds_url), or "embedded" (a WAYF page rendered by the SP itself). See the how-tos for testing against a local SimpleSAMLphp IdP, the LMU lmuidp-container (Shibboleth), and the DFN-AAI test federation.

Authentication context and logout

A login can request an authentication context such as REFEDS MFA and have the asserted one checked -- per login via SamlEngine, or for the whole router via SamlSP(settings, requested_authn_context=...); see docs/howto/authn-context.md. Logging out is a POST to {mount_path}/slo with a CSRF token; GET {mount_path}/slo only renders a confirmation form (docs/howto/logout.md).

Extensions and raw attributes

SamlEngine.create_authn_request(extensions=[...]) adds namespaced XML to <samlp:Extensions> for one login; SamlEngine.parse_response_details() also returns every <saml:Attribute> with its XML attributes. The fastapi_auth.saml.akdb module builds BundID's akdb:AuthenticationRequest and reads akdb:TrustLevel; see docs/howto/extensions.md.

Docs

Full documentation (Sphinx + MyST) lives in docs/:

uv pip install -U -e ".[docs]"
uv run sphinx-build -b html docs docs/_build

Open docs/_build/index.html, or read the sources directly under docs/index.md and docs/howto/.

License

Dual-licensed: Apache-2.0 OR EUPL-1.2 — the recipient may choose either. See LICENSE-APACHE and LICENSE-EUPL.

Metadata

Release files for fastapi-auth-saml-federated 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fastapi-auth-saml-federated 0.3.0
File Size Uploaded Provenance Actions
fastapi_auth_saml_federated-0.3.0.tar.gz 59.2 kB With provenance Details for fastapi_auth_saml_federated-0.3.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for fastapi-auth-saml-federated 0.3.0
File Interpreter ABI Platform Actions
fastapi_auth_saml_federated-0.3.0-py3-none-any.whl Python 3 none any Details for fastapi_auth_saml_federated-0.3.0-py3-none-any.whl

Total release size: 118.1 kB

Release files / fastapi_auth_saml_federated-0.3.0.tar.gz

Download URL fastapi_auth_saml_federated-0.3.0.tar.gz
Size 59.2 kB
Tags Source
SHA-256 checksum
How to use checksums
4bf6c048d8ee25d419205e401c506711d6a7c8b96ebd336c9e40da0d3e90c5e0
BLAKE2b-256 checksum
How to use checksums
b807bb88213649e6b7468165fe7acc8447b3aa58926df099ffa1bed0bfe82d52
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 5, 2026.

Transparency log

Release files / fastapi_auth_saml_federated-0.3.0-py3-none-any.whl

Download URL fastapi_auth_saml_federated-0.3.0-py3-none-any.whl
Size 58.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9a7b8c91e4b8aa014416667be95034d42558616eaa0a7b20c26a9c819c93b9d5
BLAKE2b-256 checksum
How to use checksums
fd5ee13d87ab71c832c14b8cd109d5dd773bdc6b5e13a65f9e56a9caf255f363
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 5, 2026.

Transparency log

Security for fastapi-auth-saml-federated 0.3.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.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for fastapi_auth_saml_federated-0.3.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 5, 2026.

Transparency log

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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page