Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

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.

Requirements

  • Django 1.8, 1.9, 1.10, 1.11 and 2

  • Python 3.4, 3.5, 3.6 and 3.7

Installation

The package is on PyPI so you can just install if 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.

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

If you want to have a custom behaviour when getting IP, you can create a custom function that takes request as a parameter and specify path to it in the BASIC_AUTH_GET_CLIENT_IP_FUNCTION settings, e.g.

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_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.

BASIC_AUTH_RESPONSE_CLASS = 'yourmodule.response.CustomUnathorisedResponse'

Skip middleware

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

setattr(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.

Download files

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

Source Distribution

django-basic-auth-ip-whitelist-0.3a0.tar.gz (9.3 kB view details)

Uploaded Source

Built Distribution

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

django_basic_auth_ip_whitelist-0.3a0-py3-none-any.whl (11.3 kB view details)

Uploaded Python 3

File details

Details for the file django-basic-auth-ip-whitelist-0.3a0.tar.gz.

File metadata

  • Download URL: django-basic-auth-ip-whitelist-0.3a0.tar.gz
  • Upload date:
  • Size: 9.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.2 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.7.0

File hashes

Hashes for django-basic-auth-ip-whitelist-0.3a0.tar.gz
Algorithm Hash digest
SHA256 e959edb048cafad2934f030f69e1c5791757476dc5d822a2bac932ac396351c5
MD5 7bd60cc859ebb5e5973bb8d008999c48
BLAKE2b-256 c1cdabd640ccc7708c78ff860ff9c327422746451922462762b250a17a2dcb00

See more details on using hashes here.

File details

Details for the file django_basic_auth_ip_whitelist-0.3a0-py3-none-any.whl.

File metadata

  • Download URL: django_basic_auth_ip_whitelist-0.3a0-py3-none-any.whl
  • Upload date:
  • Size: 11.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.2 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.7.0

File hashes

Hashes for django_basic_auth_ip_whitelist-0.3a0-py3-none-any.whl
Algorithm Hash digest
SHA256 42084baf8803b3daa3d09e218cbc07539fd9df3fe1241d1d0b9c32a9fcea843e
MD5 4608f9452b0b98c0148b15e703a4c00b
BLAKE2b-256 6e6765756e84a2cd5b8b27b44a9d991b7c53fb06ecb7a8d5e2e80f57c5da3556

See more details on using hashes here.

Supported by

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