Skip to main content

Access a Django app with authorized IP address

Project description

django-ip-access

Python 3.11 Django 4.2 Python CI codecov Code style: black security: bandit

Access a Django app with authorized IP address

Installation

Install with pip

pip install django-ip-access

Setup

In order to make django-ip-access works, you'll need to follow the steps below.

Settings

First you need to add the following to your setings:

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.sessions',
    'django.contrib.messages',

    'django_ip_access',
    ...
)

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    
    'django_ip_access.middleware.IpAccessMiddleware',
    ...
)

AUTHENTICATION_BACKENDS = (
    'django.contrib.auth.backends.ModelBackend',
    
    'django_ip_access.backends.IpAccessBackend',
    ...
)

The optional settings with their default values:

IP_ACCESS_CACHE_KEY_PREFIX = "ip_auth_" # the cache key will be ip_auth_{ip}
IP_ACCESS_CACHE_TTL = 60 * 5 # in seconds. 5 minutes in this example

Migrations

Next, you need to run the migrations in order to update your database schema.

python manage.py migrate

Cache

Beware that django-ip-access relies on Django's cache framework to avoid trying to authenticate unauthenticated users on each request.

How to use ?

Once you are all set up, when a request to your app is made, the IpAccessMiddleware checks for if the IP address of the request exists in the admin panel and if the user associated to the IP address is active.

Future work

  • Allow the middleware to only runs for specific routes. This would reduces unnecessary overhead for requests that don't require IP-based authentication.

Tests

Testing is managed by pytest. required packages for testing can be installed with:

make install

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_ip_access-2.3.1.tar.gz (20.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_ip_access-2.3.1-py3-none-any.whl (20.3 kB view details)

Uploaded Python 3

File details

Details for the file django_ip_access-2.3.1.tar.gz.

File metadata

  • Download URL: django_ip_access-2.3.1.tar.gz
  • Upload date:
  • Size: 20.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.10

File hashes

Hashes for django_ip_access-2.3.1.tar.gz
Algorithm Hash digest
SHA256 0980d65dfad4c4c168955611788a6e64737c97c51cdbb4c3dfe1415e9ad7aba2
MD5 0b2326ac3c4e9f1194cad265d4664308
BLAKE2b-256 cbef24092ab5a3eed1b2a51e00367c97f22eedb20e28aadca763fb3a7995bf4a

See more details on using hashes here.

File details

Details for the file django_ip_access-2.3.1-py3-none-any.whl.

File metadata

File hashes

Hashes for django_ip_access-2.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b4fc0a6578f9a1c16d17d118d068d12c2f4ef9b123ecb74115068ec2241f72a2
MD5 cc0d5dc67df1b05e170b297be1e6b3a6
BLAKE2b-256 0f2a40bb863ccd745842e73fee99c329783187af2645d3f169dd0656ee954bb9

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 Pingdom Monitoring Sentry Error logging StatusPage Status page