Auth0 Authentication Backend for Django
Project description
django-auth0-auth
Authentication backend for Django with Auth0. As of June 2025, all of
the libraries that I saw for Django Auth0 target old versions of Django,
we'll start this by supporting version 5+ and python 3.11+ (3.10 only
has 1 year of life). Also, some of these don't actually subclass
Django's AuthBackend{.verbatim} and implement a login system that is
more "beside" Django than integrated with it. Because this is fully
integrated, we can use Django's built-in @login_required{.verbatim}
decorator and it's auth Mixins.
This project is not affiliated with Auth0.
Features:
- Fully automated end-to-end testing with Playwright to ensure Auth0 integration works correctly
- Complete Django authentication backend integration using Django's built-in auth system
- Support for modern Django (5+) and Python (3.11+) versions
The names are slightly confusing because there are a lot of one-off
projects (like this one) on PyPI that attempt the same thing. The repo
name here is django-auth0-auth{.verbatim}, the pypi name is
django-auth0-authbackend{.verbatim}, and the importable package is
auth0{.verbatim}. The installation instructions below reflect these
names.
Installation
Install the package from PyPI:
pip install django-auth0-authbackend
Usage
Take a look at the sample app provided in sample/{.verbatim} to see
how it's used in a MWE. There are only a few steps. First, include the
app in your apps in your Django settings:
INSTALLED_APPS = [
...,
"auth0",
]
Next, include the auth settings and auth backend (also in your Django settings):
AUTH0_CLIENT_ID = os.environ.get("AUTH0_CLIENT_ID")
AUTH0_CLIENT_SECRET = os.environ.get("AUTH0_CLIENT_SECRET")
AUTH0_DOMAIN = os.environ.get("AUTH0_DOMAIN")
AUTH0_AUDIENCE = os.environ.get("AUTH0_AUDIENCE")
# Optional: Configure callback URI (defaults to 'auth0_callback')
# AUTH0_CALLBACK_URI = 'auth0' # Use home URL instead of callback URL
# AUTH0_CALLBACK_URI = '/custom/path' # Use custom path
# AUTH0_CALLBACK_URI = 'https://example.com/callback' # Use full URL
# Optional: Map Auth0 user_info fields to Django User model fields
# These fields will be synced from Auth0 on every login
# AUTH0_USER_FIELD_MAPPING = {
# 'first_name': 'given_name', # User.first_name = user_info['given_name']
# 'last_name': 'family_name', # User.last_name = user_info['family_name']
# }
# Optional: Configure staff and superuser permissions based on Auth0 groups/roles
# AUTH0_GROUPS_FIELD = 'groups' # Field in user_info containing groups (default: 'groups')
# AUTH0_SUPERUSER_GROUP = 'admins' # Group that grants superuser access
# AUTH0_STAFF_GROUP = 'staff' # Group that grants staff access
AUTHENTICATION_BACKENDS = [
"auth0.backend.Auth0Backend",
]
Finally, include the urls in your project urls.py{.verbatim}:
from django.urls import path, include
urlpatterns = [
...,
path("auth0/", include("auth0.urls")),
]
Running the sample app
First, create an auth0 application.
Set up python however you prefer, I'll use a virtual env:
~/.pyenv/versions/3.11.10/bin/python -m venv .venv
source .venv/bin/activate
pip install .
Running the sample app, we can do:
export AUTH0_CLIENT_ID=...
export AUTH0_CLIENT_SECRET=...
export AUTH0_DOMAIN=...
export AUTH0_AUDIENCE=...
python manage.py migrate
python manage.py runserver
Go to http://localhost:8000/auth0 and log in!
Configuration Options
Callback URI Configuration
By default, this library uses the auth0_callback{.verbatim} URL as the
callback URI sent to Auth0. You can customize this behavior using the
AUTH0_CALLBACK_URI{.verbatim} setting:
# Use the home URL instead of callback URL (recommended for cleaner UX)
AUTH0_CALLBACK_URI = 'auth0'
# Use a custom path
AUTH0_CALLBACK_URI = '/custom/callback/path'
# Use a full URL (useful for different domains)
AUTH0_CALLBACK_URI = 'https://yourdomain.com/auth/callback'
The setting accepts:
- URL name (e.g.,
'auth0'{.verbatim} or'auth0_callback'{.verbatim}) - Relative path (e.g.,
'/custom/path'{.verbatim}) - Full URL (e.g.,
'https://example.com/callback'{.verbatim})
Note: Make sure to update your Auth0 application's "Allowed Callback URLs" to match your configured callback URI.
User Field Mapping Configuration
You can automatically sync fields from Auth0's userinfo to your
Django User model on every login using the
AUTH0_USER_FIELD_MAPPING{.verbatim} setting. This is useful for
keeping user profile information up-to-date.
# Map Auth0 user_info fields to Django User model fields
AUTH0_USER_FIELD_MAPPING = {
'first_name': 'given_name', # User.first_name = user_info['given_name']
'last_name': 'family_name', # User.last_name = user_info['family_name']
'field_foo': 'field_bar', # User.field_foo = user_info['field_bar']
}
The setting accepts a dictionary where:
- Keys are Django User model field names
- Values are Auth0 user
infofield names
Important:
- Fields are synced on every login, not just on user creation
- The email field is always synced automatically if present in
user
info - Make sure the Django User model has the fields you're trying to map
- Only fields present in user
infowill be updated
Common Auth0 userinfo fields:
given_name{.verbatim} - User's first namefamily_name{.verbatim} - User's last namenickname{.verbatim} - User's nicknamename{.verbatim} - User's full namepicture{.verbatim} - URL to user's profile pictureemail{.verbatim} - User's email (synced automatically)email_verified{.verbatim} - Email verification status
Staff and Superuser Permissions Configuration
You can automatically manage Django staff and superuser permissions based on Auth0 group or role membership. This is useful for integrating Auth0's authorization with Django's admin interface.
# Configure which Auth0 groups grant Django permissions
AUTH0_SUPERUSER_GROUP = 'admins' # Users in this group become superusers
AUTH0_STAFF_GROUP = 'staff' # Users in this group become staff
# Optional: Specify the field containing groups (default: 'groups')
AUTH0_GROUPS_FIELD = 'groups' # Or 'roles', or a custom claim
Configuration Options:
AUTH0_SUPERUSER_GROUP{.verbatim} - Group/role name that grants superuser (admin) accessAUTH0_STAFF_GROUP{.verbatim} - Group/role name that grants staff access to Django adminAUTH0_GROUPS_FIELD{.verbatim} - Field in userinfocontaining groups/roles (default:'groups'{.verbatim})
Important:
- Permissions are synced on every login
- Users are granted permissions when they're in the specified group
- Users lose permissions when removed from the group
- Both settings are optional - configure only what you need
- Make sure to configure Auth0 to include groups/roles in the
user
infotoken
Setting up Auth0 Groups:
To use this feature, you need to configure Auth0 to include groups in the user token:
- In Auth0 Dashboard, go to Actions > Flows > Login
- Create a custom action to add groups to the token:
exports.onExecutePostLogin = async (event, api) => {
const namespace = 'groups';
if (event.authorization) {
api.idToken.setCustomClaim(namespace, event.authorization.roles);
api.accessToken.setCustomClaim(namespace, event.authorization.roles);
}
};
- For custom group management, you can also use Auth0's Authorization Extension or implement custom logic
Example Use Cases:
- Grant Django admin access to users in Auth0's "staff" role
- Give full superuser permissions to users in "admins" group
- Use custom namespace like
https://yourapp.com/roles{.verbatim} viaAUTH0_GROUPS_FIELD{.verbatim}
Next steps
- Test that it works with the sample app (create an auth0 account to test)
- Run through genAI to look for general improvements: is all the logic we have in the views/index the right place for this?
- Add pre-commit code checks
- Add automated release via github action
- Flesh out user documentation here in README
- Add automated tests (including full e2e testing with Playwright)
- Profit
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_auth0_authbackend-0.3.2.tar.gz.
File metadata
- Download URL: django_auth0_authbackend-0.3.2.tar.gz
- Upload date:
- Size: 50.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0e43295dd8a433c5873c9edfe2f4b47cab51ff8ff551728c150aa797620642c8
|
|
| MD5 |
c0d64287e22f8d3cc181345dec733963
|
|
| BLAKE2b-256 |
a3b5bb10952ae3bec1f79b4ee447c406803d1fadca14bc680881e1c997eeb6e1
|
Provenance
The following attestation bundles were made for django_auth0_authbackend-0.3.2.tar.gz:
Publisher:
ci.yml on andyreagan/django-auth0-auth
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_auth0_authbackend-0.3.2.tar.gz -
Subject digest:
0e43295dd8a433c5873c9edfe2f4b47cab51ff8ff551728c150aa797620642c8 - Sigstore transparency entry: 1097461465
- Sigstore integration time:
-
Permalink:
andyreagan/django-auth0-auth@19d7d4fa6da2053cf8cb22dd5ddaae35ac9108bf -
Branch / Tag:
refs/tags/v0.3.2 - Owner: https://github.com/andyreagan
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@19d7d4fa6da2053cf8cb22dd5ddaae35ac9108bf -
Trigger Event:
push
-
Statement type:
File details
Details for the file django_auth0_authbackend-0.3.2-py3-none-any.whl.
File metadata
- Download URL: django_auth0_authbackend-0.3.2-py3-none-any.whl
- Upload date:
- Size: 10.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6450dd1fcf2f5ab17f138d653d0cf9d3d43f9c199ca2bf7bfbee315a86e2b8ed
|
|
| MD5 |
390485420d90522543523e17f150794b
|
|
| BLAKE2b-256 |
58e2fc6f3d9184e3de326ef229b85cdb54cf032a2c7d462104abf490db202d6b
|
Provenance
The following attestation bundles were made for django_auth0_authbackend-0.3.2-py3-none-any.whl:
Publisher:
ci.yml on andyreagan/django-auth0-auth
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_auth0_authbackend-0.3.2-py3-none-any.whl -
Subject digest:
6450dd1fcf2f5ab17f138d653d0cf9d3d43f9c199ca2bf7bfbee315a86e2b8ed - Sigstore transparency entry: 1097461537
- Sigstore integration time:
-
Permalink:
andyreagan/django-auth0-auth@19d7d4fa6da2053cf8cb22dd5ddaae35ac9108bf -
Branch / Tag:
refs/tags/v0.3.2 - Owner: https://github.com/andyreagan
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@19d7d4fa6da2053cf8cb22dd5ddaae35ac9108bf -
Trigger Event:
push
-
Statement type: