Skip to main content

Secure your Django app by controlling the access - grant or deny user access based on device and location, including VPN detection.

Project description

Django Forbid

PyPI Python Django Tests Docs

Django Forbid aims to make website access managed and secure for the maintainers. It provides a middleware to grant or deny user access based on device and/or location. It also supports VPN detection for banning users who want to lie about their country and geolocation. Also, users can use only the VPN detection feature or disable it.

Installation

python -m pip install django-forbid

Configuration

Add the django_forbid.apps.ForbidConfig to your INSTALLED_APPS in your Django project's settings.py file.

INSTALLED_APPS = [
    ...,  # other apps
    'django_forbid.apps.ForbidConfig',
]

Also, add the django_forbid.middleware.ForbidMiddleware to the MIDDLEWARE list of the project.

MIDDLEWARE = [
    ...,  # other middlewares
    'django_forbid.middleware.ForbidMiddleware',
]

Configuring the GEOIP_PATH variable in your project's settings is important. This variable should contain the path to the GeoLite2 database file. You should download the database and follow the Django documentation for proper configuration.

Usage

After connecting the Django Forbid to your project, you can define the set of desired zones to be forbidden or allowed. All you need is to set the DJANGO_FORBID variable in your project's settings. It should be a dictionary with the following keys:

  • DEVICES - list of devices to permit or forbid access to
  • COUNTRIES - list of countries to permit or forbid access to
  • TERRITORIES - list of territories to permit or forbid access to
  • OPTIONS - a dictionary for additional settings
    • VPN - use VPN detection and forbid access to VPN users
    • URL - set of URLs to redirect to when the user is located in a forbidden country or using a VPN
      • FORBIDDEN_LOC - the URL to redirect to when the user is located in a forbidden geolocation
      • FORBIDDEN_NET - the URL to redirect to when the user is using a forbidden network (VPN)
      • FORBIDDEN_DEV - the URL to redirect to when the user is using a forbidden device

The available device types are: smartphone, peripheral - refers to all hardware components that are attached to a computer, wearable - common types of wearable technology include smartwatches and smartglasses, phablet - a smartphone having a larger screen, console - PlayStation, Xbox, etc., display, speaker - Google Assistant, Siri, Alexa, etc., desktop, tablet, camera, player - iPod, Sony Walkman, Creative Zen, etc., phone, car - refers to a car browser and tv - refers to TVs having internet access.

DJANGO_FORBID = {
    'DEVICES': ['desktop', 'smartphone', 'console', 'tablet', 'tv'],
    'COUNTRIES': ['US', 'GB'],
    'TERRITORIES': ['EU'],
    'OPTIONS': {
        'VPN': True,
        'URL': {
            'FORBIDDEN_LOC': 'forbidden_location',
            'FORBIDDEN_NET': 'forbidden_network',
            'FORBIDDEN_DEV': 'forbidden_device',
        },
    },
}

The available country codes in the required ISO 3166 alpha-2 format are listed here. And the available continent codes (territories) are: AF - Africa, AN - Antarctica, AS - Asia, EU - Europe, NA - North America, OC - Oceania and SA - South America.

None of the settings are required. If you don't specify any settings, the middleware will not do anything.

Contribute

Any contribution is welcome. If you have any ideas or suggestions, feel free to open an issue or a pull request. And don't forget to add tests for your changes.

License

Copyright (C) 2023 Artyom Vancyan. MIT

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_forbid-0.1.8.tar.gz (13.6 kB view details)

Uploaded Source

Built Distribution

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

django_forbid-0.1.8-py3-none-any.whl (10.8 kB view details)

Uploaded Python 3

File details

Details for the file django_forbid-0.1.8.tar.gz.

File metadata

  • Download URL: django_forbid-0.1.8.tar.gz
  • Upload date:
  • Size: 13.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for django_forbid-0.1.8.tar.gz
Algorithm Hash digest
SHA256 fc97c044924c9aa72a7ab7368004fe8a79f0be29ca04f1126fa708d183f9c3c9
MD5 64b6ee57f787ed24371b9fbfe19334c0
BLAKE2b-256 806acd8a1f4b81b4f364e7fe1d241dd095f741db99d84bbce12d7665259bd3c5

See more details on using hashes here.

File details

Details for the file django_forbid-0.1.8-py3-none-any.whl.

File metadata

  • Download URL: django_forbid-0.1.8-py3-none-any.whl
  • Upload date:
  • Size: 10.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for django_forbid-0.1.8-py3-none-any.whl
Algorithm Hash digest
SHA256 b92bb6ebc5dd1e1f228f82c1d4f074b401eb4dba9379e0859d8207534e422497
MD5 8515b9e775344952c4e8919b8b2bafab
BLAKE2b-256 4565e9cce6d18bb7f9c5ecc46ec0e2e4593f06ab99bac4c124aff4b0d66f90ec

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