FastAPI Keycloak SSO
Project description
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file keycloak-fast-sso-2023.11.13.tar.gz
.
File metadata
- Download URL: keycloak-fast-sso-2023.11.13.tar.gz
- Upload date:
- Size: 8.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.18
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 10aff1d7b7492397f9b818883aedf3d64561b7c69e606da3ebbace8195e5c383 |
|
MD5 | 4ccecf33aea56a9a0638d3efd1336256 |
|
BLAKE2b-256 | 37519f7a06081fd3dd15550696d1a1239caff34c40c5d22ed27621e5aafa9819 |
File details
Details for the file keycloak_fast_sso-2023.11.13-py3-none-any.whl
.
File metadata
- Download URL: keycloak_fast_sso-2023.11.13-py3-none-any.whl
- Upload date:
- Size: 14.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.18
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 498311e9187af68b554e9b6fb8328d42e8717a761ca142eb50ebc01a3b3e810c |
|
MD5 | 88e4bb92024365de262b5f63f6bd2ecf |
|
BLAKE2b-256 | 45fb7772f900ae252020ea5b053f513bbdb73e2c7b149d81b7409542efe2062d |