No project description provided
Project description
fast-sso
fastsso
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
Hashes for fastsso-1.0.1a0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f56adef154c661c7d86fe1b0ecfae039d42e11d2b1b2b72969565326de13d236 |
|
MD5 | 6205d7b92ab26f3a22e886d0f77a1f28 |
|
BLAKE2b-256 | 331c3bdcbfd549896d28a169ebb1a27848a4378d00c6562abcad54e176ac9e39 |