Skip to main content

FastAPI Auth Gateway

Reason this release was yanked:

deprecated

Project description

FastAPI Authentication Gateway Middleware

This module provides middleware for verifying authorization tokens from request headers or cookies in a FastAPI application. It ensures secure authentication by validating tokens against an external authentication service.

Features

  • Middleware for FastAPI that intercepts requests for authentication validation.

  • Supports token authentication via headers or cookies.

  • Configurable validation service URL, headers, and timeout.

  • Custom error messages and headers.

  • Ability to exclude specific URLs from authentication.

Installation

You can install the package using pip:

pip install fastapi-auth-gateway

Alternatively, if you prefer to use poetry for package dependencies:

poetry shell
poetry add fastapi-auth-gateway

Usage

Middleware Setup

To use the authentication middleware, include it in your FastAPI application:

from fastapi_auth_gateway import FastAPIAuthGateway, AuthLocation

app = FastAPI()

app.add_middleware(
    FastAPIAuthGateway,
    validation_service_url="https://auth.example.com/validate",
    key="Authorization",
    auth_location=AuthLocation.HEADER,
    timeout=10,
    custom_headers={"X-Custom-Header": "Value"},
    custom_error={"error": "Unauthorized access"},
    exclude_urls=["/public", "/health"]
)

Token Validation Process

  • The middleware extracts the token from either the request header (Authorization) or a cookie.

  • If a token is missing, it returns a 401 Unauthorized response.

  • The token is validated against an external authentication service via AuthValidationClient.

  • If the token is invalid, it returns the error response from the authentication service.

  • If the token is valid, the request proceeds to the next middleware or endpoint.

Components

FastAPIAuthGateway

A middleware that intercepts requests and verifies authentication tokens before allowing access to endpoints.

AuthValidationClient

A client that handles the authentication validation process using aiohttp to communicate with an external validation service.

AuthLocation

An enumeration defining where authentication tokens can be extracted from:

  • HEADER: Extract token from request headers.

  • COOKIE: Extract token from request cookies.

Configuration Options

Parameter Type Description
validation_service_url str The URL of the authentication validation service.
key str The key used for token extraction (e.g., "Authorization").
auth_location AuthLocation Defines whether authentication is handled via headers or cookies.
timeout int Timeout for authentication requests (in seconds).
custom_headers dict(optional) Custom headers to include in authentication requests.
custom_error dict(optional) Custom error message response for unauthorized access.
exclude_urls list(optional) List of endpoint paths to exclude from authentication.

Example Response

If authentication fails, the middleware returns a 401 Unauthorized response:

{
    "message": "Unauthorized"
}

License

This project is licensed under the MIT License.

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

fastapi_auth_gateway-1.2.2.tar.gz (4.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fastapi_auth_gateway-1.2.2-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_auth_gateway-1.2.2.tar.gz.

File metadata

  • Download URL: fastapi_auth_gateway-1.2.2.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.0.1 CPython/3.10.16 Linux/6.8.0-1020-azure

File hashes

Hashes for fastapi_auth_gateway-1.2.2.tar.gz
Algorithm Hash digest
SHA256 507b05b9551f08941b2a361815223b4a3f490d44e57e15c47c3b8cfe10694170
MD5 0c2739e5ddafde6a42986eba436bb840
BLAKE2b-256 c05a290326ad736353b93895865b3a685404b53b989fa7f74bb25aa80e55aa34

See more details on using hashes here.

File details

Details for the file fastapi_auth_gateway-1.2.2-py3-none-any.whl.

File metadata

  • Download URL: fastapi_auth_gateway-1.2.2-py3-none-any.whl
  • Upload date:
  • Size: 5.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.0.1 CPython/3.10.16 Linux/6.8.0-1020-azure

File hashes

Hashes for fastapi_auth_gateway-1.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c3396337d9a02f33702b139de7e12a176547e22254935a37b443145398c322bd
MD5 4b8bc6abf6fff6a1c731ac5a82cc2b5f
BLAKE2b-256 4ab9344ba60c053d404a2b82c580e6a031309195091b99aa4ffc6cec7cecc3b8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page