dj-auth
A reusable Django app providing production-ready authentication out of the box:
- Email login — email/password auth via django-allauth, no username required
- Passkeys — WebAuthn/FIDO2 passkey login (biometrics, hardware keys)
- TOTP / 2FA — authenticator app support with recovery codes
- Social auth — Google and GitHub OAuth preconfigured, others easy to add
- Polished UI — Tailwind CSS templates for login, signup, MFA settings, and more
- Zero models — no migrations; drop it into any project without touching your schema
- Typed — ships a
py.typedmarker, so your mypy sees real signatures rather thanAny - Internal API — optional node-to-node user lookup for multi-node projects, with a route restriction that keeps it off the public internet
- System checks — warns you at startup if required dependencies are misconfigured
Quick Start
1. Install
# From PyPI (published as "ybz-dj-auth"; the importable package is still dj_auth)
pip install ybz-dj-auth
# Or install directly from GitHub:
# Latest commit on main
pip install git+https://github.com/damycra/dj-auth.git
# Specific tagged release (recommended for reproducible builds)
pip install git+https://github.com/damycra/dj-auth.git@v0.1.0
# Or download the wheel from a GitHub Release and install locally
pip install ybz_dj_auth-0.2.1-py3-none-any.whl
2. Add to INSTALLED_APPS
Important:
dj_authmust appear beforeallauthand allallauth.*entries. Django's template loader searches apps in order — if allauth comes first, its own unstyled built-in templates shadow dj_auth's styled overrides.manage.py checkwill warn you (dj_auth.W004) if the ordering is wrong.
INSTALLED_APPS = [
# ... Django built-ins ...
"django.contrib.sites", # required by allauth
"dj_auth", # must come before allauth
"allauth",
"allauth.account",
"allauth.socialaccount", # optional: social login
"allauth.mfa", # optional: MFA / passkeys
# ... your apps ...
]
allauth.socialaccountandallauth.mfaare genuinely optional: the login and signup pages detect whether they are installed and simply omit the social-login buttons / passkey button when they're not.manage.py checkemits a warning (dj_auth.W001/dj_auth.W002) so you know the feature is unavailable.
3. Configure MIDDLEWARE
Add AccountMiddleware after SessionMiddleware:
MIDDLEWARE = [
...
"django.contrib.sessions.middleware.SessionMiddleware",
...
"allauth.account.middleware.AccountMiddleware",
]
4. Configure AUTHENTICATION_BACKENDS
AUTHENTICATION_BACKENDS = [
"django.contrib.auth.backends.ModelBackend", # keep for admin
"allauth.account.auth_backends.AuthenticationBackend", # required by allauth
]
5. Include URLs
# config/urls.py
from django.urls import include, path
urlpatterns = [
path("accounts/", include("dj_auth.urls")),
# ... your other URLs ...
]
6. Run migrations
python manage.py migrate
That's it. Visit /accounts/login/ to see the login page.
Configuration
dj_auth injects sensible defaults on startup via AppConfig.ready(). These are standard allauth and Django settings — no special DJ_AUTH_* namespace. Override any of them in your project's settings.py using the normal allauth setting names.
| Setting | Default | Description |
|---|---|---|
ACCOUNT_LOGIN_METHODS |
{"email"} |
Login with email only (no username) |
ACCOUNT_SIGNUP_FIELDS |
["email*", "password1*", "password2*"] |
Signup form fields |
ACCOUNT_EMAIL_VERIFICATION |
"optional" |
"mandatory", "optional", or "none" |
LOGIN_REDIRECT_URL |
"/" |
Where to go after login |
LOGOUT_REDIRECT_URL |
"/" |
Where to go after logout |
MFA_SUPPORTED_TYPES |
["totp", "recovery_codes", "webauthn"] |
Enabled MFA methods |
MFA_PASSKEY_LOGIN_ENABLED |
True |
Show passkey login button |
MFA_PASSKEY_SIGNUP_ENABLED |
False |
Allow passkey signup (requires mandatory email verification) |
MFA_WEBAUTHN_ALLOW_INSECURE_ORIGIN |
True |
Allow HTTP in development — set to False in production |
SOCIALACCOUNT_PROVIDERS |
{} |
Social provider credentials (see below) |
SITE_ID |
1 |
Django sites framework site ID |
Template Overriding
dj_auth ships templates inside the package (dj_auth/templates/). Django's template loader checks project-level DIRS first, so you can override any template by placing a file at the same path in your project's template directory.
Overriding base.html
The main layout is base.html. Key blocks:
| Block | Required | Purpose |
|---|---|---|
content |
yes | Main page content — all auth pages render here |
head_title |
no | Page <title> text |
stylesheet |
no | The <link> to dj_auth's stylesheet (see Styling) |
extra_head |
no | Additional <head> content (CSS links, meta tags) |
nav |
no | Entire navigation bar |
nav_logo_href |
no | Logo link href (default: /) |
nav_logo_content |
no | Logo icon + site name HTML |
nav_links |
no | Right-side nav items |
footer |
no | Page footer |
extra_body |
no | Scripts before </body> |
To customise the layout, create templates/base.html in your project (which takes
priority over the package's version via Django's DIRS setting). You can start from
scratch or copy dj_auth/templates/base.html from the package as a starting point.
Do not use {% extends "base.html" %} in your override — that's circular.
Example — minimal base.html keeping dj_auth's styles but adding your own nav:
{# myproject/templates/base.html #}
{% load static %}<!DOCTYPE html>
<html lang="en">
<head>
<title>{% block head_title %}My App{% endblock %}</title>
{% block stylesheet %}<link rel="stylesheet" href="{% static 'dj_auth/dj_auth.css' %}">{% endblock %}
{% block extra_head %}{% endblock %}
</head>
<body>
<nav><!-- your nav here --></nav>
<main>{% block content %}{% endblock %}</main>
{% block extra_body %}{% endblock %}
</body>
</html>
Overriding allauth's internal pages
allauth's built-in pages (TOTP setup, WebAuthn management, etc.) extend
allauth/layouts/base.html. To make them match your project's branding:
{# myproject/templates/allauth/layouts/base.html #}
{% extends "base.html" %}
Styling
dj_auth ships a compiled stylesheet, dj_auth/static/dj_auth/dj_auth.css (about 27 KB),
holding exactly the Tailwind utilities its templates use. Both layouts link it, so the
auth screens are styled with no build step and nothing loaded from a third party.
collectstatic picks it up like any app's static files.
Recolouring
The templates name one brand colour by role, primary (bg-primary-600,
text-primary-700, focus:ring-primary-500, …), never a Tailwind hue. The stylesheet
compiles those to CSS variables with Tailwind's indigo as the default, so recolouring is
plain CSS — no Tailwind needed. Set any of the stops 50–950:
:root {
--color-primary-50: #f4f9e6;
--color-primary-500: #769826;
--color-primary-600: #5b7a1a; /* filled buttons: keep white text at 4.5:1 or better */
--color-primary-700: #4a6414; /* hover, and text on the light stops */
}
This works from any stylesheet on the page, loaded before or after dj_auth's: the
defaults sit in a cascade layer, and a declaration outside any layer always wins. The
neutral greys are slate-* and are not themeable.
Using your own Tailwind build instead
If your project writes its own templates with Tailwind, don't load both stylesheets — two builds on one page repeat the reset and every shared utility, and can disagree. Build one stylesheet that covers both, and link it in place of dj_auth's:
- Add dj_auth's templates to your build's sources. With Tailwind v4:
@source "<path-to-venv>/lib/python3.x/site-packages/dj_auth/templates";
(@sourcedoes not expand a glob in a directory segment, so the Python version is literal.) - Define
primaryin your theme. Without it everyprimary-*class compiles to nothing and the auth screens lose their buttons and links — silently.@theme { --color-primary-50: oklch(96.2% 0.018 272.314); /* … 100 through 900 … */ --color-primary-950: oklch(25.7% 0.09 281.288); }
- Override the
stylesheetblock to link your build — in your ownbase.html, or in any page that extends dj_auth's:{% block stylesheet %}<link rel="stylesheet" href="{% static 'css/main.css' %}">{% endblock %}
The templates are written for Tailwind v4. assets/dj_auth.src.css in this repository
is the build dj_auth itself uses, including three v3-era defaults the templates rely on
(grey borders, grey placeholders, pointer buttons); copy those into your own build too.
examples/basic does all of this for its home page.
Changing the templates
Edit a template, then rebuild the stylesheet with bin/build-css.sh (it downloads a
pinned Tailwind standalone binary on first run — no Node) and commit both. CI rebuilds
it and fails if the committed copy is stale.
Social Providers
Login/signup buttons are rendered only for providers that are actually usable:
a provider whose APP has an empty client_id (e.g. credentials left blank in
your .env) is hidden automatically, so you can keep provider apps in
INSTALLED_APPS and placeholder config in settings without showing dead
buttons. The buttons live in account/snippets/social_buttons.html, which you
can override like any other template.
SOCIALACCOUNT_PROVIDERS = {
"google": {
"APP": {
"client_id": "your-client-id",
"secret": "your-client-secret",
"key": "",
},
"SCOPE": ["profile", "email"],
"AUTH_PARAMS": {"access_type": "online"},
},
}
Add "allauth.socialaccount.providers.google" to INSTALLED_APPS.
GitHub
SOCIALACCOUNT_PROVIDERS = {
"github": {
"APP": {
"client_id": "your-client-id",
"secret": "your-client-secret",
"key": "",
},
"SCOPE": ["user:email"],
},
}
Add "allauth.socialaccount.providers.github" to INSTALLED_APPS.
For other providers, see the django-allauth documentation.
MFA / Passkeys
MFA is enabled by default when allauth.mfa is in INSTALLED_APPS. Users manage their security settings at /accounts/2fa/.
Passkey login
Passkeys are enabled by default (MFA_PASSKEY_LOGIN_ENABLED = True). The login page shows a "Sign in with a passkey" button automatically.
Passkey signup
Disabled by default because it requires mandatory email verification:
ACCOUNT_EMAIL_VERIFICATION = "mandatory"
MFA_PASSKEY_SIGNUP_ENABLED = True
Production WebAuthn
For production (HTTPS), remove the insecure origin allowance:
MFA_WEBAUTHN_ALLOW_INSECURE_ORIGIN = False
Internal node-to-node API
Optional, and off unless configured. A single-node project can ignore this entire section.
Once a project splits across more than one node — one holding auth_user, others holding
tenant data — the other nodes need to ask the first one about users. dj_auth.internal
provides that: one endpoint, its client, and the machinery to keep both off the public
internet.
The endpoint
# urls.py — explicitly, and NOT under accounts/
urlpatterns = [
path("accounts/", include("dj_auth.urls")),
path("", include("dj_auth.internal.urls")), # serves /internal/users/
]
# settings.py, on the node that holds auth_user
DJ_AUTH_INTERNAL_API_TOKENS = [os.environ["INTERNAL_API_TOKEN"]]
Including the URLs with no token raises ImproperlyConfigured at startup rather than
serving user data behind nothing. The way to not have an internal API is to not include
the URLs.
GET /internal/users/?email=… or ?ids=1,2,3 returns {"users": [{"id", "email", "full_name"}]}. full_name is get_full_name(), which under this package's email-only
signup defaults is usually empty — treat it as optional.
The client, on the other nodes
DJ_AUTH_INTERNAL_ORIGIN = "http://auth.internal:9000"
DJ_AUTH_INTERNAL_API_TOKENS = [os.environ["INTERNAL_API_TOKEN"]]
from dj_auth.internal.client import find_user_by_email, get_users_by_ids, AuthNodeUnavailable
try:
user = find_user_by_email("someone@example.com") # InternalUser | None
users = get_users_by_ids([1, 2, 3]) # list[InternalUser]
except AuthNodeUnavailable:
... # network error, timeout, non-2xx, or invalid JSON
call_auth_node(path, method=..., payload=...) is exported too, so a project's own
node-to-node calls get the same origin, token and error handling without reimplementing them.
Tokens are a list so rotation is two deploys rather than one synchronised one: add the new token everywhere, then remove the old. A caller presents the first; the server accepts any. Comparison is constant-time.
Keeping it off the public internet
This is the part most likely to be got wrong, so it is worth reading even if the rest looked obvious. Add the middleware first:
MIDDLEWARE = [
"dj_auth.internal.middleware.InternalRouteMiddleware",
...
]
It answers 404 — not 403 — to anything under /internal/ that did not arrive by the
internal route, and it does so before the token is examined, so nothing about token state is
observable from the wrong route. There are two independent checks, and neither is
sufficient in every environment:
| Setting | Use it when | Boundary or backstop |
|---|---|---|
DJ_AUTH_INTERNAL_PORT |
The internal port is genuinely unpublished — e.g. a Docker Compose port absent from ports: |
The boundary there |
DJ_AUTH_INTERNAL_CLIENT_NETWORKS |
The internal port is published on a public address — the normal case behind a platform proxy | The boundary there |
Configuring both is usually right. Bind the app to a second port and point the internal origin at it:
gunicorn ... --bind :8000 --bind :9000
DJ_AUTH_INTERNAL_PORT = "9000" # from SERVER_PORT, unforgeable
DJ_AUTH_INTERNAL_CLIENT_NETWORKS = ["10.0.0.0/8"] # CIDRs callers must come from
DJ_AUTH_INTERNAL_CLIENT_IP_HEADER = "" # empty = use REMOTE_ADDR
DJ_AUTH_INTERNAL_CLIENT_IP_HEADER names a header to trust for the client address, needed
when a proxy sits in the path and REMOTE_ADDR is therefore the proxy. Set it only where a
proxy always overwrites that header. On an app reachable without that proxy in front, a
trusted header is client-supplied input — which is exactly why a Host allowlist is not used
here. An absent or unparseable address fails closed.
Fly.io: the port check is not a boundary
Worth stating plainly, because it is counter-intuitive and it fails silently.
To reach a second port over Flycast — which is
what lets private traffic wake a suspended machine — you add a [[services]] block for it.
But Fly publishes a service's ports on every address the app holds, and its own
documentation is explicit:
If you have public IP addresses assigned to your app, then services in
fly.tomlare exposed to the public internet.
There is no per-service private-only flag. So the block that makes port 9000 reachable
privately makes it reachable publicly, and such a request passes the port check by
construction — it really did arrive on the internal port. Meanwhile /internal/ on 443
returns a tidy 404, so it all looks like it is working.
On Fly, use the client-network check:
[env]
DJ_AUTH_INTERNAL_PORT = '9000' # still worth setting; defence in depth
DJ_AUTH_INTERNAL_CLIENT_NETWORKS = 'fdaa::/16' # Fly's 6PN range: private, never public
DJ_AUTH_INTERNAL_CLIENT_IP_HEADER = 'Fly-Client-IP' # the proxy overwrites this; verified
fdaa::/16 covers every 6PN address and nothing public. Fly-Client-IP is safe to trust
here specifically because Fly Proxy overwrites whatever a client sends — confirmed against a
live deploy, with forged values including an unparseable one, all discarded.
System checks
manage.py check catches the configurations that otherwise fail quietly:
| ID | Meaning |
|---|---|
dj_auth.E006 |
Internal API enabled with no route restriction while DEBUG=False |
dj_auth.E007 |
A restriction is configured but the middleware is missing, so nothing enforces it |
dj_auth.W005 |
CLIENT_IP_HEADER set with no CLIENT_NETWORKS, so the header is read by nothing |
dj_auth.W006 |
Tokens that look like placeholders, or are under 32 characters, while DEBUG=False |
Running the Example App
cd examples/basic
pip install -e ../../ # install dj-auth from source
pip install -r requirements.txt
cp .env.example .env # edit as needed
python manage.py migrate
python manage.py createsuperuser
python manage.py runserver
Visit http://localhost:8000 to see the example app.
Running the Test Suite
pip install -e ".[dev]"
pytest
Or with tox (tests against multiple Django versions):
tox
To run just the lint checks:
tox -e lint
# or
ruff check dj_auth tests
Metadata
Release files for ybz-dj-auth 0.4.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| ybz_dj_auth-0.4.0.tar.gz | 86.4 kB | With provenance | Details for ybz_dj_auth-0.4.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| ybz_dj_auth-0.4.0-py3-none-any.whl | Python 3 | none | any | Details for ybz_dj_auth-0.4.0-py3-none-any.whl |
Total release size: 145.1 kB
Release files / ybz_dj_auth-0.4.0.tar.gz
| Download URL | ybz_dj_auth-0.4.0.tar.gz |
|---|---|
| Size | 86.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
51c8775b5e5c71357f80a32c9859f1f80a2d1fbe52b3ea1c488a976f36c05bff
|
|
BLAKE2b-256 checksum How to use checksums |
b86c93a4ba1bb6b56de063d2cf133856f070f67ed900115c4f1a9e1bd8769797
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 30, 2026.
Transparency logRelease files / ybz_dj_auth-0.4.0-py3-none-any.whl
| Download URL | ybz_dj_auth-0.4.0-py3-none-any.whl |
|---|---|
| Size | 58.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ada20776b9a6fc22feef9feaedc15ca2fd675d144fa577983ee30a34b6acdd1a
|
|
BLAKE2b-256 checksum How to use checksums |
ac7a3d72a26545bab2ccf4b799f65c02566128b482c5e85f673962f41a394ba2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 30, 2026.
Transparency logSecurity for ybz-dj-auth 0.4.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for ybz_dj_auth-0.4.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 30, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.