Skip to main content

Middleware for FastAPI to authenticate a user against keycloak

Project description

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 decorators and 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 providers convenience decoraters 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 public key obtained from Keycloak
    • 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)
  • A decorator to use previously stored information to enforce certain roles or permissions on FastAPI endpoints
  • Convenience dependencies to retrieve the user object or the authorization result after evaluation within the FastAPI endpoint

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!

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastapi_keycloak_middleware-0.4.0.tar.gz (15.1 kB view details)

Uploaded Source

Built Distribution

File details

Details for the file fastapi_keycloak_middleware-0.4.0.tar.gz.

File metadata

  • Download URL: fastapi_keycloak_middleware-0.4.0.tar.gz
  • Upload date:
  • Size: 15.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.1 CPython/3.10.3 Linux/6.5.0-1018-azure

File hashes

Hashes for fastapi_keycloak_middleware-0.4.0.tar.gz
Algorithm Hash digest
SHA256 0ad1c060d9d7a9087d77b8a0e424b13736298213b88ad27070c2431c6050d0be
MD5 4a798f85f92a157f2edb063f52c3ea53
BLAKE2b-256 b49a1674a16d0c7972e914bcfec7cdd1aba595aa6b41148ecb0bad545c2cba4f

See more details on using hashes here.

Provenance

File details

Details for the file fastapi_keycloak_middleware-0.4.0-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_keycloak_middleware-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 25b6ee2b260d7b6b8688f47054ab473157370644fb154ec73f1aabb788b6da80
MD5 708b927f5395c390540c6c8431f107d9
BLAKE2b-256 7de79e67055697bb4eeea0dd0eceb62a9b34ed55731a17d0f2c1578aebf693c6

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page