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

TLS_CLIENT_AUTHENTICATION_DEBUG_FAKE_HEADERS

  • Type: dict
  • Default: {}
  • Behavior:
    • Any headers defined in this dictionary (with "HTTP_" prefix) will be treated by this authentication middleware as if they had been sent as HTTP headers.
    • This setting is ignored unless DEBUG is set to true. It is intended to simplify the use of this library during development.

Example:

TLS_CLIENT_AUTHENTICATION_DEBUG_FAKE_HEADERS = {
    "HTTP_X_CLIENT_CERT_VALID": "SUCCESS",
    "HTTP_X_CLIENT_CERT_DN": "C=AU, ST=Victoria, L=Melbourne, CN=mp"
}

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.1.tar.gz (3.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_tls_certificate_auth-1.0.1-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for django_tls_certificate_auth-1.0.1.tar.gz
Algorithm Hash digest
SHA256 e3611dea2acd6793768d592be07b443d85a20fb6ed459e9407831f2ab2ca48e3
MD5 0d2980575777c0773c3aa21be283a971
BLAKE2b-256 84b3a63cdfeddfa987cb0f8965e4f5e75a1dd4e04d0c1952cdd9b9b02731e2db

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_tls_certificate_auth-1.0.1.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.1-py3-none-any.whl.

File metadata

File hashes

Hashes for django_tls_certificate_auth-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 17360a9f82055dd13d3cdca52882ee628d00dc6d56a48a744a71de15cb30cab3
MD5 d74b178ef5b42eeb7acf9cd0e8791259
BLAKE2b-256 3f875f2d30796f21926f4151e8e59a74038884878e59fb7959dcfe709f20e6c9

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_tls_certificate_auth-1.0.1-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