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 beSUCCESSX-Client-Cert-Dn: certificate subject DN string containingCN=<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 withPermissionDenied.True: if the mapped user does not exist, the backend creates it usingcreate_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
DEBUGis 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 authenticationLOGIN_REDIRECT_URL: destination after successful TLS login
Notes
- Header names and success value are currently fixed in code:
X-Client-Cert-Validmust equalSUCCESSX-Client-Cert-Dnmust include aCN=...component
- Ensure your proxy strips/overwrites client-supplied versions of these headers so users cannot spoof certificate identity.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_tls_certificate_auth-1.0.1.tar.gz.
File metadata
- Download URL: django_tls_certificate_auth-1.0.1.tar.gz
- Upload date:
- Size: 3.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e3611dea2acd6793768d592be07b443d85a20fb6ed459e9407831f2ab2ca48e3
|
|
| MD5 |
0d2980575777c0773c3aa21be283a971
|
|
| BLAKE2b-256 |
84b3a63cdfeddfa987cb0f8965e4f5e75a1dd4e04d0c1952cdd9b9b02731e2db
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_tls_certificate_auth-1.0.1.tar.gz -
Subject digest:
e3611dea2acd6793768d592be07b443d85a20fb6ed459e9407831f2ab2ca48e3 - Sigstore transparency entry: 1418855538
- Sigstore integration time:
-
Permalink:
U-238/django-tls-certificate-auth@db947e060d5fe5103f3354686e1d0cb4e1d3c9f1 -
Branch / Tag:
refs/tags/1.0.1 - Owner: https://github.com/U-238
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@db947e060d5fe5103f3354686e1d0cb4e1d3c9f1 -
Trigger Event:
push
-
Statement type:
File details
Details for the file django_tls_certificate_auth-1.0.1-py3-none-any.whl.
File metadata
- Download URL: django_tls_certificate_auth-1.0.1-py3-none-any.whl
- Upload date:
- Size: 4.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
17360a9f82055dd13d3cdca52882ee628d00dc6d56a48a744a71de15cb30cab3
|
|
| MD5 |
d74b178ef5b42eeb7acf9cd0e8791259
|
|
| BLAKE2b-256 |
3f875f2d30796f21926f4151e8e59a74038884878e59fb7959dcfe709f20e6c9
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_tls_certificate_auth-1.0.1-py3-none-any.whl -
Subject digest:
17360a9f82055dd13d3cdca52882ee628d00dc6d56a48a744a71de15cb30cab3 - Sigstore transparency entry: 1418855647
- Sigstore integration time:
-
Permalink:
U-238/django-tls-certificate-auth@db947e060d5fe5103f3354686e1d0cb4e1d3c9f1 -
Branch / Tag:
refs/tags/1.0.1 - Owner: https://github.com/U-238
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@db947e060d5fe5103f3354686e1d0cb4e1d3c9f1 -
Trigger Event:
push
-
Statement type: