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

TISIT Keycloak Adapter

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 tisit-keycloak-adapter

or pip:

pip install tisit-keycloak-adapter

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!

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.

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

tisit_keycloak_adapter-1.3.0.dev7.tar.gz (25.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tisit_keycloak_adapter-1.3.0.dev7-py3-none-any.whl (34.8 kB view details)

Uploaded Python 3

File details

Details for the file tisit_keycloak_adapter-1.3.0.dev7.tar.gz.

File metadata

File hashes

Hashes for tisit_keycloak_adapter-1.3.0.dev7.tar.gz
Algorithm Hash digest
SHA256 a5f4c6d134d133c944d49127a85798ec08af3058ddf911fd5fe3c7be315b8d4e
MD5 d5aa5b617a4244b076aa495016944903
BLAKE2b-256 6c05993d9cf1b50546f010d0540bd5d378af6f0b0fd8a65a20167948c7632a9b

See more details on using hashes here.

File details

Details for the file tisit_keycloak_adapter-1.3.0.dev7-py3-none-any.whl.

File metadata

File hashes

Hashes for tisit_keycloak_adapter-1.3.0.dev7-py3-none-any.whl
Algorithm Hash digest
SHA256 b7119cda320935fb691144989bbc1220c26c7a972fe7c770e5eeb061fec29291
MD5 56d707679cf7136ecee6cc08eb0b25a4
BLAKE2b-256 50198d4c2540aef149c604dc7c5a570714915955c1d051ebf9e1fce9bffa793f

See more details on using hashes here.

Supported by

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