Skip to main content

Automatic Swagger UI authentication for FastAPI development

Project description

FastAPI Swagger Auth

Automatically authenticate Swagger UI in FastAPI during development - solving the pain of manually copying JWT tokens every time you want to test endpoints.

Features

  • Automatic Authentication - Swagger UI auto-injects JWT tokens
  • Zero Configuration - Works out of the box with sensible defaults
  • Multiple Providers - Supabase or custom JWT (extensible for any auth service)
  • Security-First - Only activates in development mode
  • Auto-Refresh - Tokens refresh automatically before expiry
  • Type-Safe - Full type hints for better IDE support

Installation

pip install fastapi-swagger-auth

With Supabase support:

pip install fastapi-swagger-auth[supabase]

Quick Start

Basic Usage (Custom JWT)

from fastapi import FastAPI
from fastapi_swagger_auth import SwaggerAuthDev

app = FastAPI(debug=True)

# One-line setup
SwaggerAuthDev(
    app,
    auth_provider="custom",
    dev_credentials={
        "email": "admin@dev.local",
        "sub": "user_123",
    }
)

Supabase Provider

SwaggerAuthDev(
    app,
    auth_provider="supabase",
    dev_credentials={
        "email": "admin@dev.local",
        "password": "devpass123",
        "supabase_url": "https://your-project.supabase.co",
        "supabase_key": "your-anon-key",
    }
)

Custom Token Getter

def get_my_token():
    # Your custom logic to get a token
    return "my-jwt-token"

SwaggerAuthDev(app, token_getter=get_my_token)

Custom Provider

from fastapi_swagger_auth.providers.base import AuthProvider

class MyProvider(AuthProvider):
    async def get_token(self, credentials: dict) -> str:
        # Your auth logic
        return "jwt-token"

    async def refresh_token(self, current_token: str) -> str:
        # Your refresh logic
        return "new-jwt-token"

    def get_token_expiry(self, token: str) -> int:
        # Parse token and return seconds until expiry
        return 3600

SwaggerAuthDev(
    app,
    provider_instance=MyProvider(),
    dev_credentials={"email": "admin@dev.local", "password": "secret"}
)

Configuration Options

Parameter Type Default Description
app FastAPI Required FastAPI application instance
auth_provider str "custom" Provider type: "custom" or "supabase"
dev_credentials dict None Credentials for authentication
token_getter callable None Custom function to get token (overrides provider)
auto_refresh bool True Enable automatic token refresh
enabled bool None Explicitly enable/disable (None = auto-detect from debug mode)
provider_instance AuthProvider None Custom provider instance

How It Works

  1. Initialization - SwaggerAuthDev validates environment (dev mode only by default)
  2. Authentication - Selected provider authenticates using dev_credentials
  3. Token Injection - Custom Swagger UI HTML injects JWT via requestInterceptor
  4. Auto-Refresh - Token automatically refreshes before expiry (optional)

Security

  • Development Only - Only activates when app.debug=True by default
  • No Production Risk - Explicitly enable with enabled=True if needed
  • Clear Logging - Debug messages show authentication status
  • Graceful Fallback - Shows instructions if authentication fails

Examples

See the examples/ directory for complete working examples:

  • basic_usage.py - Simple custom JWT provider
  • supabase_example.py - Supabase authentication
  • custom_provider.py - Custom provider implementation

Development

# Install with dev dependencies
pip install -e ".[dev]"

# Run tests
pytest

# Lint
ruff check .

# Format
ruff format .

FastAPI Swagger Auth eliminates this workflow entirely. Similar to Swashbuckle in .NET, it's a development tool that just works.

License

MIT License - see LICENSE file for details.

Contributing

Contributions welcome! Please feel free to submit a Pull Request.

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_swagger_auth-0.1.0.tar.gz (12.0 kB view details)

Uploaded Source

Built Distribution

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

fastapi_swagger_auth-0.1.0-py3-none-any.whl (12.1 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_swagger_auth-0.1.0.tar.gz.

File metadata

  • Download URL: fastapi_swagger_auth-0.1.0.tar.gz
  • Upload date:
  • Size: 12.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for fastapi_swagger_auth-0.1.0.tar.gz
Algorithm Hash digest
SHA256 cdb5f098e1226c01b13597ab5a3dd23a700063ec90d9a8b933538645cfac0326
MD5 a5a8a47cf4d0661312e35e2b5e35ed1c
BLAKE2b-256 b60fc0e32ecd6df70b9ffc0cf004be8d097aed5732d7894a8f01befbe5e27acd

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastapi_swagger_auth-0.1.0.tar.gz:

Publisher: publish.yml on maxfallstrom/fastapi-swagger-auth

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file fastapi_swagger_auth-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_swagger_auth-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7adb7aca5e377bd1aa468ded49390ee751fa2db7761266c7f7d0a19791eb2278
MD5 1f07dd9b41b8206f41dae947160ace79
BLAKE2b-256 3c6c676e06631ed647d064726b2ba1d2435bc9697741d0ca2b15fe53e252ec5e

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastapi_swagger_auth-0.1.0-py3-none-any.whl:

Publisher: publish.yml on maxfallstrom/fastapi-swagger-auth

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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