Middleware for FastAPI to authenticate a user against keycloak
Actively maintained. Last release 7 weeks ago. A safe choice.
pip install fastapi-keycloak-middlewareFastAPI 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).