Skip to main content

A library that allows IP restrictions for views/endpoints in Django REST framework.

Project description

DRF IP Restrictions

PyPI version Run linter and tests Codecov

A library that allows IP restrictions for views/endpoints in Django REST framework.

Installation

  1. Install using pip:
pip install drf-ip-restrictions
  1. Add the library to your INSTALLED_APPS list.
INSTALLED_APPS = [
    ...
    'drf_ip_restrictions',
    ...
]
  1. Override the allowed IP addresses your settings.py according to your needs:
# within settings.py

DRF_IP_RESTRICTION_SETTINGS = {
    "ALLOWED_IP_LIST": ["127.0.0.1"],
}

Usage

Add the AllowedIpList class to any views / endpoints that should only provide access for the configured IP addresses, e.g. to restrict a view set:

# within views.py

class MyViewSet(viewsets.ModelViewSet):
    permission_classes = (AllowedIpList,)
    ...

or to restrict only a single action:

# within views.py

class MyViewSet(viewsets.ModelViewSet):
    ...

    @action(
        detail=False,
        methods=["get"],
        http_method_names=["get"],
        authentication_classes=[],
        permission_classes=[AllowedIpList],  # <-- this is the important part for IP restrictions to work
        url_path=r"my-method",
    )
    def my_method(self, request, *args, **kwargs):
        # do stuff and return rest_framework.response.Response in the end

Django Compatibility Matrix

If your project uses an older verison of Django or Django Rest Framework, you can choose an older version of this project.

This Project Python Version Django Version Django Rest Framework
1.0.* 3.7, 3.8, 3.9, 3.10 3.2, 4.0 3.12, 3.13

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

drf-ip-restrictions-1.1.1.tar.gz (3.9 kB view hashes)

Uploaded Source

Built Distribution

drf_ip_restrictions-1.1.1-py3-none-any.whl (4.4 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page