Skip to main content

Windows Authentication Token handling for Django

PyPI - Version GitHub branch check runs

This package provides a Django middleware that provides a REMOTE_USER variable when authenticating in IIS using Windows Authentication.

When IIS on Windows is used as a webserver frontend for Django, authentication can be handled by the webserver using Windows Authentication. Microsoft recommends using the HttpPlatformHandler method to integrate Python webapps with IIS. Unlike the FastCGI handler which requires the old and unmaintained wfastcgi Python dependency, the HttpPlatformHandler doesn't set the REMOTE_USER variable after authentication. Instead, the authenticated user receives a Windows Authentication Token, which is available as a header named X-IIS-WindowsAuthToken.

This package provides a middleware that extracts this token from the request, retrieves the actual username from Windows, and sets the REMOTE_USER variable accordingly. This allows you to use Django's built-in remote user authentication mechanisms seamlessly.

Installation

You can install the package using pip:

pip install django-windowsauthtoken

On Windows, this will also install the pywin32 package, which is required to interact with Windows APIs. Note that pywin32 is only available on Windows, so this package will not work on other operating systems.

After installation, add the middleware to your Django settings:

MIDDLEWARE = [
    ...,
    "django.auth.middleware.AuthenticationMiddleware",
    # Ensure WindowsAuthTokenMiddleware comes after AuthenticationMiddleware and before RemoteUserMiddleware
    "django_windowsauthtoken.middleware.WindowsAuthTokenMiddleware",
    # You'll likely want to use Django's RemoteUserMiddleware too, to process the REMOTE_USER variable
    "django.contrib.auth.middleware.RemoteUserMiddleware",
    ...
]

If you're using Django's RemoteUserMiddleware, you might also want to set:

AUTHENTICATION_BACKENDS = [
    "django.contrib.auth.backends.RemoteUserBackend",
    ...
]

Now configure IIS to integrate with your Django application using the HttpPlatformHandler, and ensure that Windows Authentication is enabled for your site.

TODO: Add detailed instructions for setting up IIS.

Usage

Once the middleware is added, it will automatically handle the extraction of the Windows Authentication token from the X-IIS-WindowsAuthToken header and set the REMOTE_USER variable. You can then use Django's authentication system as usual.

The RemoteUserMiddleware will use the REMOTE_USER variable to authenticate users against Django's user model. If a user with the given username does not exist, it will by default create a new user and sign in as that user. See the Django documentation for more details on how RemoteUserMiddleware works.

Username format

By default, the middleware will set the REMOTE_USER variable to the username in the format DOMAIN\username. While this is true to the Windows Authentication standard, it may not be the format you want to use in your Django application, especially if you are using Django's default User model which does not allow backslashes in usernames.

If you prefer to use just the username without the domain, you can configure this by setting the following in your Django settings:

WINDOWSAUTHTOKEN_USERNAME_FORMATTER = "django_windowsauthtoken.formatters.format_username_only"

Alternatively, if you want to use the username@domain format, you can set:

WINDOWSAUTHTOKEN_USERNAME_FORMATTER = "django_windowsauthtoken.formatters.format_email_like"

Both of the above formats are acceptable by the builtin Django User model. You can also implement your own custom formatter function. The function should take two arguments: username and domain, and return the formatted username.

Debugging

When setting up IIS or the middleware is not working as expected, there is a debug view that shows all relevant information from the request. To enable it, add the following to your urls.py:

from django.contrib.auth.decorators import login_not_required
from django.urls import path
from django_windowsauthtoken.views import debug_view

urlpatterns = [
    ...,
    path("windowsauthtoken-debug/", login_not_required(debug_view), name="windowsauthtoken-debug"),
    ...,
]

You will also need to enable DEBUG in your Django settings. Then navigate to /windowsauthtoken-debug/ in your browser. This JSON view will display the headers and other relevant information from the request, which can help you diagnose issues with the middleware or IIS configuration. The actual need for the login_not_required decorator depends on your configuration.

Versioning

This project uses Semantic Versioning for versioning. Versions are in the format MAJOR.MINOR.PATCH, with optional pre-release and build metadata.

Versions are tagged as full releases (v1.2.3), development releases (v1.2.3.dev4) or release candidates (v1.2.3rc4).

Development

To contribute to the development of this package, clone the repository and install the development dependencies:

git clone https://github.com/whyscream/django-windowsauthtoken
cd django-windowsauthtoken
uv sync --dev

Note on pywin32

When developing on a non-Windows system, you can work around the absence of pywin32 by setting the following environment variable:

WINDOWSAUTHTOKEN_IGNORE_PYWIN32_ERRORS=true

This will allow you to run the tests and work on the code without pywin32, but note that for the middleware to return actual usernames, the package is required.

Running Tests

You can run the tests using pytest:

pytest

When making changes, ensure that you add tests for any new functionality and run the existing tests to verify that everything works as expected.

Coding standards

Code formatting and linting is done using ruff and pre-commit. See the pre-commit docs on how to set it up. You can check the formatting manually by running:

pre-commit run --all-files

Publishing to PyPI

There is a GitHub Action set up to publish new releases to PyPI. To publish a new version, simply create a tag in the repository using a git client or through the github website:

git tag v1.2.3
git push origin v1.2.3

The action will automatically build and upload the package to PyPI.

Thanks

Thanks to Lextudio for their excellent blog posts on using HttpPlatformHandler with Windows Authentication, which inspired this project:

License

This project is licensed under the BSD-3-Clause License. See the LICENSE file for details.

Metadata

Release files for django-windowsauthtoken 0.1.3

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-windowsauthtoken 0.1.3
File Size Uploaded Provenance Actions
django_windowsauthtoken-0.1.3.tar.gz 91.3 kB With provenance Details for django_windowsauthtoken-0.1.3.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for django-windowsauthtoken 0.1.3
File Interpreter ABI Platform Actions
django_windowsauthtoken-0.1.3-py3-none-any.whl Python 3 none any Details for django_windowsauthtoken-0.1.3-py3-none-any.whl

Total release size: 100.1 kB

Release files / django_windowsauthtoken-0.1.3.tar.gz

Download URL django_windowsauthtoken-0.1.3.tar.gz
Size 91.3 kB
Tags Source
SHA-256 checksum
How to use checksums
19ff2e5f70d9956994b11d3a4b91c1b2aaf2f2c488fc5b8bcf8e3d20100fbb07
BLAKE2b-256 checksum
How to use checksums
cde26fb160fc6433937316dfcfb80fdce5b42d448d05b10e13862f3322521a63
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

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 Jun 14, 2026.

Transparency log

Release files / django_windowsauthtoken-0.1.3-py3-none-any.whl

Download URL django_windowsauthtoken-0.1.3-py3-none-any.whl
Size 8.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7f9807068e7da50be6f5f8d148ab2c708c50cbf5601028d5016951ffccf9dc89
BLAKE2b-256 checksum
How to use checksums
0365d4b32ef0b1f058f1a6d8822eb34d5329a0b51876831b614355d05026c5b5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

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 Jun 14, 2026.

Transparency log

Security for django-windowsauthtoken 0.1.3

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_windowsauthtoken-0.1.3.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 Jun 14, 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.

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