Skip to main content

FastAPI-compatible authentication, OAuth, CSRF, and session management.

Project description

authshield

A modular, framework-agnostic security suite providing FastAPI-compatible Authentication, CSRF Protection, and Session Management.

authshield uses a dynamic factory architecture, allowing it to seamlessly inject secure, chainable configuration methods directly into FastAPI or any custom subclass wrapper you are already using -- without breaking your existing inheritance tree or IDE autocomplete.


Features

  • Zero-Dependency-Inversion: Extends your application class dynamically at runtime.
  • Fluent & Chainable API: Configure your entire security stack in a single, elegant block.
  • Fully Modular: Core engines (Auth, CSRF, Sessions) are separated internally for clean maintainability.
  • Type Safe: Built from the ground up to support PEP 561 standard inline type hints.

Installation

Install authshield via uv (or your preferred package manager):

uv add authshield

Quick Start

authshield provides a shield_class factory that wraps your application class (e.g., FastAPI) and injects fluent configuration methods like .useAuth() and .useCSRF().

from fastapi import FastAPI
from authshield import shield_class

# 1. Dynamically wrap your base application class
ShieldedApp = shield_class(FastAPI)

# 2. Instantiate and chain your security configuration seamlessly
app = (
    ShieldedApp(title="My Shielded API")
    .useAuth(config={...})
    .useCSRF(config={...})
)

@app.get("/")
async def root():
    return {"status": "shielded"}

Architecture

Module Overview

authshield/
├── config.py           # CsrfConfig, AuthConfig, SsoConfig (Pydantic models)
├── extended.py         # shield_class() factory
├── auth/
│   ├── models.py       # UserSession, UserEntry, UserUpdate
│   ├── _auth_handler.py    # Password + SSO authentication logic
│   ├── _hashing.py         # Argon2id password hashing
│   ├── _require_auth.py    # FastAPI dependency for route protection
│   └── _use_auth.py        # Wires AuthConfig into app state
├── csrf/
│   ├── _csrf_handler.py    # CSRFMiddleware (Double-Submit Cookie)
│   └── _use_csrf.py        # Registers CSRF middleware
├── oauth/              # (planned)
└── session/            # (planned)

Authentication

Two authentication flows are supported:

Flow Entry point Description
Password authenticate_user(email, password, config) Verifies credentials via Argon2id hash and returns a UserSession.
SSO authenticate_user_by_sso(claims, config) Matches SSO claims to local users (by sub or email), with optional auto-merging and auto-provisioning.

Route-level protection uses the require_auth FastAPI dependency:

from fastapi import Depends
from authshield.auth import require_auth

@app.get("/me")
async def me(user=Depends(require_auth())):
    return user

@app.get("/admin")
async def admin(user=Depends(require_auth("admin", "superadmin"))):
    return user

CSRF Protection

CSRFMiddleware implements the Double-Submit Cookie pattern with optional HMAC-signed token binding. Configure via CsrfConfig:

from authshield.config import CsrfConfig

config = CsrfConfig(
    trusted_origins=["api.example.com", "app.example.com"],
    cookie_samesite="Strict",
    signed_mode=True,
    secret_key="your-secret-key",
)

Configuration Models

Model Purpose
CsrfConfig Cookie name, header, SameSite, trusted origins, signed mode
AuthConfig SSO toggle, session cookie name, session resolver, user lookup
SsoConfig SSO callbacks, auto-merging/provisioning, role mapping

Advanced Architecture

Stacking Custom Classes

Because authshield uses dynamic typing instead of a rigid subclass hierarchy, it plays nicely if you are already using a custom framework wrapper:

class MyEnterpriseApp(FastAPI):
    def company_telemetry(self):
        pass

# authshield blends perfectly into your custom pipeline
SecuredApp = shield_class(MyEnterpriseApp)

app = SecuredApp()
app.company_telemetry()  # Stays intact!
app.useAuth(...)         # Injected smoothly!

Roadmap / Coming Soon

  • Server-side and Encrypted Client-side Session Management
  • OAuth2 support for OAuth2.0 and OpenID Connect

License

This project is licensed under the MIT License.

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

authshield-0.1.0.tar.gz (17.6 kB view details)

Uploaded Source

Built Distribution

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

authshield-0.1.0-py3-none-any.whl (25.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: authshield-0.1.0.tar.gz
  • Upload date:
  • Size: 17.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.28 {"installer":{"name":"uv","version":"0.11.28","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for authshield-0.1.0.tar.gz
Algorithm Hash digest
SHA256 a01afb8d71e2070cf72ad396d1e3ee59b21446381ac9f9af502a7509add25946
MD5 a1dabfccd5410c1a83288f9e459d1996
BLAKE2b-256 f3544bbfdff3b0f9e6053c88f810b5387d85079776fa41c113a18257c8c262e8

See more details on using hashes here.

File details

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

File metadata

  • Download URL: authshield-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 25.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.28 {"installer":{"name":"uv","version":"0.11.28","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for authshield-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b2f3cfcef45aa0e7b6409f2673a203249c38c19e69241143733ce0c8d8fee2b0
MD5 3b792880c3cc7f70ee49b965eebf3a2e
BLAKE2b-256 20be9851ff97b677207bc3daaf9927a1bf79370d5e2c2d5ae9f8d31746b9d6e6

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