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.

Signatures and the IdP sign-in endpoint

By default the SP requires a signed assertion. want_response_signed=True (with want_assertions_signed=False, or True for both) requires a signed response instead; see docs/howto/signatures.md. SamlEngine.idp_sso_url() returns the IdP's SingleSignOnService location, for example for a CSP form-action; see docs/howto/sso-endpoint.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.4.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.4.0
File Size Uploaded Provenance Actions
fastapi_auth_saml_federated-0.4.0.tar.gz 60.5 kB With provenance Details for fastapi_auth_saml_federated-0.4.0.tar.gz

Built distribution (wheel)

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

Total release size: 120.5 kB

Release files / fastapi_auth_saml_federated-0.4.0.tar.gz

Download URL fastapi_auth_saml_federated-0.4.0.tar.gz
Size 60.5 kB
Tags Source
SHA-256 checksum
How to use checksums
9907da6feede4d4ca9bb350fb61438765083b85b742c8793c05550e09f4c9b4d
BLAKE2b-256 checksum
How to use checksums
9f4db8fe56b8c87fbe4fb13bddc947b834a9f3a5cd61481fbd703192a4771f25
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 6, 2026.

Transparency log

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

Download URL fastapi_auth_saml_federated-0.4.0-py3-none-any.whl
Size 60.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1f512c7e8eab07878ae5a935ff4a97bcbd90ac0407c5294d6274f4f23b305253
BLAKE2b-256 checksum
How to use checksums
1b07c10a07c90ea59966d0be4fe9811b06245c50d9b8ac75719a16919ce942d2
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 6, 2026.

Transparency log

Security for fastapi-auth-saml-federated 0.4.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.4.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 6, 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