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:
```sh
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.0.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.0-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: fastapi_auth_gateway-1.2.0.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.0.tar.gz
Algorithm Hash digest
SHA256 d7f5dfb20d1bc42d7f929b540a5ed3670bc4ce466c019cf31bb403aeda7232df
MD5 b024cffe3e89585ddcaa238cf8a149e8
BLAKE2b-256 17adca1a9175bea60b1820f66c3b0ee3a47e330a01a7f8beb2b699ee6d7c9034

See more details on using hashes here.

File details

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

File metadata

  • Download URL: fastapi_auth_gateway-1.2.0-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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 63f012cb35f1cf512ddec64b370760ea7621bcac081c00c7e0d575b58ff9316a
MD5 42fc8d53275cf5db34e363c6c1949d08
BLAKE2b-256 e6a1b783f6bb926b7c3e2430e9c74847f0273dd219118cf34ab46856984c1d0e

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