fastapi-basic-auth


Namefastapi-basic-auth JSON
Version 0.1.3 PyPI version JSON
download
home_pagehttps://github.com/dakohhh/fastapi-basic-auth
SummaryA simple and flexible Basic Authentication middleware for FastAPI applications
upload_time2025-01-25 01:21:09
maintainerNone
docs_urlNone
authordakohhh
requires_python<4.0,>=3.9
licenseMIT
keywords fastapi authentication basic-auth middleware
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # FastAPI Basic Auth

A simple and flexible Basic Authentication middleware for FastAPI applications.

## Installation

```bash
pip install fastapi-basic-auth
```

## Usage

### Basic Usage with Dictionary

```python
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:

```python
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.build)

@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:

```python
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.build)

@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)
            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/dakohhh/fastapi-basic-auth",
    "name": "fastapi-basic-auth",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<4.0,>=3.9",
    "maintainer_email": null,
    "keywords": "fastapi, authentication, basic-auth, middleware",
    "author": "dakohhh",
    "author_email": "wiizzydreadmill@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/38/e2/26ac116a8bc22c8cecf4700eabddec171db7926449885270bd22812cd3e9/fastapi_basic_auth-0.1.3.tar.gz",
    "platform": null,
    "description": "# FastAPI Basic Auth\n\nA simple and flexible Basic Authentication middleware for FastAPI applications.\n\n## Installation\n\n```bash\npip install fastapi-basic-auth\n```\n\n## Usage\n\n### Basic Usage with Dictionary\n\n```python\nfrom fastapi import FastAPI\nfrom fastapi_basic_auth import FastAPIBasicAuthMiddleware\n\napp = FastAPI()\n\n# Add middleware\nauth = FastAPIBasicAuthMiddleware(\n    urls=[\"/protected\"],\n    users={\"admin\": \"password\"}\n)\napp.add_middleware(auth.build)\n\n@app.get(\"/protected\")\ndef protected():\n    return {\"message\": \"This route is protected\"}\n```\n\n### Using BasicAuthUser Model\n\nYou can also use the `BasicAuthUser` model directly for more control and validation:\n\n```python\nfrom fastapi import FastAPI\nfrom fastapi_basic_auth import FastAPIBasicAuthMiddleware, BasicAuthUser\n\napp = FastAPI()\n\n# Create users using BasicAuthUser model\nusers = [\n    BasicAuthUser(username=\"admin\", password=\"password123\"),\n    BasicAuthUser(username=\"user1\", password=\"userpass\")\n]\n\n# Add middleware with BasicAuthUser list\nauth = FastAPIBasicAuthMiddleware(\n    urls=[\"/protected\", \"/admin\"],  # Protect multiple routes\n    users=users\n)\napp.add_middleware(auth.build)\n\n@app.get(\"/protected\")\ndef protected():\n    return {\"message\": \"This route is protected\"}\n\n@app.get(\"/admin\")\ndef admin():\n    return {\"message\": \"Admin route is protected\"}\n```\n\n### Protecting FastAPI Documentation\n\nYou can protect your FastAPI's Swagger UI and ReDoc documentation by including their URLs in the protected routes:\n\n```python\nfrom fastapi import FastAPI\nfrom fastapi_basic_auth import FastAPIBasicAuthMiddleware\n\napp = FastAPI()\n\n# Add middleware to protect docs\nauth = FastAPIBasicAuthMiddleware(\n    urls=[\n        \"/docs\",           # Swagger UI\n        \"/redoc\",         # ReDoc\n        \"/openapi.json\",  # OpenAPI schema\n        \"/protected\"      # Your protected routes\n    ],\n    users={\"admin\": \"password\"}\n)\napp.add_middleware(auth.build)\n\n@app.get(\"/protected\")\ndef protected():\n    return {\"message\": \"This route is protected\"}\n\n@app.get(\"/public\")\ndef public():\n    return {\"message\": \"This route is public\"}\n```\n\nThis setup will:\n- Require authentication to access Swagger UI at `/docs`\n- Require authentication to access ReDoc at `/redoc`\n- Protect the OpenAPI schema at `/openapi.json`\n- Allow public access to non-protected routes\n- Maintain the interactive features of Swagger UI after authentication\n\nThe `BasicAuthUser` model includes built-in validation:\n- Username must be alphanumeric (can include underscores and hyphens)\n- Both username and password are required fields\n\n## Features\n\n- Protect specific routes with Basic Authentication\n- Support for multiple users\n- Flexible configuration\n  - Use simple dictionary for quick setup\n  - Use BasicAuthUser model for additional validation\n- Secure password comparison\n- FastAPI docs protection\n  - Swagger UI (/docs)\n  - ReDoc (/redoc)\n  - OpenAPI schema (/openapi.json)",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "A simple and flexible Basic Authentication middleware for FastAPI applications",
    "version": "0.1.3",
    "project_urls": {
        "Homepage": "https://github.com/dakohhh/fastapi-basic-auth",
        "Repository": "https://github.com/dakohhh/fastapi-basic-auth"
    },
    "split_keywords": [
        "fastapi",
        " authentication",
        " basic-auth",
        " middleware"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "f61bf52feba44d9e4fe25a2634615aca16c2bef01135f32c342f98fc8ea4755e",
                "md5": "096851b2953db07b51888d43bd3f5592",
                "sha256": "6662fc152800d26fad8dd42bb9a24110e751a49a10eeb55cad35a078c3b8a8d4"
            },
            "downloads": -1,
            "filename": "fastapi_basic_auth-0.1.3-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "096851b2953db07b51888d43bd3f5592",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4.0,>=3.9",
            "size": 4018,
            "upload_time": "2025-01-25T01:21:05",
            "upload_time_iso_8601": "2025-01-25T01:21:05.911188Z",
            "url": "https://files.pythonhosted.org/packages/f6/1b/f52feba44d9e4fe25a2634615aca16c2bef01135f32c342f98fc8ea4755e/fastapi_basic_auth-0.1.3-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "38e226ac116a8bc22c8cecf4700eabddec171db7926449885270bd22812cd3e9",
                "md5": "cb9c04a201d88b2a7580e805440d375f",
                "sha256": "2fed3a3c5ab6ed489afe412e956f2d1cf58fbcd3ee7c646e0ad1615b8c2a2752"
            },
            "downloads": -1,
            "filename": "fastapi_basic_auth-0.1.3.tar.gz",
            "has_sig": false,
            "md5_digest": "cb9c04a201d88b2a7580e805440d375f",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4.0,>=3.9",
            "size": 3530,
            "upload_time": "2025-01-25T01:21:09",
            "upload_time_iso_8601": "2025-01-25T01:21:09.642098Z",
            "url": "https://files.pythonhosted.org/packages/38/e2/26ac116a8bc22c8cecf4700eabddec171db7926449885270bd22812cd3e9/fastapi_basic_auth-0.1.3.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-01-25 01:21:09",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "dakohhh",
    "github_project": "fastapi-basic-auth",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "fastapi-basic-auth"
}
        
Elapsed time: 1.09311s