Skip to main content

django-auth-external

PyPI version Python License: MIT Tests

JWT token generation and validation for Django. Built for architectures where one service issues tokens and multiple services validate them using a shared secret.

This library handles only the cryptographic layer: signing, verification, and expiry. Session invalidation and revocation are the responsibility of the consuming application.

install

pip install django-auth-external

With Django REST Framework support:

pip install django-auth-external[drf]

quick start

Define the token schema in settings.py:

AUTH_EXTERNAL = {
    "SECRET_KEY": "your-secret-key",
    "CLAIMS": ["user_id", "email", "modules"],
    "TOKEN_TTL_SECONDS": 28800,  # 8 hours
}

Add the middleware:

MIDDLEWARE = [
    ...
    "django_auth_external.middleware.AuthExternalMiddleware",
]

Generate a token (in the authentication service):

from django_auth_external import TokenConfig, generate_token

config = TokenConfig(secret_key="your-secret-key", claims=["user_id", "email", "modules"])
token = generate_token(
    {"user_id": "abc-123", "email": "user@example.com", "modules": ["api", "admin"]},
    config,
)

Validate in a view:

def my_view(request):
    if request.auth_payload is None:
        return HttpResponse(status=401)
    user_id = request.auth_payload["user_id"]
    ...

configuration

Key Required Default Description
SECRET_KEY yes — Signing secret. Must be identical across all services.
CLAIMS no [] Claim names required in the token payload.
ALGORITHM no "HS256" JWT signing algorithm.
TOKEN_TTL_SECONDS no 28800 Token lifetime in seconds (8 hours).
USER_ID_CLAIM no "user_id" Claim name used as user identifier.

token generation

generate_token validates that the payload contains all configured claims before signing:

from django_auth_external import TokenConfig, generate_token
from django_auth_external.exceptions import MissingClaim

config = TokenConfig(secret_key="secret", claims=["user_id", "email"])

generate_token({"user_id": "abc"}, config)                          # raises MissingClaim: email
generate_token({"user_id": "abc", "email": "x@y.com"}, config)     # ok

Extra fields in the payload are included in the token without modification.

DRF integration

REST_FRAMEWORK = {
    "DEFAULT_AUTHENTICATION_CLASSES": [
        "django_auth_external.drf.ExternalTokenAuthentication",
    ],
}

request.user will be the token payload dict. request.auth will be the raw token string.

exceptions

Exception When
TokenExpired Token has passed its expiry time.
TokenInvalid Signature is wrong or token is malformed.
MissingClaim Payload is missing a required claim during generation.
AccessDenied General access refusal (for use in calling code).

All inherit from AuthExternalError.

license

MIT

Metadata

Release files for django-auth-external 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-auth-external 0.1.0
File Size Uploaded Provenance Actions
django_auth_external-0.1.0.tar.gz 9.6 kB With provenance Details for django_auth_external-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for django-auth-external 0.1.0
File Interpreter ABI Platform Actions
django_auth_external-0.1.0-py3-none-any.whl Python 3 none any Details for django_auth_external-0.1.0-py3-none-any.whl

Total release size: 16.7 kB

Release files / django_auth_external-0.1.0.tar.gz

Download URL django_auth_external-0.1.0.tar.gz
Size 9.6 kB
Tags Source
SHA-256 checksum
How to use checksums
dd3f30679b62b91a35ddbf136c932d2e8eb60ff378596fb67244e80c4b3dc867
BLAKE2b-256 checksum
How to use checksums
27880cc41c3dfbaecafc8f9b6b3809283c4570d115de2718ba4f961ed7046089
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 Aug 24, 2026.

Transparency log

Release files / django_auth_external-0.1.0-py3-none-any.whl

Download URL django_auth_external-0.1.0-py3-none-any.whl
Size 7.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
05b647ec023e98a38705d712263442d64ecd22f8f5e20eadef65d5f00abce6c5
BLAKE2b-256 checksum
How to use checksums
b4f8a2da22d9254eef79ea1d19256968307efe38546b7b59b3a04e8e11b138c0
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 Aug 24, 2026.

Transparency log

Security for django-auth-external 0.1.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

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for django_auth_external-0.1.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 Aug 24, 2026.

Transparency log

A 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.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page