Skip to main content

FastAPI Auth Gateway

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.1.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.1-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: fastapi_auth_gateway-1.2.1.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.1.tar.gz
Algorithm Hash digest
SHA256 ed581b949d5d992af3190b898092f40c0982663a50362ae95de8b76394697441
MD5 6ad5af580a9f3c0d3dc8a9773ffb07ba
BLAKE2b-256 e344a7f9d31c38c8d24c8935464e4d31dfb6028a6bd4491b1f19b4b00778094d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: fastapi_auth_gateway-1.2.1-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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6be4be337f3028f1be8fc2dff00799ace930be071b3af033f43d6c10e0318e25
MD5 9037f0e22d41ea14e6ebd9f06c694bd2
BLAKE2b-256 c998e65a0290a6253336c2da8f61dac6fceee7d22ebf961533d10246220d8255

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