Metadata-Version: 2.4
Name: fastapi_keycloak
Version: 2.0.0rc1
Summary: Keycloak API Client for integrating authentication and authorization with FastAPI
Author-email: Jonas Scholl <jonas@code-specialist.com>, Yannic Schröer <yannic@code-specialist.com>, Alex Barceló <alex@betarho.net>
Maintainer-email: Alex Barceló <alex@betarho.net>
Project-URL: documentation, https://fastapi-keycloak.readthedocs.io/
Project-URL: repository, https://github.com/fastapi-keycloak/fastapi-keycloak
Project-URL: issues, https://github.com/fastapi-keycloak/fastapi-keycloak/issues
Keywords: Keycloak,FastAPI,Authentication,Authorization
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: Topic :: Internet :: WWW/HTTP :: Session
Classifier: Topic :: Internet :: WWW/HTTP :: WSGI
Classifier: Topic :: Software Development :: Libraries :: Application Frameworks
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Framework :: FastAPI
Classifier: License :: OSI Approved :: Apache Software License
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: anyio>=3.4.0
Requires-Dist: asgiref>=3.4.1
Requires-Dist: certifi>=2023.7.22
Requires-Dist: charset-normalizer>=2.0.9
Requires-Dist: click>=8.0.3
Requires-Dist: fastapi>=0.70.1
Requires-Dist: h11>=0.12.0
Requires-Dist: idna>=3.3
Requires-Dist: pydantic>=1.5a1
Requires-Dist: pyjwt[crypto]>=2.9.0
Requires-Dist: requests>=2.31.0
Requires-Dist: sniffio>=1.2.0
Requires-Dist: starlette>=0.36.2
Requires-Dist: typing_extensions>=4.0.1
Requires-Dist: urllib3>=1.26.17
Requires-Dist: uvicorn>=0.16.0
Requires-Dist: itsdangerous>=2.0.1
Provides-Extra: dev
Requires-Dist: pytest>=8.3.4; extra == "dev"
Requires-Dist: pytest-cov>=6.0.0; extra == "dev"
Requires-Dist: responses>=0.25.0; extra == "dev"
Requires-Dist: ruff>=0.8.0; extra == "dev"
Provides-Extra: docs
Requires-Dist: sphinx>=8.0.0; extra == "docs"
Requires-Dist: myst-parser>=4.0.0; extra == "docs"
Requires-Dist: furo>=2024.8.6; extra == "docs"
Requires-Dist: sphinx-copybutton>=0.5.2; extra == "docs"
Dynamic: license-file

# FastAPI Keycloak Integration

![Py3.10](https://img.shields.io/badge/-Python%203.10-brightgreen)
![Py3.11](https://img.shields.io/badge/-Python%203.11-brightgreen)
![Py3.12](https://img.shields.io/badge/-Python%203.12-brightgreen)
![Py3.13](https://img.shields.io/badge/-Python%203.13-brightgreen)

## Introduction

Welcome to `fastapi-keycloak`. This projects goal is to ease the integration of Keycloak (OpenID Connect) with Python, especially FastAPI. FastAPI is not necessary but is
encouraged due to specific features. Currently, this package supports only the `password` and the `authorization_code`. However, the `get_current_user()` method accepts any JWT
that was signed using Keycloak´s private key.

## Installation

```shell
pip install fastapi_keycloak
```

## Usage

```python
from fastapi import FastAPI, Depends
from fastapi_keycloak import FastAPIKeycloak, OIDCUser

app = FastAPI()
idp = FastAPIKeycloak(
    server_url="https://auth.some-domain.com/auth",
    client_id="some-client",
    client_secret="some-secret",
    admin_client_secret="some-admin-cli-secret",
    realm="some-realm-name",
    callback_uri="http://localhost:8081/callback",
)
idp.add_swagger_config(app)


@app.get("/protected")
def protected(user: OIDCUser = Depends(idp.get_current_user())):
    return f"Hi {user}"
```

If your service only needs to authenticate requests (no user/role/group management), use `FastAPIKeycloakAuth`
instead — it requires no `admin_client_secret`:

```python
from fastapi_keycloak import FastAPIKeycloakAuth

idp = FastAPIKeycloakAuth(
    server_url="https://auth.some-domain.com/auth",
    client_id="some-client",
    client_secret="some-secret",
    realm="some-realm-name",
    callback_uri="http://localhost:8081/callback",
)
```

`FastAPIKeycloak` extends `FastAPIKeycloakAuth` with the full admin API, so existing code keeps working unchanged.

## Docs

Docs are available at [https://fastapi-keycloak.readthedocs.io/](https://fastapi-keycloak.readthedocs.io/).

## TLDR

FastAPI Keycloak enables you to do the following things without writing a single line of additional code:

- Verify identities and roles of users with Keycloak
- Get a list of available identity providers
- Create/read/delete users
- Create/read/delete roles
- Create/read/delete/assign groups (recursive). Thanks to @fabiothz
- Assign/remove roles from users
- Implement the `password` or the `authorization_code` flow (login/callback/logout)

## Contributions

We would like encourage anyone using this package to contribute to its improvement, if anything isn't working as expected or isn't well enough documented, please open an issue or a
pull request. Please note that for any code contribution tests are required. See [AGENTS.md](AGENTS.md) for the full contributor guide, including how to run the test suite,
lint/format the code, and build the docs locally.

## Original authors

Shoutout to the original authors of this project:

- Yannic Schröer @yannicschroeer
- Jonas Scholl @JonasScholl

This project was in the [Code Specialist organization](https://github.com/code-specialist/) before being moved here.
