Skip to main content

FastAPI Keycloak SSO

Project description

keycloakfastsso

keycloakfastsso

PyPI - License

PyPI - License

keycloak-fast-sso

keycloakfastsso is a Python package that facilitates the integration of Keycloak authentication in applications built with the FastAPI web framework.

Features 🎁

  • Authentication with bearer JWT tokens.

  • JWT token validation.

  • Authorization based on roles, groups and the user’s email verification state in Keycloak.

  • Provides information about the validated user.

  • Handy tools for checking roles, groups and other user attributes.

Usage 💡

Middleware

Use KeycloakFastSSOMiddleware to protect your API routes. You must first configure the connection to Keycloak with the appropriate parameters.

from fastapi import FastAPI
from keycloakfastsso.middleware import KeycloakFastSSOMiddleware

app = FastAPI()
app.add_middleware( KeycloakFastSSOMiddleware,
                    server_url="https://my-keycloak-url/auth/",
                    client_id="my-client-id",
                    realm_name="my-realm-name",
                    client_secret_key="my-client-secret-key")

Decorators

These decorators can be used to restrict access to routes based on user roles or groups.

from keycloakfastsso.decorators import require_role, require_group, require_scope, require_email_verified, require_active_user, require_token_type, require_resource_access, require_allowed_origin

@app.get("/require_role")
@require_role(["admin"])
def require_role_endpoint():
    return {"Hello": "World"}

@app.get("/require_group")
@require_group(["my_group"])
def require_group_endpoint():
    return {"Hello": "World"}

# other routes continue the same way

Utilities

The utilities allow you to retrieve specific information about the currently authenticated user.

from keycloakfastsso.utils import KeycloakUtils

# In your route
@app.get("/whoami")
def who_am_i(request: Request):
    return {"user_id": KeycloakUtils.get_user_id(request)}

Installation 🛠️

You can install keycloakfastsso with pip:

pip install keycloak-fast-sso

For more information on how to use this package, please refer to the official documentation.

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-fast-sso-2023.11.13.tar.gz (8.7 kB view hashes)

Uploaded Source

Built Distribution

keycloak_fast_sso-2023.11.13-py3-none-any.whl (14.1 kB view hashes)

Uploaded Python 3

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