Initial commit of require_ipv6 Django app

Base files generated using:

```sh
django-admin startapp require_ipv6
```
This commit is contained in:
coox 2020-05-10 11:41:44 +02:00
parent 50139cea19
commit 0262782844
8 changed files with 70 additions and 0 deletions

View file

@ -1,3 +1,4 @@
DJANGO_ALLOWED_HOSTS=
DJANGO_DEBUG=True
DJANGO_SECRET_KEY=example-secret-key
REQUIRE_IPV6_DISABLE=

0
require_ipv6/__init__.py Normal file
View file

10
require_ipv6/apps.py Normal file
View file

@ -0,0 +1,10 @@
"""
Configuration of the require_ipv6 Django app
"""
from django.apps import AppConfig
class RequireIpv6Config(AppConfig):
name = "require_ipv6"
verbose_name = "Require IPv6"

View file

@ -0,0 +1,46 @@
"""
Middlewares of the require_ipv6 Django app
"""
from django.conf import settings
from django.core.exceptions import MiddlewareNotUsed, SuspiciousOperation
from django.shortcuts import render
from ipware import get_client_ip
import ipaddress
import logging
logger = logging.getLogger(__name__)
class RequireIPv6Middleware:
"""
A middleware that responds with an error if request is not done via IPv6
"""
def __init__(self, get_response):
# Requirement on IPv6 can be disabled by explicitly using a `True` value
# in setting `REQUIRE_IPV6_DISABLE`
should_disable = getattr(settings, "REQUIRE_IPV6_DISABLE", False) is True
if should_disable:
logger.warning("RequireIPv6 Middleware is disabled per settings.")
raise MiddlewareNotUsed
self.get_response = get_response
def __call__(self, request):
client_ip_address_as_string = get_client_ip(request)[0]
try:
client_ip_address = ipaddress.ip_address(client_ip_address_as_string)
except ValueError:
raise SuspiciousOperation(
"Invalid client IP address: %s" % client_ip_address_as_string
)
if not isinstance(client_ip_address, ipaddress.IPv6Address):
# Client does not use IPv6, render appropriate error page
return render(request, "require_ipv6/error.html", status=400)
# Client uses IPv6, continue processing request
return self.get_response(request)

View file

@ -0,0 +1,6 @@
<!doctype html>
<html lang="en">
<body>
<em>Sorry, only reachable by IPv6</em>
</body>
</html>

View file

@ -10,6 +10,7 @@
asgiref==3.2.7 # via django
django-environ==0.4.5 # via ungleich_screening_task (setup.py)
django-ipware==2.1.0 # via ungleich_screening_task (setup.py)
django==3.0.6 # via ungleich_screening_task (setup.py)
pytz==2020.1 # via django
sqlparse==0.3.1 # via django

View file

@ -3,6 +3,7 @@ from setuptools import setup
requirements = [
"django",
"django-environ",
"django-ipware",
]
dev_requirements = [

View file

@ -49,6 +49,7 @@ INSTALLED_APPS = [
"django.contrib.messages",
"django.contrib.staticfiles",
"friendly_cat",
"require_ipv6",
]
MIDDLEWARE = [
@ -56,6 +57,7 @@ MIDDLEWARE = [
"django.contrib.sessions.middleware.SessionMiddleware",
"django.middleware.common.CommonMiddleware",
"django.middleware.csrf.CsrfViewMiddleware",
"require_ipv6.middleware.RequireIPv6Middleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
@ -124,3 +126,6 @@ USE_TZ = True
# https://docs.djangoproject.com/en/3.0/howto/static-files/
STATIC_URL = "/static/"
# Allow settings of the “Require IPv6” application to be set as environment variables
REQUIRE_IPV6_DISABLE = env.bool("REQUIRE_IPV6_DISABLE", False)