Skip to main content

Keycloak OAuth client for Python projects

Project description

python-keycloak-oauth

Keycloak OAuth client for Python projects with optional integrations for FastAPI & Starlette-Admin.

Getting started

FastAPI

pip install keycloak-oauth[fastapi]
from typing import Annotated
from fastapi import FastAPI, Request, Depends
from starlette.middleware.sessions import SessionMiddleware
from backend.settings import settings, BASE_URL, SECRET_KEY  # secrets
from keycloak_oauth import KeycloakOAuth2

keycloak = KeycloakOAuth2(
    client_id=settings.keycloak.client_id,
    client_secret=settings.keycloak.client_secret,
    server_metadata_url=str(settings.keycloak.server_metadata_url),
    client_kwargs=settings.keycloak.client_kwargs,
    base_url=BASE_URL,
)
# create router and register API endpoints
keycloak.setup_fastapi_routes()

app = FastAPI()
app.add_middleware(SessionMiddleware, secret_key=SECRET_KEY)
app.include_router(keycloak.router, prefix="/auth")

@app.get("/")
def index(
    request: Request, user: Annotated[User, Depends(KeycloakOAuth2.get_user)]
):
    """Protected endpoint, will return 401 Unauthorized if not signed in."""
    return f"Hello {user.name}"

We now expose the API endpoints for Keycloak:

  • /auth/login: redirect to Keycloak login page
  • /auth/callback: authorize user with Keycloak access token
  • /auth/logout: deauthorize user and redirect to the logout page

Starlette-Admin

pip install keycloak-oauth[starlette-admin]
from starlette.middleware.sessions import SessionMiddleware
from starlette_admin.contrib.sqla import Admin
from backend.settings import settings, BASE_URL, SECRET_KEY  # secrets
from keycloak_oauth import KeycloakOAuth2
from keycloak.starlette_admin import KeycloakAuthProvider

keycloak = KeycloakOAuth2(
    client_id=settings.keycloak.client_id,
    client_secret=settings.keycloak.client_secret,
    server_metadata_url=str(settings.keycloak.server_metadata_url),
    client_kwargs=settings.keycloak.client_kwargs,
    base_url=BASE_URL,
)

admin = Admin(
    # engine,
    title=...,
    base_url=BASE_URL,
    auth_provider=KeycloakAuthProvider(keycloak),
    middlewares=[Middleware(SessionMiddleware, secret_key=SECRET_KEY)],
)

admin.add_view(...)

Development

If you want to contribute to this project, you can simply clone the repository and run poetry install --all-extras.

Please also run pre-commit install for linting and enforcing a consistent code style.

Contributing

We are happy if you want to contribute to this project. If you find any bugs or have suggestions for improvements, please open an issue. We are also happy to accept your PRs. Just open an issue beforehand and let us know what you want to do and why.

License

This project is licensed under the MIT license. Have a look at the LICENSE for more details.

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

keycloak_oauth-0.3.0.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

keycloak_oauth-0.3.0-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file keycloak_oauth-0.3.0.tar.gz.

File metadata

  • Download URL: keycloak_oauth-0.3.0.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.10.12 Linux/6.5.0-1016-azure

File hashes

Hashes for keycloak_oauth-0.3.0.tar.gz
Algorithm Hash digest
SHA256 ef361ffe3ce872da0ec44447cba18d39b8fde3a31b17135094eb64210f8213a5
MD5 50f0552a862a60be5d54a1974c641b94
BLAKE2b-256 49b8508ad5c06adc878f5357b2d94150bac2a5c6a6c54176a094e781201d04c1

See more details on using hashes here.

File details

Details for the file keycloak_oauth-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: keycloak_oauth-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 6.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.10.12 Linux/6.5.0-1016-azure

File hashes

Hashes for keycloak_oauth-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ff6dab300fb1defca0b93f108765d7bcfdb2495617d411d92d9cb89e3978ed9d
MD5 63b198f51feb797f914a8c38457cd2a1
BLAKE2b-256 d32beda444c27607b3e00765c7f6f86f2fcad37599fa690e6cb7859f7d82ff08

See more details on using hashes here.

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