Skip to main content

A simple and flexible Basic Authentication middleware for FastAPI applications

Project description

FastAPI Basic Auth

A simple and flexible Basic Authentication middleware for FastAPI applications.

Installation

pip install fastapi-basic-auth

Usage

Basic Usage with Dictionary

from fastapi import FastAPI
from fastapi_basic_auth import FastAPIBasicAuthMiddleware

app = FastAPI()

# Add middleware
auth = FastAPIBasicAuthMiddleware(
    urls=["/protected"],
    users={"admin": "password"}
)
app.add_middleware(auth.build)

@app.get("/protected")
def protected():
    return {"message": "This route is protected"}

Using BasicAuthUser Model

You can also use the BasicAuthUser model directly for more control and validation:

from fastapi import FastAPI
from fastapi_basic_auth import FastAPIBasicAuthMiddleware, BasicAuthUser

app = FastAPI()

# Create users using BasicAuthUser model
users = [
    BasicAuthUser(username="admin", password="password123"),
    BasicAuthUser(username="user1", password="userpass")
]

# Add middleware with BasicAuthUser list
auth = FastAPIBasicAuthMiddleware(
    urls=["/protected", "/admin"],  # Protect multiple routes
    users=users
)
app.add_middleware(auth)

@app.get("/protected")
def protected():
    return {"message": "This route is protected"}

@app.get("/admin")
def admin():
    return {"message": "Admin route is protected"}

Protecting FastAPI Documentation

You can protect your FastAPI's Swagger UI and ReDoc documentation by including their URLs in the protected routes:

from fastapi import FastAPI
from fastapi_basic_auth import FastAPIBasicAuthMiddleware

app = FastAPI()

# Add middleware to protect docs
auth = FastAPIBasicAuthMiddleware(
    urls=[
        "/docs",           # Swagger UI
        "/redoc",         # ReDoc
        "/openapi.json",  # OpenAPI schema
        "/protected"      # Your protected routes
    ],
    users={"admin": "password"}
)
app.add_middleware(auth)

@app.get("/protected")
def protected():
    return {"message": "This route is protected"}

@app.get("/public")
def public():
    return {"message": "This route is public"}

This setup will:

  • Require authentication to access Swagger UI at /docs
  • Require authentication to access ReDoc at /redoc
  • Protect the OpenAPI schema at /openapi.json
  • Allow public access to non-protected routes
  • Maintain the interactive features of Swagger UI after authentication

The BasicAuthUser model includes built-in validation:

  • Username must be alphanumeric (can include underscores and hyphens)
  • Both username and password are required fields

Features

  • Protect specific routes with Basic Authentication
  • Support for multiple users
  • Flexible configuration
    • Use simple dictionary for quick setup
    • Use BasicAuthUser model for additional validation
  • Secure password comparison
  • FastAPI docs protection
    • Swagger UI (/docs)
    • ReDoc (/redoc)
    • OpenAPI schema (/openapi.json)

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_basic_auth-0.1.4.tar.gz (3.5 kB view details)

Uploaded Source

Built Distribution

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

fastapi_basic_auth-0.1.4-py3-none-any.whl (4.0 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_basic_auth-0.1.4.tar.gz.

File metadata

  • Download URL: fastapi_basic_auth-0.1.4.tar.gz
  • Upload date:
  • Size: 3.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.5 Darwin/24.0.0

File hashes

Hashes for fastapi_basic_auth-0.1.4.tar.gz
Algorithm Hash digest
SHA256 d37b08cef789a4ad082500249302722386f7c2f3b0e8661e36fe84c20fe5ce8d
MD5 5bebbd855fd80dc7160c1fc5bcc3d0bb
BLAKE2b-256 ddd1d72961f080759bae0a530d49db3e2cb481497a9429a776fefcf8206f0970

See more details on using hashes here.

File details

Details for the file fastapi_basic_auth-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: fastapi_basic_auth-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 4.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.5 Darwin/24.0.0

File hashes

Hashes for fastapi_basic_auth-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 2d92c263cca2f7634578d6f2028f40cdbb25d496e07b43bb95c65e0a932f5e96
MD5 ef378b8d7a5172e4e329ff81358e7241
BLAKE2b-256 ec644f5ff7a70bb3851d0894a67506e351043c57e40923b8b0a14538e7b4f0a7

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