Skip to main content

A Django middleware to restrict incoming IPs to admin panel

Project description

https://github.com/sdonk/django-admin-ip-restrictor/actions/workflows/ci.yml/badge.svg https://img.shields.io/pypi/v/django-admin-ip-restrictor.svg https://img.shields.io/pypi/pyversions/django-admin-ip-restrictor.svg https://img.shields.io/pypi/l/django-admin-ip-restrictor.svg

A Django middleware to restrict the access to the Django admin based on incoming IPs

Requirements

  • Python >= 3.10

  • Django >= 4.0

  • django-ipware >= 7

Usage

First install the package:

$ pip install django-admin-ip-restrictor

Then add the middleware to your settings:

MIDDLEWARE = [
    ...
    'admin_ip_restrictor.middleware.AdminIPRestrictorMiddleware'
]

Set these variables in your settings.py to control who has access to the admin (IPV4 and IPV6 can be mixed):

RESTRICT_ADMIN=True
ALLOWED_ADMIN_IPS=['127.0.0.1', '::1']
ALLOWED_ADMIN_IP_RANGES=['127.0.0.0/24', '::/1']
RESTRICTED_APP_NAMES=['admin']
TRUST_PRIVATE_IP=True

Use RESTRICTED_APP_NAMES to restrict the access to more apps. Admin app is always included.

If using environment variables make sure that the variables receive the right type of value. django-admin-ip-restrictor automatically converts the following formats:

$ export RESTRICT_ADMIN='true'
$ export ALLOWED_ADMIN_IPS='127.0.0.1,::1'
$ export ALLOWED_ADMIN_IP_RANGES='127.0.0.0/24,::/1'
$ export RESTRICTED_APP_NAMES='wagtail_admin,foo'

For RESTRICT_ADMIN also these values can be used: True, 1, false, False, 0

Use TRUST_PRIVATE_IP to skip checking IP addresses from a trusted private network.

Changelog

Full changelog at https://github.com/sdonk/django-admin-ip-restrictor/blob/master/CHANGELOG.rst

Run tests

Install uv (https://docs.astral.sh/uv/getting-started/installation/) then run:

$ uv run tox

Contribute

Fork the project and submit a PR!

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_admin_ip_restrictor-3.0.0.tar.gz (38.2 kB view details)

Uploaded Source

Built Distribution

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

django_admin_ip_restrictor-3.0.0-py3-none-any.whl (4.2 kB view details)

Uploaded Python 3

File details

Details for the file django_admin_ip_restrictor-3.0.0.tar.gz.

File metadata

File hashes

Hashes for django_admin_ip_restrictor-3.0.0.tar.gz
Algorithm Hash digest
SHA256 5f4beba0a0fd6f1e3501cb2ed711b3a36c7056a009ab5b55db6c95ba99bb97e7
MD5 716500b5662f7e9fd153674213a1b366
BLAKE2b-256 340d1e42b3f6ed97621b0501ec04bdce4cb4e912f83fc5ca8192a8d4cc01b23e

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_admin_ip_restrictor-3.0.0.tar.gz:

Publisher: publish.yml on sdonk/django-admin-ip-restrictor

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_admin_ip_restrictor-3.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_admin_ip_restrictor-3.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 68c0094539504c4f6570b90aa79072862369ea24555b946cc79e58aaa35af7f7
MD5 3cc545ac5f800ea7806135779af95134
BLAKE2b-256 d7a7dc1d9aeadb66274c9953888be4e23c7c9183df5997bee2be61b87f33dcc5

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_admin_ip_restrictor-3.0.0-py3-none-any.whl:

Publisher: publish.yml on sdonk/django-admin-ip-restrictor

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