Skip to main content

JWT Allauth

PyPI Python Tests Docs License

Device-level session management for Django REST Framework, with refresh token theft detection.

JWT Allauth gives every login its own tracked session, rotates the refresh token on each use, and — when a rotated token is presented a second time — revokes the entire session rather than just rejecting the replayed credential. Around that it ships the endpoints an API needs to be usable on day one: login, social login, sign-up, invitations, e-mail verification, password reset, MFA and role-based permissions.

Built on Django REST Framework, django-allauth and Simple JWT.

The problem it solves

Rotating refresh tokens is standard advice, and every Django stack does it. What almost none of them do is handle the case rotation exists for.

When a refresh token is stolen, both the attacker and the legitimate user hold a credential from the same session. Whoever refreshes second presents a token that has already been rotated. A blacklist rejects that second request and stops there — so if the attacker refreshes first, the user gets locked out while the attacker keeps a valid, indefinitely renewable session. The theft never surfaces.

A replay is evidence that a session is compromised, and it is treated as such here: the whole session is revoked and both parties have to log in again. This is the behaviour described in OAuth 2.0 Security Best Current Practice §4.14.2, and it is the reason this library exists.

Refresh token rotation is not enough works through the whole argument — including the four ways an implementation of it fails silently, whether you use this library or write it yourself.

How it compares

Against the packages in the same slot — batteries-included authentication for a Django REST API:

dj-rest-auth djoser allauth headless JWT Allauth
JWT access/refresh tokens opt-in ¹ Simple JWT ✗ ² Simple JWT
Refresh token rotation Simple JWT's Simple JWT's own, compulsory
Replay revokes the whole session
Session records per device (IP, OS, browser) ✗ ³
Absolute session lifetime across rotations
Role and claims re-read from the DB on rotation
Login, sign-up, e-mail verification, password reset
Second factor TOTP, passkeys WebAuthn TOTP, recovery codes, WebAuthn TOTP, recovery codes
Social authentication ✓ ⁴
User invitations (admin creates, invitee sets password)  ⁵

¹ dj-rest-auth authenticates with DRF's own tokens by default. JWT means installing Simple JWT yourself and setting USE_JWT = True; it is not a dependency of the package.

² allauth.headless exposes AbstractTokenStrategy: "We make no assumptions in this regard. If you need access tokens, you will have to implement a token strategy that returns an access token here." The rows marked — follow from that: there is no token implementation to compare.

³ allauth.usersessions lists Django sessions, not JWT sessions.

⁴ Provider token and authorization code with PKCE, one generic endpoint per flow. The server-initiated redirect flow is not covered. A provider that vouches for an address an established account already holds signs that account in and leaves its password usable, rather than wiping it as allauth's e-mail authentication does. See Social login.

⁵ allauth's documentation states that "handling invitations is not supported by allauth" and points at a separate app (Advanced usage). django-invitations is that app, and it models a different thing: it stores an invitation, and the invitee then signs up themselves — with any address they like, not necessarily the one invited. Here the admin creates the account and fixes the identity; the invitee only proves the mailbox and chooses a password. Available alongside a public sign-up, or instead of one. See User invitations.

Requirements

Python 3.10+ and Django 4.2 through 6.1, on Django REST Framework 3.15+.

The dependencies carry no upper bounds. A startup check reports an allauth or Simple JWT major newer than the release was tested against (jwt_allauth.W003), rather than the install refusing to resolve.

Quick Start

Install using pip:

pip install django-jwt-allauth

Optional features ship as extras, so nothing you do not use is installed:

pip install "django-jwt-allauth[social]"   # sign in through a provider
pip install "django-jwt-allauth[mfa]"      # TOTP second factor
pip install "django-jwt-allauth[schema]"   # OpenAPI schema and Swagger UI

See Optional features.

You can start a new Django project with JWT Allauth pre-configured:

jwt-allauth startproject myproject

Then:

cd myproject
python manage.py makemigrations
python manage.py migrate
python manage.py runserver

Available options:

  • --email=True — enables email configuration in the project
  • --template=PATH — uses a custom template directory for project creation

Adding it to an existing project

No particular user model is required. Roles are read from a role field when the user model has one, and derived from is_staff / is_superuser when it does not — so a project that cannot swap AUTH_USER_MODEL (which is most of them past the first migration) still gets staff and superusers told apart from regular users, with nothing to migrate.

To define roles of your own, add the field to the user model you already have:

from django.contrib.auth.models import AbstractUser
from jwt_allauth.models import RoleMixin

class MyUser(RoleMixin, AbstractUser):
    pass

Existing staff rows need backfilling in that migration, or they drop to a regular user on their next login — see the user model documentation. New projects can skip all of it with AUTH_USER_MODEL = 'jwt_allauth.JAUser'.

