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

Notice: After the JWT is processed, the request.state holds:

  1. user_uid - The Firebase unique ID.
  2. user_capabilities - A list of capabilities for later use in the request processing, if needed.
  3. user - If check_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
from utils import get_org, get_user_by_fb_uid, get_capability_by_service_action

async def get_user(firebase_uid):
    user = await get_user_by_fb_uid(firebase_uid)
    return user

# if there is specific variable in the body that needed checks of who access its data only
async def check_access(user: dict, body: dict):
    # check in the db the user and his parameters
    # for example:
    capabilities = user.get("capabilities")
    if "capability_id" in body:
        access =  any(capability for capability in capabilities if capability.get("id") == body["capability_id"] )
        if not access:
            return False, f"User can't access the request."
    if "org_id" in body:
        org = get_org(body["org_id"])
        if not org:
            return False, f"Org not found"
        return True, {"org": org}
    return False, f"User {user.get('id')} from another organization then the one that was requested."


async def get_capability(service, action):
    capability = await get_capability_by_service_action(service, action)
    return capability

@app.get("/your-route/<service>/<action>")
@firebase_jwt_authenticated(get_user, 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)
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-1.0.5.tar.gz (16.7 kB view details)

Uploaded Source

Built Distribution

kal_middleware-1.0.5-py2.py3-none-any.whl (8.7 kB view details)

Uploaded Python 2 Python 3

File details

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

File metadata

  • Download URL: kal_middleware-1.0.5.tar.gz
  • Upload date:
  • Size: 16.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.5

File hashes

Hashes for kal_middleware-1.0.5.tar.gz
Algorithm Hash digest
SHA256 e11bf04d8e97f4d59c0f903d8934c6ce9ba3d70d49955df7701e376e3b3a445e
MD5 d0aba743279a90e83a4ff10c68240b4f
BLAKE2b-256 289bd1c92407da2aa6df9d7ff38f4a00fcb209b3ac0caaeb6f65e711e22631a9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for kal_middleware-1.0.5-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 80a65358f2eb04fb8fb8b4895c0149a7fd5bfdd0bb4eb0851173a8d2594a361b
MD5 0e126328f24646b05502d7a9f827d727
BLAKE2b-256 2c12e712aa7169bde4c4d9f5dbe74f2b7af8d923758a567b757fe6adc45db8e3

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