Skip to main content

Django authentication middleware using TLS client certificates

Project description

django-tls-certificate-auth

Authenticate Django users using TLS client certificates terminated by a reverse proxy (for example, nginx).

The package reads certificate details from request headers and logs users in when they visit settings.LOGIN_URL.

Installation

Install from PyPI:

pip install django-tls-certificate-auth

Django Setup

1. Add middleware

Add the middleware to MIDDLEWARE (after session middleware so login() can write to the session):

MIDDLEWARE = [
    "django.contrib.sessions.middleware.SessionMiddleware",
    # ...
    "django_tls_certificate_auth.TLSClientCertAuthenticationMiddleware",
    # ...
]

2. Add authentication backend

Add the backend to AUTHENTICATION_BACKENDS:

AUTHENTICATION_BACKENDS = [
    "django_tls_certificate_auth.TLSClientCertAuthenticationBackend",
]

If you still want username/password (or other methods), include Django's default backend as well:

AUTHENTICATION_BACKENDS = [
    "django_tls_certificate_auth.TLSClientCertAuthenticationBackend",
    "django.contrib.auth.backends.ModelBackend",
]

3. Configure login URLs

The middleware runs only when the request path matches LOGIN_URL.

LOGIN_URL = "/accounts/login/"
LOGIN_REDIRECT_URL = "/"

When a valid certificate is present, the middleware authenticates the user and redirects to LOGIN_REDIRECT_URL.

Required Proxy Headers

Your reverse proxy must verify client certificates and pass these headers to Django:

  • X-Client-Cert-Valid: must be SUCCESS
  • X-Client-Cert-Dn: certificate subject DN string containing CN=<username>

nginx example:

ssl_client_certificate /etc/ssl/certs/mysite.ca.crt;
ssl_verify_client on;

proxy_set_header X-Client-Cert-Valid $ssl_client_verify;
proxy_set_header X-Client-Cert-Dn $ssl_client_s_dn;

How Username Mapping Works

The backend extracts CN from X-Client-Cert-Dn and looks up a Django user by your user model's USERNAME_FIELD.

Example DN:

/C=AU/O=Example/CN=alice

This maps to username alice.

Package Settings

The following settings can be set in your Django project's settings.py:

TLS_CLIENT_AUTHENTICATION_AUTO_CREATE

  • Type: bool
  • Default: False
  • Behavior:
    • False: if the mapped user does not exist, authentication fails with PermissionDenied.
    • True: if the mapped user does not exist, the backend creates it using create_user(...).

Example:

TLS_CLIENT_AUTHENTICATION_AUTO_CREATE = True

Django Settings Used by This Package

These are standard Django settings consumed by the package:

  • LOGIN_URL: path that triggers certificate authentication
  • LOGIN_REDIRECT_URL: destination after successful TLS login

Notes

  • Header names and success value are currently fixed in code:
    • X-Client-Cert-Valid must equal SUCCESS
    • X-Client-Cert-Dn must include a CN=... component
  • Ensure your proxy strips/overwrites client-supplied versions of these headers so users cannot spoof certificate identity.

Project details


Download files

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

Source Distribution

django_tls_certificate_auth-1.0.0.tar.gz (3.5 kB view details)

Uploaded Source

Built Distribution

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

django_tls_certificate_auth-1.0.0-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file django_tls_certificate_auth-1.0.0.tar.gz.

File metadata

File hashes

Hashes for django_tls_certificate_auth-1.0.0.tar.gz
Algorithm Hash digest
SHA256 c034e54bcaa69be2d7531c819256b2b5841f0451e54ce0fcb33abce2e5712791
MD5 5948f1f9aece4df972f1b91d1936d992
BLAKE2b-256 acb33ae2faa6a82633d8128622c755f042f3f31432bd4770a6fa7737272afdba

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_tls_certificate_auth-1.0.0.tar.gz:

Publisher: publish.yml on U-238/django-tls-certificate-auth

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_tls_certificate_auth-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_tls_certificate_auth-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3ce54e22b364807c27fc36d64cb47b3c2591fca9dc57bc1b5cb9d6b340172181
MD5 0952bd19c36b0e89051eb8abd9e30246
BLAKE2b-256 6c39ad9d568365ae22c2f1d459850422c72e136f0642099fc2dd1c5b399297cf

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_tls_certificate_auth-1.0.0-py3-none-any.whl:

Publisher: publish.yml on U-238/django-tls-certificate-auth

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

Supported by

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