Skip to main content

A middleware for IP-based access restriction in Django

Project description

IP Restriction Middleware

A Django middleware for restricting access based on IP addresses.

Installation

pip install ip_restriction_middleware

# Usage
    To use the IPRestrictionMiddleware in your Django project, follow these steps:

## 1. Add Middleware to Django Settings
        Add IPRestrictionMiddleware to the MIDDLEWARE list in your Django settings (settings.py):

```sh
# settings.py

MIDDLEWARE = [
    # Other middleware...
    'ip_restriction_middleware.middleware.IPRestrictionMiddleware',
    # Other middleware...
]

## 2. Configure IP and Path Restrictions
        Add the following settings to configure the allowed IP addresses and restricted paths:

```sh
# settings.py

# List of IP addresses allowed to access non-admin paths
ALLOWED_IPS = ['127.0.0.1', '192.168.1.1']

# List of IP addresses allowed to access admin paths
ALLOWED_ADMIN_IPS = ['127.0.0.1', '192.168.1.1']

# Dictionary of restricted paths with IP restrictions
RESTRICTED_PATH = {
    "127.0.0.1": ["/test/servicev1/", "/test/servicev2/", "/test/servicev3/"],
    "2.0.34.22": ["/test/servicev2/"],
    "2.0.30.1,127.0.0.2": ["/test/servicev3/"]
}

# Middleware Details
## IPRestrictionMiddleware
    The IPRestrictionMiddleware class is responsible for checking the client's IP address against the allowed lists and the restricted paths. If the IP address is not allowed to access the requested path, the middleware returns a 403 Forbidden response.

#Initialization
    The middleware is initialized with the following attributes:

    allowed_ips: Set of allowed IP addresses for non-admin paths.
    allowed_admin_ips: Set of allowed IP addresses for admin paths.
    restricted_paths_and_IP: Dictionary of restricted paths and their allowed IP addresses.
    Request Processing
    The middleware processes each request to check the client's IP address and the requested path. If the IP address is not allowed, it returns a 403 Forbidden response.

#License
    This project is licensed under the MIT License. See the LICENSE file for details.

#Contributing
    Contributions are welcome! Please open an issue or submit a pull request if you have any improvements or bug fixes.

#Contact
    For questions or comments, please contact Bothraj P.

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

ip_restriction_middleware-1.0.2.tar.gz (3.8 kB view details)

Uploaded Source

Built Distribution

File details

Details for the file ip_restriction_middleware-1.0.2.tar.gz.

File metadata

File hashes

Hashes for ip_restriction_middleware-1.0.2.tar.gz
Algorithm Hash digest
SHA256 982dbfc274bab4eb7ba905c814945a23c0943dc1347126054e5fa2ac17e548db
MD5 f071bafd28388217e49cf7e3dbddd686
BLAKE2b-256 b95d883c1099683cf8207054c40d6a6de9fb31f22523d07e6e162febfe3661fd

See more details on using hashes here.

File details

Details for the file ip_restriction_middleware-1.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for ip_restriction_middleware-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 d1bd2f48078aef91c29535d228b6c7bcf275ddba788af65847b8959d757d3cd6
MD5 9a4186dc3b9a51175ce211045cd8bc7f
BLAKE2b-256 30d28ad89e73a2775c0cd3fef0bffdf5e2d7c01eb594229d645eec292f456701

See more details on using hashes here.

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