Features

  • Refresh token whitelist: in place of Simple JWT's blacklist, every login gets a session row carrying the device it was issued to — IP, browser, OS, device model — so sessions can be listed and revoked individually, or all at once.
  • Replay detection: a rotated refresh token presented twice revokes the session it belongs to, on the assumption that two parties are holding it.
  • Absolute session lifetime: rotation cannot extend a session past JWT_ALLAUTH_SESSION_LIFETIME; the exp of both tokens is capped to it.
  • Claims that stay current: role, e-mail verification state and custom claims are re-read from the database on every rotation, so a privilege change applies within the lifetime of one access token instead of surviving until the refresh token expires.
  • Stateless by default: access tokens are verified without a database query. JWT_ALLAUTH_ACCESS_TOKEN_SESSION_CHECK trades one indexed query per request for immediate revocation of access tokens too.
  • Revocation on credential change: setting a password drops every session, every outstanding capability (unused reset links, MFA challenges) and every unconfirmed secondary address.
  • Role-based permissions: authorization from a JWT claim, with no user table lookup.
  • User invitations: an admin creates the account, the invitee proves the mailbox and chooses their own password. Alongside a public sign-up, or instead of one.
  • Social login: sign in through any provider django-allauth registers, by provider token or by authorization code with PKCE, with one generic endpoint per flow. An address a provider vouches for signs in the account that already holds it, without wiping the password that account still uses.
  • The rest of the flows: e-mail verification, password reset and change, MFA over TOTP with recovery codes, session logout.
  • Effortless setup: get a project running with a single command.

Why whitelisting?

The refresh token whitelist tracks the devices authorized by the user, stored in the database and checked when a refresh token is exchanged for a new access token.

This is what lets users revoke access to a stolen or lost device, or sign out of every session at once. Refresh tokens are regenerated on each use, so the whitelist is an accurate picture of which sessions are live — and it is what makes replay detection possible at all: a token that is not in the whitelist has either been rotated already or was forged, and both answers mean the session goes down.

Auto-renewal keeps sessions alive without repeated logins — ideal for mobile apps, where users should not have to reauthenticate every time they open the app.

Access tokens stay short-lived and self-contained, so ordinary API requests are authenticated without touching the database.

Email verification

To enable email verification, configure the email provider in your settings.py:

EMAIL_VERIFICATION = True
EMAIL_HOST = ...
EMAIL_PORT = ...
EMAIL_HOST_USER = ...
EMAIL_HOST_PASSWORD = ...
EMAIL_USE_TLS = ...
DEFAULT_FROM_EMAIL = ...

EMAIL_VERIFICATION also accepts 'mandatory', 'optional' and 'none' by name.

Redirection URLs

The relative url to be redirected once the email is verified:

EMAIL_VERIFIED_REDIRECT = ...

The relative url with the form to set the new password on password reset:

PASSWORD_RESET_REDIRECT = ...

If not configured, users will be redirected to the default password reset form at /jwt-allauth/password/reset/default/. This form provides a modern, responsive interface for password reset with proper form validation and error handling.

Templates

The templates can be configured in a JWT_ALLAUTH_TEMPLATES dictionary:

  • PASS_RESET_SUBJECT — subject of the password reset email (default: email/password/reset_email_subject.txt).
  • PASS_RESET_EMAIL — template of the password reset email (default: email/password/reset_email_message.html).
  • EMAIL_VERIFICATION_SUBJECT — subject of the signup email verification sent (default: email/signup/email_subject.txt).
  • EMAIL_VERIFICATION — template of the signup email verification sent (default: email/signup/email_message.html).

Example:

JWT_ALLAUTH_TEMPLATES = {
    'PASS_RESET_SUBJECT': 'mysite/templates/password_reset_subject.txt',
    ...
}

Documentation

Full documentation at jwt-allauth.readthedocs.io.

Acknowledgements

This project began as a fork of django-rest-auth. Thanks to the authors for their great work.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_jwt_allauth-1.5.0.tar.gz (119.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_jwt_allauth-1.5.0-py3-none-any.whl (161.4 kB view details)

Uploaded Python 3

File details

Details for the file django_jwt_allauth-1.5.0.tar.gz.

File metadata

  • Download URL: django_jwt_allauth-1.5.0.tar.gz
  • Upload date:
  • Size: 119.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for django_jwt_allauth-1.5.0.tar.gz
Algorithm Hash digest
SHA256 980f3d1aecd0448e2e670ecf1f34168d8bb07445db530c3812191ac3c26fe657
MD5 817acf9af18bf61b2bb6ebb963b88037
BLAKE2b-256 08857e715d663c04ad66016c99dfa122c8b8e7db30b1bd4efbef88e5e10487e5

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_jwt_allauth-1.5.0.tar.gz:

Publisher: publish.yml on castellanos-dev/jwt-allauth

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file django_jwt_allauth-1.5.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_jwt_allauth-1.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ec6fecdf7aabc547f925642e750b68d6d5bd666399a0167e714a6abf9b7d6383
MD5 dd025afa8d08e2b419fdc1429789e194
BLAKE2b-256 dfaf4c5666e0b8d4d4942ab9613b58a1257d6d5eab8696aad6712eca229928fa

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_jwt_allauth-1.5.0-py3-none-any.whl:

Publisher: publish.yml on castellanos-dev/jwt-allauth

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

1.5.1

2 files

This release

1.5.0 This release

2 files

1.4.1

2 files

1.4.0

2 files

1.3.1

2 files

1.3.0

2 files

1.2.6

2 files

1.2.5

2 files

1.2.4

2 files

1.2.3

2 files

1.2.2

2 files

1.2.1

2 files

1.2.0

2 files

1.1.1

2 files

1.1.0

2 files

1.0.3

2 files

1.0.2

2 files

1.0.1

2 files

1.0.0

2 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page