Skip to main content

Kaleidoo middleware package

Project description

kal-middleware

image

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 jwt_authenticated decorator:

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 = {
        "firebase_uid": "12345",
        "org_id": "12345"
    }
    return body["org_id"] == user["org_id"]

@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, config_map)
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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

kal_middleware-0.0.5.tar.gz (14.3 kB view details)

Uploaded Source

Built Distribution

kal_middleware-0.0.5-py2.py3-none-any.whl (6.2 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file kal_middleware-0.0.5.tar.gz.

File metadata

  • Download URL: kal_middleware-0.0.5.tar.gz
  • Upload date:
  • Size: 14.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.12.3

File hashes

Hashes for kal_middleware-0.0.5.tar.gz
Algorithm Hash digest
SHA256 3329d974cc359288ab52f7a229b0390df8aef19b2c47db273829f2fd014c37fe
MD5 3b5f650f75971a148b5006210df691a7
BLAKE2b-256 8833d6c8154a4be2950988f7ed125447d73387119e39bfe525ad9f7238f809e3

See more details on using hashes here.

File details

Details for the file kal_middleware-0.0.5-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for kal_middleware-0.0.5-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 06624bfed012ed72330cb915892e63e53495b6ebca2ea2cc96f802d196f3ad83
MD5 14bcd1f9fb0eb6d818fe15b4d733fb24
BLAKE2b-256 3b500698e3e7e7cb3713e49c985a956f2eeab8d3983b239db45e6a57293b92de

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