Skip to main content

django-basic-auth-ip-whitelist

GitHub actions CI status https://img.shields.io/pypi/v/django-basic-auth-ip-whitelist.svg https://img.shields.io/pypi/dm/django-basic-auth-ip-whitelist.svg

This simple package ships middleware that lets you to set basic authentication and IP whitelisting via Django settings.

Use case

This package has been created for staging and demo sites that need to be completely hidden from the Internet behind a password or accessible only to certain IP networks.

Do not depend on this package to protect highly valuable information. This package is at a good way to disable staging sites being discovered by search engines and Internet users trying to access staging sites. It is advised that any sensitive information is protected using Django authentication system.

Requirements

All supported versions of Python and Django are supported.

Installation

The package is on PyPI so you can just install it with pip.

pip install django-basic-auth-ip-whitelist

Configuration

In your Django settings you can configure the following settings:

BASIC_AUTH_LOGIN and BASIC_AUTH_PASSWORD

Credentials that you want to use with your basic authentication.

BASIC_AUTH_WHITELISTED_IP_NETWORKS

Set a list of network ranges (strings) compatible with Python’s ipaddress.ip_network that you want to be able to access the website without authentication from. It must be either a string with networks separated by comma or Python iterable.

Warning: See [Getting IP Address](#getting-ip-address) below for caveats around IP address detection.

BASIC_AUTH_REALM

String specifying the realm of the default response.

Example settings

MIDDLEWARE += [
    'baipw.middleware.BasicAuthIPWhitelistMiddleware'
]
BASIC_AUTH_LOGIN = 'somelogin'
BASIC_AUTH_PASSWORD = 'greatpassword'
BASIC_AUTH_WHITELISTED_IP_NETWORKS = [
    '192.168.0.0/28',
    '2001:db00::0/24',
]

Advanced customisation

Getting IP Address

By default, BasicAuthIPWhitelistMiddleware uses request.META["REMOTE_ADDR"] as the client’s IP, which corresponds to the IP address connecting to Django. If you have a reverse proxy (eg nginx in front), this will result in the IP address of nginx, not the client.

Correctly determining the IP address can vary between deployments. Guessing incorrectly can result in security issues. Instead, this library requires you configure this yourselves.

In most deployments, the X-Forwarded-For header can be used to correctly determine the client’s IP. We recommend django-xff to help parse this header correctly. Because django-xff overrides REMOTE_ADDR by default, it is natively supported by BasicAuthIPWhitelistMiddleware.

django-ipware is another popular library, however may take more customization to implement.

To fully customize IP address detection, you can set BASIC_AUTH_GET_CLIENT_IP_FUNCTION to a function which takes a request and returns a valid IP address:

BASIC_AUTH_GET_CLIENT_IP_FUNCTION = 'utils.ip.get_client_ip'

BASIC_AUTH_WHITELISTED_HTTP_HOSTS

Set a list of hosts that your website will be open to without basic authentication. This is useful if your website is hosted under multiple domains and you want only one of them to be publicly visible, e.g. by search engines.

This is by no means a security feature. Please do not use to secure your site.

BASIC_AUTH_WHITELISTED_HTTP_HOSTS = [
    'your-public-domain.com',
]

BASIC_AUTH_WHITELISTED_PATHS

Set a list of paths that your website will serve without basic authentication. This can be used to support API integrations for example with third-party services which don’t support basic authentication.

Paths listed in the setting BASIC_AUTH_WHITELISTED_PATHS are treated as roots, and any subpath will be whitelisted too. For example:

BASIC_AUTH_WHITELISTED_PATHS = [
    '/api',
]

This will open up the path https://mydomain.com/api/, as well as anything below it, e.g. https://mydomain.com/api/document/1/.

BASIC_AUTH_RESPONSE_TEMPLATE

If you want to display a different template on the 401 page, please use this setting to point at the template.

BASIC_AUTH_RESPONSE_TEMPLATE = '401.html'

BASIC_AUTH_RESPONSE_CLASS

If you want to specify custom response class, you can do so with this setting. Provide the path as a string.

The middleware handles disabling caching and setting the X-Robots-Tag header.

BASIC_AUTH_RESPONSE_CLASS = 'yourmodule.response.CustomUnathorisedResponse'

BASIC_AUTH_DISABLE_CONSUMING_AUTHORIZATION_HEADER

Set this setting to True if you want the Authorization HTTP header to not be deleted from the request object after it has been used by this package’s middleware.

BASIC_AUTH_DISABLE_CONSUMING_AUTHORIZATION_HEADER = True

Skip middleware

You can skip the middleware by setting _skip_basic_auth_ip_whitelist_middleware_check attribute on the request to True.

request._skip_basic_auth_ip_whitelist_middleware_check = True

This may be handy if you have other middleware that you want to have co-existing different middleware that restrict access to the website.

Caching

The middleware tries to configure any external caching securely:

  • Basic Auth responses (HTTP 401) are never cached.

  • Requests which pass the IP whitelist are never cached.

  • Authenticated responses are cached. Authorization is added to the Vary header.

  • Requests which pass the host whitelist are cached based on their host. Host is added to the Vary header.

Metadata

Release files for django-basic-auth-ip-whitelist 0.9.0

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-basic-auth-ip-whitelist 0.9.0
File Size Uploaded Provenance Actions
django_basic_auth_ip_whitelist-0.9.0.tar.gz 8.9 kB With provenance Details for django_basic_auth_ip_whitelist-0.9.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for django-basic-auth-ip-whitelist 0.9.0
File Interpreter ABI Platform Actions
django_basic_auth_ip_whitelist-0.9.0-py3-none-any.whl Python 3 none any Details for django_basic_auth_ip_whitelist-0.9.0-py3-none-any.whl

Total release size: 17.6 kB

Release files / django_basic_auth_ip_whitelist-0.9.0.tar.gz

Download URL django_basic_auth_ip_whitelist-0.9.0.tar.gz
Size 8.9 kB
Tags Source
SHA-256 checksum
How to use checksums
b1221fd2fbaf7087aa1935367a60bc68968169c4ee44cdd19552a9f754cb1a13
BLAKE2b-256 checksum
How to use checksums
a787663da88af6cb3958a82971bb20037f8d7a11e0881f96366aff5213533a75
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Nov 18, 2026.

Transparency log

Release files / django_basic_auth_ip_whitelist-0.9.0-py3-none-any.whl

Download URL django_basic_auth_ip_whitelist-0.9.0-py3-none-any.whl
Size 8.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
68d357049b1f74d95f0b288ec03051c44f2480f2c84c4c04c7f0579db818dd39
BLAKE2b-256 checksum
How to use checksums
f3b6e4dbe2d0f0663d38d0759ee2d909b3210d990fc46f0e6e846da1a4f99536
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Nov 18, 2026.

Transparency log

Security for django-basic-auth-ip-whitelist 0.9.0

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

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for django_basic_auth_ip_whitelist-0.9.0.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 Nov 18, 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