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.1.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.1-py3-none-any.whl (25.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: authshield-0.1.1.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.1.tar.gz
Algorithm Hash digest
SHA256 28ad2d33aa734a260fd6f1cb786c409bbe1cad420882d6c1558b3a2dbb148a4b
MD5 8bd52194ec58accd49437511802099de
BLAKE2b-256 6b80747706aaafa056951ff868e9304d3f879f3af6862304b26f7f1265faeeb6

See more details on using hashes here.

File details

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

File metadata

  • Download URL: authshield-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 25.7 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a776de312f332cbcd1ad2197f6048caa5d57a44741f14f6ef0a1215bd6f72536
MD5 58a401e7ee7bcf23041c5ffc2b14aacf
BLAKE2b-256 f5ba244ba0fa526e5a86eadb9418b83d1fb522550be1dedc3359b9f50c4305e2

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