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
Release history Release notifications | RSS feed
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-1.0.0.tar.gz
(3.8 kB
view details)
Built Distribution
File details
Details for the file ip_restriction-1.0.0.tar.gz
.
File metadata
- Download URL: ip_restriction-1.0.0.tar.gz
- Upload date:
- Size: 3.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8d535704df54cb093f225e37d73c9b782552f0bcd5123eb1c15a08d45c736853 |
|
MD5 | d3a6ae3940da64089f61007dbbfc95f4 |
|
BLAKE2b-256 | 62785d1dd372f5ef743eb43bb96c4d3a1f62cc85bd2041cc3ed69a7e4c7bfe65 |
File details
Details for the file ip_restriction-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: ip_restriction-1.0.0-py3-none-any.whl
- Upload date:
- Size: 4.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7ed1327d3f102fadee08ea85a9e2b99f2749c57d3e4ebe0e4b49b6f166a0bbf2 |
|
MD5 | 7fb23dbaa2172e5a18b5239253789f0e |
|
BLAKE2b-256 | f76f07454f1a9fea35e0d09ea710dcca8ec3034ab5a5e1b620b42045c28a8a2f |