Authdog

Python · Django

Authentication for Django

A middleware that resolves the session, a require_auth decorator, and a logout handler for Django, integrated with the request and view lifecycle. Validate Authdog sessions on every request, on the same wire as the Node SDKs.

One line in MIDDLEWARE
Register authdog.middleware and every request carries a resolved context. No per-view wiring.
One enforcement point
Decorate a view with @authdog.require_auth and unauthenticated requests get a 401 before your view runs.
Lazy Django import
import authdog.django works without Django configured; the framework is imported only when a binding is called.
At most one userinfo call
Middleware, decorator, and session() share one resolved context per request.

Register the middleware

# auth.py
from authdog.django import Authdog

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

# settings.py
MIDDLEWARE = [..., "myapp.auth.authdog.middleware"]

Gate views with a decorator

# views.py
@authdog.require_auth
def me(request):
    return JsonResponse(authdog.session(request).user)

Middleware and decorators

Everything Django apps need

A drop-in middleware that resolves the session, a require_auth decorator, and a typed context on request.authdog_context.

01

One middleware, resolved once

Add authdog.middleware to MIDDLEWARE and every request gets a resolved AuthdogContext on request.authdog_context. It never raises: anonymous requests simply resolve to an unauthenticated context.

  • Never raises
  • Anonymous requests resolve too
02

require_auth decorator

Wrap a view in @authdog.require_auth and Django returns JsonResponse({'error': 'Unauthorized'}, status=401) for unauthenticated requests. This is the security boundary.

  • 401 before your view
  • Per-view opt-in
03

Read the session anywhere

authdog.session(request) returns the typed context with token, user, and is_authenticated from any view, resolved by the middleware and reused for the request.

  • Typed context
  • Reused per request
04

At most one userinfo call

The resolved context is cached on the request, so the middleware, a require_auth view, and session() all share a single outbound userinfo call.

  • Cached on the request
  • One outbound call
05

Safe logout handler

authdog.logout(request) returns an HttpResponseRedirect that clears the session cookie and redirects to a redirect_uri sanitized against open redirects.

  • Clears 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 Django services interchangeably.

  • One environment
  • Node and Python together
01

Install

pip install 'authdog-fastapi[django]'.

02

Register

Add authdog.middleware to MIDDLEWARE in settings.py.

03

Gate views

Decorate the views that require a user.

Add auth to your Django app.

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