Skip to main content

A high-performance, framework-agnostic authorization and session management library for Python

Project description

AuthTuna 🐟

AuthTuna is an async-first, high-performance authorization, session, and user management library for Python, with first-class FastAPI support.

Note: While the core is designed to be framework-agnostic, the officially supported and actively maintained integration is FastAPI. Other adapters may be added in the future.

AuthTuna provides a robust, multi-layered security foundation for modern web applications. It is designed for developers who need to build complex, multi-tenant systems without compromising on security or performance. The library combines a powerful hierarchical permission model with an advanced, stateful session management system to actively defend against a wide range of modern threats.

Core Concepts

  • Hierarchical RBAC (Role-Based Access Control): Permissions are structured in a logical, multi-level hierarchy perfect for SaaS and collaborative platforms: Organization → Project → Team → Resource. A user's abilities can change depending on their context.
  • Object-Level Security: Go beyond simple roles with fine-grained permissions based on a resource's specific attributes, such as ownership. This allows for intuitive rules like "a user can always edit their own posts."
  • Advanced Session Management: A high-security, dual-state session model that actively detects and prevents session hijacking. It uses a server-side session store as the source of truth, providing full control over session validity.
  • Async SQLAlchemy: All database operations are async, using SQLAlchemy 2.x with async drivers (PostgreSQL via asyncpg, SQLite via aiosqlite).
  • Framework-Agnostic Core: The core engine is pure Python, with adapters for seamless integration with FastAPI.

Features

  • FastAPI-first integration: ready-to-use dependencies (get_current_user, PermissionChecker, RoleChecker) and session middleware.
  • Async SQLAlchemy models and manager for Users, Roles, Permissions, Sessions, Tokens, MFA, Social Accounts.
  • Dual-state session model: server-side sessions + JWT cookie with rotating random_string and periodic DB verification.
  • Session hijack detection: region/device fingerprint checks, IP tracking, automatic invalidation.
  • Email flows: verification, password reset, MFA notifications (Jinja templates included).
  • Extensible RBAC with scoped permissions (e.g., "project:read" with scope_from_path).
  • SQL-first design with PostgreSQL and SQLite support only.

Installation

Install from PyPI:

pip install authtuna

Configuration

Key environment variables in authtuna.core.config.Settings (can also be overridden via .env):

  • DEFAULT_DATABASE_URI: Async database URL (e.g., postgresql+asyncpg://user:pass@host/db or sqlite+aiosqlite:///./authtuna.db)
  • SESSION_TOKEN_NAME: Cookie name for session (default: session_token)
  • SESSION_LIFETIME_SECONDS / SESSION_ABSOLUTE_LIFETIME_SECONDS
  • SESSION_DB_VERIFICATION_INTERVAL: Seconds between DB checks for session validity
  • EMAIL_ENABLED / SMTP settings for email flows

See authtuna/core/config.py for full list and defaults.

Quick Start

FastAPI setup with session middleware and simple permission/role checks:

from fastapi import FastAPI, Depends
from authtuna.middlewares.session import DatabaseSessionMiddleware
from authtuna.integrations.fastapi_integration import get_current_user, PermissionChecker, RoleChecker
from authtuna.core.database import User

app = FastAPI()

# Attach the session middleware
app.add_middleware(DatabaseSessionMiddleware)

@app.get("/me")
async def whoami(user: User = Depends(get_current_user)):
    return {"id": user.id, "username": user.username, "email": user.email}

# Require a specific permission (AND by default)
@app.get("/projects/{project_id}")
async def read_project(
    project_id: str,
    user: User = Depends(PermissionChecker("project:read", scope_from_path="project_id"))
):
    return {"project_id": project_id, "user": user.id}

# Require one of multiple roles
@app.get("/admin")
async def admin_area(user: User = Depends(RoleChecker("admin", "moderator"))):
    return {"message": f"Welcome, {user.username}"}

Built-in Routers and Templates

AuthTuna ships optional routers for auth and social login and a set of Jinja templates you can mount quickly.

from fastapi import FastAPI
from authtuna.routers import auth as auth_router, social as social_router
from authtuna.middlewares.session import DatabaseSessionMiddleware

app = FastAPI()
app.add_middleware(DatabaseSessionMiddleware)

app.include_router(auth_router.router, prefix="/auth", tags=["auth"])
app.include_router(social_router.router, prefix="/auth", tags=["social"])

Philosophy

Robust security should be accessible, not an afterthought. AuthTuna provides the tools to manage complex authorization logic in a way that is both intuitive and highly secure.


For more details, see the code and inline documentation.

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

authtuna-0.1.4.tar.gz (57.7 kB view details)

Uploaded Source

Built Distribution

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

authtuna-0.1.4-py3-none-any.whl (86.9 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for authtuna-0.1.4.tar.gz
Algorithm Hash digest
SHA256 6445e9388bccd35f7fd777bc768e132960ef5069b340cb2d7af7105481c82965
MD5 60752bfc610413f31b3ca9c533c60520
BLAKE2b-256 264d6162d2695960fdb0e9aaa030bcac485fd1ac41fdb7b34bfc8a3cd168e5ff

See more details on using hashes here.

Provenance

The following attestation bundles were made for authtuna-0.1.4.tar.gz:

Publisher: publish-on-push.yml on shashstormer/AuthTuna

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

File details

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

File metadata

  • Download URL: authtuna-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 86.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for authtuna-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 7c74c035fc58455b38545b71285f189011bb35f17ec6f44291faa299b05f6909
MD5 f5149a3a47762d01d5d8ad3d3294a79a
BLAKE2b-256 d7bced01ed51cf567c7d36f960d01b313d2e73ab54dac51fff753570cfa08ea1

See more details on using hashes here.

Provenance

The following attestation bundles were made for authtuna-0.1.4-py3-none-any.whl:

Publisher: publish-on-push.yml on shashstormer/AuthTuna

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