Auth Middleware Documentation

Auth Middleware is a modern, high-performance authentication and authorization middleware for FastAPI and Starlette applications. It provides seamless integration with popular identity providers like AWS Cognito, Azure Entra ID, and Google, while supporting custom authentication providers.

PyPI Version Python Version License

Key Features

  • JWT Authentication: Secure JWT token validation and processing

  • Multiple Providers: AWS Cognito, Azure Entra ID, Google, and custom providers

  • Group-based Authorization: Fine-grained access control with user groups

  • Permission System: Flexible permission-based authorization

  • High Performance: Async-first design for maximum throughput

  • Easy Integration: Simple middleware setup with minimal configuration

  • OpenAPI Support: Automatic API documentation with security schemas

  • Type Safety: Full TypeScript-style type hints for better development experience

Quick Start

Installation:

pip install auth-middleware

Basic usage with a generic OIDC issuer (works with AWS Cognito, Authentik, Keycloak, Auth0, Okta, …):

from fastapi import FastAPI, Depends
from auth_middleware import JwtAuthMiddleware
from auth_middleware.guards import require_user
from auth_middleware.providers.oidc.oidc_provider import OidcProvider
from auth_middleware.providers.oidc.oidc_provider_settings import (
    OidcProviderSettings,
)

app = FastAPI(title="My Secure API")

# Configure OIDC authentication. For AWS Cognito, the issuer is
# https://cognito-idp.{region}.amazonaws.com/{user_pool_id}
auth_settings = OidcProviderSettings(
    issuer="https://your-issuer.example.com",
    audience="your-client-id",
    jwt_token_verification_disabled=False,
)

# Add authentication middleware
app.add_middleware(
    JwtAuthMiddleware,
    auth_provider=OidcProvider(settings=auth_settings),
)

@app.get("/protected", dependencies=[Depends(require_user())])
async def protected_endpoint(request):
    return {"message": f"Hello {request.state.current_user.name}"}

Contents

Advanced Topics:

License

Mozilla Public License 2.0 (MPL-2.0). The library can be used freely in closed-source and commercial applications. If you modify the library’s own files and distribute the result, those files must be published under MPL-2.0. Releases up to and including 0.7.0 were published under the MIT License.

Indices and tables