Skip to content

Getting started

Install

Install directly from GitHub with uv:

uv add "git+https://github.com/okelet/fastapi-simple-tinyauth"

The core package depends only on starlette. You use FastAPI to write the endpoints and dependencies; it is already part of any FastAPI project.

Wire up the middleware

Add TinyauthMiddleware with a TinyauthConfig that trusts your reverse proxy. The trusted-proxy list is mandatory:

from fastapi import FastAPI

from fastapi_simple_tinyauth.config import TinyauthConfig
from fastapi_simple_tinyauth.middleware import TinyauthMiddleware

config = TinyauthConfig(trusted_proxies=["127.0.0.1/32", "::1/128"])

app = FastAPI()
app.add_middleware(TinyauthMiddleware, config=config)

Read the user in an endpoint

from typing import Annotated

from fastapi import Depends
from fastapi.responses import JSONResponse

from fastapi_simple_tinyauth.helpers import optional_user, require_user
from fastapi_simple_tinyauth.models import AuthUser


@app.get("/")
async def index(user: Annotated[AuthUser | None, Depends(optional_user)] = None) -> JSONResponse:
    if user is None:
        return JSONResponse({"authenticated": False})
    return JSONResponse({"username": user.username})


@app.get("/me")
async def me(user: Annotated[AuthUser, Depends(require_user)]) -> JSONResponse:
    return JSONResponse({"username": user.username, "groups": list(user.groups)})

Try it locally

Run the bundled example and simulate Tinyauth by sending the identity headers yourself from loopback (which the example trusts):

uv run --group examples uvicorn examples.fastapi_app.main:app --reload
curl -H 'Remote-User: ada' -H 'Remote-Groups: admins,staff' http://127.0.0.1:8000/me

Next, see Configuration and Dependencies.