Authdog

Python · Flask

Authentication for Flask

A session resolver, a require_auth decorator, and a logout handler for Flask. The context resolves once and caches on flask.g. Validate Authdog sessions on every request, on the same wire as the Node SDKs.

One call for the session
Call authdog.session() in any view for a typed AuthdogContext, cached on flask.g with no per-request wiring.
One enforcement point
Decorate a view with @authdog.require_auth and unauthenticated requests get a 401 before your view runs.
Resolved once per request
The context lives on flask.g, so repeated session() calls never trigger a second userinfo lookup.
Plain Flask
Ordinary view decorators that compose with blueprints and leave your app structure unchanged.

Wire up the app

# app.py
from flask import Flask
from authdog.flask import Authdog

app = Flask(__name__)
authdog = Authdog(public_key=os.environ["PK_AUTHDOG"])

@app.get("/")
def index():
    return {"authenticated": authdog.session().is_authenticated}

Gate views with a decorator

# app.py
@app.get("/me")
@authdog.require_auth
def me():
    return authdog.session().user

Decorators and flask.g

Everything Flask apps need

A drop-in session resolver, a require_auth decorator, and a typed context cached on flask.g.

01

A session on flask.g

authdog.session() resolves the request's session and caches it on flask.g, returning a typed AuthdogContext with token, user, and is_authenticated from any view.

  • Typed AuthdogContext
  • Cached on flask.g
02

require_auth decorator

Stack @authdog.require_auth on a view and Flask rejects unauthenticated requests with a 401, returning the user object otherwise. This is the security boundary.

  • 401 before your view
  • Returns the verified user
03

At most one userinfo call

The resolved context is cached on flask.g, so calling session() several times in one request still makes a single outbound userinfo call.

  • One outbound call
  • Repeat calls are free
04

Plain Flask decorators

The bindings are ordinary view decorators. Stack them under your route, combine them with blueprints, and keep your app's structure unchanged.

  • Works with blueprints
  • No app restructuring
05

Safe logout handler

authdog.logout() returns a redirect Response that expires the session cookie and redirects to a redirect_uri sanitized against open redirects.

  • Expires the cookie
  • Open-redirect safe
06

Same wire as Node

It mirrors @authdog/express and @authdog/fastify on the wire, so one Authdog environment serves your Node and Flask services interchangeably.

  • One environment
  • Node and Python together
01

Install

pip install 'authdog-fastapi[flask]'.

02

Create the client

Instantiate Authdog with your environment's public key.

03

Gate views

Stack require_auth under the routes you protect.

Add auth to your Flask app.

pip install 'authdog-fastapi[flask]', add the decorator, and gate your views with require_auth today. Free to start, with secure defaults built in.