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

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.1
File Size Uploaded Provenance Actions
django_auth_external-0.1.1.tar.gz 9.7 kB With provenance Details for django_auth_external-0.1.1.tar.gz

Built distribution (wheel)

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

Total release size: 16.8 kB

Release files / django_auth_external-0.1.1.tar.gz

Download URL django_auth_external-0.1.1.tar.gz
Size 9.7 kB
Tags Source
SHA-256 checksum
How to use checksums
ff11a9d21909bb85522f3f249f0cc11be95ae0e0c8bc049dd23361502e964dbc
BLAKE2b-256 checksum
How to use checksums
71e9268b5c32081f344b034baf84caa56588463a17880811bd4617966ab1fa95
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 25, 2026.

Transparency log

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

Download URL django_auth_external-0.1.1-py3-none-any.whl
Size 7.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
926c72afac6337e3812fe16cda036d254725895dd05a604a5eb7ce607250a867
BLAKE2b-256 checksum
How to use checksums
061f44b57213295ea99f2a0803d59f7d04c18300e751185cbfad7f93b7d92a14
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 25, 2026.

Transparency log

Security for django-auth-external 0.1.1

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.1.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 25, 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.1 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