Skip to main content

Session-based auth for FastAPI: cookie + DB storage, real logout

Project description

FastAPI Anchor

PyPI version Python 3.10+ License: MIT GitHub

Session-based auth for FastAPI with HttpOnly cookies and optional PostgreSQL storage. Logout invalidates the session on the server so it stops working immediately.

Inspired by Lucia. Built on fastapi-sessions by Jordan Isaacs.


Features

Feature Description
Cookie-based Session ID in a signed HttpOnly cookie. No token in JS, better XSS story.
Backend storage In-memory (dev) or PostgreSQL (prod). Session lives in your DB.
Real logout Delete the session row; the next request with that cookie gets 401.
Pluggable Same frontend/backend/verifier model as fastapi-sessions. Mix and match.

Installation

pip install fastapi-anchor

With PostgreSQL support:

pip install fastapi-anchor[postgres]

Quick start (in-memory)

from uuid import UUID, uuid4
from fastapi import Depends, FastAPI, HTTPException, Response
from fastapi_anchor.backends.implementations import InMemoryBackend
from fastapi_anchor.frontends.implementations import SessionCookie, CookieParameters
from fastapi_anchor.session_verifier import SessionVerifier
from pydantic import BaseModel

class SessionData(BaseModel):
    user_id: str

cookie_params = CookieParameters(max_age=14*24*3600, httponly=True, secure=False)
cookie = SessionCookie(
    cookie_name="session",
    identifier="auth",
    auto_error=True,
    secret_key="your-secret",
    cookie_params=cookie_params,
)
backend = InMemoryBackend[UUID, SessionData]()

class AuthVerifier(SessionVerifier[UUID, SessionData]):
    def __init__(self):
        self._identifier = "auth"
        self._backend = backend
        self._auto_error = True
        self._auth_http_exception = HTTPException(status_code=401, detail="Invalid session")

    @property
    def identifier(self): return self._identifier
    @property
    def backend(self): return self._backend
    @property
    def auto_error(self): return self._auto_error
    @property
    def auth_http_exception(self): return self._auth_http_exception
    def verify_session(self, model: SessionData): return True

verifier = AuthVerifier()
app = FastAPI()

@app.post("/login")
async def login(user_id: str, response: Response):
    session_id = uuid4()
    await backend.create(session_id, SessionData(user_id=user_id))
    cookie.attach_to_response(response, session_id)
    return {"ok": True}

@app.get("/me", dependencies=[Depends(cookie)])
async def me(session_data: SessionData = Depends(verifier)):
    return session_data

@app.post("/logout")
async def logout(response: Response, session_id: UUID = Depends(cookie)):
    await backend.delete(session_id)
    cookie.delete_from_response(response)
    return {"ok": True}

PostgreSQL backend (real logout in production)

Create the table with anchor_sessions_table, then use PostgresBackend:

from sqlalchemy import MetaData
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine, async_sessionmaker
from fastapi_anchor.backends.implementations import (
    PostgresBackend,
    PostgresSessionData,
    anchor_sessions_table,
)

engine = create_async_engine("postgresql+asyncpg://user:pass@localhost/db")
async_session_factory = async_sessionmaker(
    engine, class_=AsyncSession, expire_on_commit=False
)
metadata = MetaData()
sessions_table = anchor_sessions_table(metadata)
# Create tables once: await engine.run_sync(metadata.create_all)

backend = PostgresBackend(async_session_factory, table=sessions_table)

Session payload for Postgres: user_id, expires_at, created_at (use PostgresSessionData or your own Pydantic model with those fields). On logout, call await backend.delete(session_id) and clear the cookie; the session is removed from the DB.

Links

License

MIT. Original fastapi-sessions by Jordan Isaacs (MIT).

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_anchor-0.1.1.tar.gz (10.0 kB view details)

Uploaded Source

Built Distribution

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

fastapi_anchor-0.1.1-py3-none-any.whl (11.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: fastapi_anchor-0.1.1.tar.gz
  • Upload date:
  • Size: 10.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for fastapi_anchor-0.1.1.tar.gz
Algorithm Hash digest
SHA256 31ec30ff5c6920749f0ced2a6f2ca47dbccf5efde4262fdb66f2febdf7f5b4d9
MD5 b8947c261e62fe665129df4fd5a67db7
BLAKE2b-256 c236400ef9439613cba80f371c0c66172c38e17277508cbd590daf38387d693d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: fastapi_anchor-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 11.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for fastapi_anchor-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1538ec81e2714a6dddcd00b20c12447277e6d86bd5ea0cc8d2c03311754c5f9f
MD5 033aabd2729f1b03f95d1079b138ddc1
BLAKE2b-256 ebee9bf1094ff3d8f4a7c4458dd5a1c78b21ec557bdbccc88bd31e265048c43e

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