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.
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 AWS Cognito:
from fastapi import FastAPI, Depends
from auth_middleware import JwtAuthMiddleware, require_user
from auth_middleware.providers.authn.cognito_provider import CognitoProvider
from auth_middleware.providers.authn.cognito_authz_provider_settings import (
CognitoAuthzProviderSettings,
)
app = FastAPI(title="My Secure API")
# Configure Cognito authentication
auth_settings = CognitoAuthzProviderSettings(
user_pool_id="your_user_pool_id",
user_pool_region="your_aws_region",
jwt_token_verification_disabled=False,
)
# Add authentication middleware
app.add_middleware(
JwtAuthMiddleware,
auth_provider=CognitoProvider(settings=auth_settings),
)
@app.get("/protected", dependencies=[Depends(require_user())])
async def protected_endpoint(request):
return {"message": f"Hello {request.state.current_user.name}"}
Documentation Contents
Getting Started:
Infrastructure Setup:
- Infrastructure Setup
- Provider-Specific Setup Guides
- AWS Cognito Infrastructure Setup
- Prerequisites
- Step 1: Create a User Pool
- Step 2: Create App Clients
- Step 3: Create User Groups
- Step 4: Create Users
- Step 5: Assign Users to Groups
- Step 6: Configure User Pool Settings
- Step 7: Testing Your Setup
- Configuration Summary
- Troubleshooting
- Security Best Practices
- Next Steps
- Azure Entra ID Infrastructure Setup
- Overview
- Coming Soon
- Google Identity Infrastructure Setup
- Overview
- Coming Soon
- Overview
- Security Considerations
- Next Steps
Authentication Providers:
Advanced Topics:
- Groups Provider
- Overview
- Built-in Providers
- Using Groups in Your Application
- Custom Groups Provider
- Configuration Examples
- Testing Groups Providers
- Best Practices
- Migration and Deployment
- Troubleshooting
- API Reference
- See Also
- Permissions Provider
- Overview
- Built-in Providers
- Using Permissions in Your Application
- Permission Patterns
- Custom Permissions Provider
- Advanced Features
- Permission Management
- Testing Permissions Providers
- Best Practices
- Troubleshooting
- API Reference
- See Also
- Extending Authorization Providers
- Overview
- Understanding the Provider Interface
- Integration Patterns
- Advanced Patterns
- Caching and Performance
- Error Handling and Resilience
- Testing Custom Providers
- Deployment Considerations
- Best Practices Summary
- See Also
- The User Property
- Control Authentication and Authorization
- jwt_bearer_manager
- Exception Handling
API Reference: