A FastAPI dependency
Depends(authdog.session) resolves the request's session and returns a typed AuthdogContext with token, user, and is_authenticated for any path operation.
- Typed AuthdogContext
- Any path operation
Python · FastAPI
A session resolver, a require_auth gate, and a logout handler for FastAPI, delivered as first-class dependencies that return a typed AuthdogContext. Validate Authdog sessions on every request, on the same wire as the Node SDKs.
Add the dependency
# main.py
from authdog.fastapi import Authdog
authdog = Authdog(public_key=os.environ["PK_AUTHDOG"])
@app.get("/")
async def index(ctx=Depends(authdog.session)):
return {"authenticated": ctx.is_authenticated}
Gate routes with require_auth
# routes.py
@app.get("/me")
async def me(user=Depends(authdog.require_auth)):
return user
Dependencies, not middleware
A drop-in session resolver, a require_auth gate, and a typed context, all expressed as FastAPI dependencies you compose, override, and test like any other.
Depends(authdog.session) resolves the request's session and returns a typed AuthdogContext with token, user, and is_authenticated for any path operation.
Depend on authdog.require_auth and FastAPI raises HTTPException(401) for unauthenticated requests, returning the user object otherwise. This is the security boundary.
The resolved context is cached on request.state, so combining session and require_auth on one request makes a single outbound userinfo call.
The bindings are plain FastAPI dependencies. Compose them with your own Depends(), override them in tests, and document them automatically in /docs.
authdog.logout(request) returns a RedirectResponse that expires the session cookie and redirects to a redirect_uri sanitized against open redirects.
It mirrors @authdog/express and @authdog/fastify on the wire, so one Authdog environment serves your Node and FastAPI services interchangeably.
pip install 'authdog-fastapi[fastapi]'.
Instantiate Authdog with your environment's public key.
Depend on require_auth where a user is required.
pip install 'authdog-fastapi[fastapi]', wire up the dependency, and gate your routes with require_auth today. Free to start, with secure defaults built in.