Kaleidoo middleware package
Project description
kal-middleware
kal-middleware
is a Python package designed for FastAPI applications to provide robust JWT and Service-to-Service (STS) authentication using Firebase and Google Identity Platform.
Features
- JWT Authentication: Ensures that the JWTs are valid and checks user roles against provided configurations.
- STS Authentication: Validates tokens for service-to-service communication ensuring that only verified services can communicate.
Installation
Install kal-middleware
using pip:
pip install kal-middleware
Usage
JWT Authentication
To add JWT authentication to your FastAPI endpoints, you can use the jwt_authenticated
decorator provided by kal-middleware
. This decorator checks if the JWT token in the Authorization
header is valid and whether the user has the appropriate role based on a configuration map.
Here's an example of how to apply the firebase_jwt_authenticated
decorator:
Notice: After the JWT is processed, the request.state
holds:
user_uid
- The Firebase unique ID.user_capabilities
- A list of capabilities for later use in the request processing, if needed.user
- Ifcheck_access
is used, the user object will be attached to the request, so the entire process does not need to call the request again.
from kal_middleware.jwt import firebase_jwt_authenticated
from typing import List
# Define a function to retrieve the user's role based on their user ID
def get_user_capabilities(user_id: str):
# Implement your logic to retrieve the user's capabilities
# If the user not found, return "".
# Example - the user can access the get action in example service only.
example_capabilities = [{
"service": "example_service",
"action": "get"
}]
return example_capabilities
# if there is specific variable in the body that needed checks of who access its data only
def check_access(firebase_uid: str, body: dict, user_capabilities_list: List):
# check in the db the user and his parameters
# for example if in the db the user with that exactly firebase_uid is:
user = {
"id": "333444",
"firebase_uid": "12345",
"org_id": "12345"
}
if body["org_id"] == user["org_id"]:
return True, user
return False, f"User {user.get('id')} from another organization then the one that was requested."
@app.get("/your-route/<service>/<action>")
@firebase_jwt_authenticated(get_user_capabilities, check_access)
async def your_route_function(
request: Request = None,
service: Union[str, None] = None,
action: Union[str, None] = None
):
# Your route logic
return {"message": "This is a protected route"}
# Or - if there is no need to check for specific data in the body
@app.get("/your-route-without-check-access/<service>/<action>")
@firebase_jwt_authenticated(get_user_capabilities)
async def your_route_function_without_check_access(
request: Request = None,
service: Union[str, None] = None,
action: Union[str, None] = None
):
# Your route logic
return {"message": "This is a protected route"}
STS Authentication
For service-to-service (STS) authentication using Google's Identity Platform, you can use the sts_authenticated
decorator. This ensures that the calling service's token is verified to enable secure interactions between services.
Here's how to use the sts_authenticated
decorator in your FastAPI app:
- Make sure first you have env variable named
ALLOWED_SERVICE_ACCOUNTS
with the following structure:example1@gserviceaccount.com, example2@gserviceaccount.com
from kal_middleware.sts import sts_authenticated
@app.get("/secure-service")
@sts_authenticated
async def secure_service_function():
# Logic that requires service-to-service authentication
return {"message": "Service-to-service call is authenticated"}
This configuration will parse and verify the Authorization header, ensuring that only requests with a verified bearer token can access the endpoint.
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 kal_middleware-0.0.7-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2dd1e53fab26b9e60cdeb5d9c73f9bf517efef43590028b2ccc7e3b3f73e391d |
|
MD5 | 2eb1dbb4834f52cff8b0a54e8e299614 |
|
BLAKE2b-256 | 087c1fa4b835bc1c1c21a2dd2d5d806fa339eee49d0ea5b4a041dca5d38c3a54 |