Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

Documentation Status License: MIT GitHub issues GitHub release (latest by date) GitHub top language pre-commit.ci status

FastAPI Keycloak Middleware

Full documentation is available at Read The Docs

This package provides a middleware for FastAPI that simplifies integrating with Keycloak for authentication and authorization. It supports OIDC and supports validating access tokens, reading roles and basic authentication. In addition it provides several dependencies to easily integrate into your FastAPI application.

It relies on the python-keycloak package, which is the only dependency outside of the FastAPI ecosystem which would be installed anyway. Shoutout to the author of fastapi-auth-middleware which served as inspiration for this package and some of its code.

In the future, I plan to add support for fine grained authorization using Keycloak Authorization services.

Motivation

Using FastAPI and Keycloak quite a lot, and keeping to repeat myself quite a lot when it comes to authentiating users, I decided to create this library to help with this.

There is a clear separation between the authentication and authorization:

  • Authentication is about verifying the identity of the user (who they are). This is done by an authentication backend that verifies the users access token obtained from the identity provider (Keycloak in this case).
  • Authorization is about deciding which resources can be accessed. This package provides convenience dependencies to enforce certain roles or permissions on FastAPI endpoints.

Installation

Install the package using poetry:

poetry add fastapi-keycloak-middleware

or pip:

pip install fastapi-keycloak-middleware

Features

The package helps with:

  • An easy to use middleware that validates the request for an access token
  • Validation can done in one of two ways:
    • Validate locally using the realm public keys (JWKS) obtained from Keycloak, supporting key rotation without restarts
    • Validate using the Keycloak token introspection endpoint
  • Using Starlette authentication mechanisms to store both the user object as well as the authorization scopes in the Request object
  • Ability to provide custom callback functions to retrieve the user object (e.g. from your database) and to provide an arbitrary mapping to authentication scopes (e.g. roles to permissions)
  • Support for service principals (client credentials tokens) with a configurable detection, working with any identity provider
  • A dependency to enforce certain roles or permissions on FastAPI endpoints, for users and services alike
  • Convenience dependencies to retrieve the user or service object, the authorization scopes or all token claims within the FastAPI endpoint

Upgrading from 1.x? See the migration guide.

Acknowledgements

This package is heavily inspired by fastapi-auth-middleware which provides some of the same functionality but without the direct integration into Keycloak. Thanks for writing and providing this great piece of software!

Contributing

The client is written in pure Python. Any changes or pull requests are more than welcome, but please adhere to the code style.

Ruff is used both for code formatting and linting. Before committing, please run the following command to ensure that your code is properly formatted:

ruff check .
ruff format .

A pre-commit hook configuration is supplied as part of the project.

Development

This project is using Act to handle local development tasks. It is used to work locally and also to test Github actions before deploying them.

Metadata

Release files for fastapi-keycloak-middleware 2.0.0rc1

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-keycloak-middleware 2.0.0rc1
File Size Uploaded
fastapi_keycloak_middleware-2.0.0rc1.tar.gz 20.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for fastapi-keycloak-middleware 2.0.0rc1
File Interpreter ABI Platform
fastapi_keycloak_middleware-2.0.0rc1-py3-none-any.whl Python 3 none any Details

Total release size: 48.6 kB

Release files / fastapi_keycloak_middleware-2.0.0rc1.tar.gz

Download URL fastapi_keycloak_middleware-2.0.0rc1.tar.gz
Size 20.3 kB
Tags Source
SHA-256 checksum
How to use checksums
892ba30844ba9af8fd5a70ee27f87aaa04c308f96e37cf12b414dd01f53c98f5
BLAKE2b-256 checksum
How to use checksums
3ed92064f746838f864f95a1052826ef146b4938b8a6812b869031586bc532a7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.5.1 CPython/3.13.1 Linux/6.17.0-1022-azure

Release files / fastapi_keycloak_middleware-2.0.0rc1-py3-none-any.whl

Download URL fastapi_keycloak_middleware-2.0.0rc1-py3-none-any.whl
Size 28.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8833d4d7f656452305097c24a79893118349871c5ea35d1f8320ccd251cc0dfb
BLAKE2b-256 checksum
How to use checksums
0bf7fff78e0138e9c1c303b111b60ab7cb94c4c3e54477e9f950e710c49cb1a2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.5.1 CPython/3.13.1 Linux/6.17.0-1022-azure

Release history Release notifications | RSS feed

2.0.0

2 release files

This release

2.0.0rc1 This release

2 release files

1.6.0

2 release files

1.5.0

2 release files

1.4.0

2 release files

1.3.0

2 release files

1.2.0

2 release files

1.1.0

2 release files

1.0.2

2 release files

1.0.1

2 release files

0.4.2

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.1

2 release files

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