Do not whitelist access by IP range
This commit is contained in:
parent
22e0b7dc27
commit
22104d3565
2 changed files with 1 additions and 26 deletions
|
|
@ -1,7 +1,6 @@
|
||||||
from django.http import HttpResponseRedirect
|
from django.http import HttpResponseRedirect
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
|
|
||||||
import ipaddress
|
|
||||||
import re
|
import re
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -20,30 +19,9 @@ class LoginRequiredMiddleware:
|
||||||
If user is not authenticated and external, redirect to login view
|
If user is not authenticated and external, redirect to login view
|
||||||
before calling the view.
|
before calling the view.
|
||||||
"""
|
"""
|
||||||
if not request.user.is_authenticated and not self.check_ip(request):
|
if not request.user.is_authenticated:
|
||||||
if not self.whitelist_re.match(request.path_info):
|
if not self.whitelist_re.match(request.path_info):
|
||||||
return HttpResponseRedirect(settings.LOGIN_URL)
|
return HttpResponseRedirect(settings.LOGIN_URL)
|
||||||
|
|
||||||
response = self.get_response(request)
|
response = self.get_response(request)
|
||||||
return response
|
return response
|
||||||
|
|
||||||
def check_ip(self, request):
|
|
||||||
"""
|
|
||||||
Return true if IP is in authorized range
|
|
||||||
"""
|
|
||||||
# Get IP address
|
|
||||||
if 'HTTP_X_REAL_IP' in request.META:
|
|
||||||
ip = request.META.get('HTTP_X_REAL_IP')
|
|
||||||
elif 'HTTP_X_FORWARDED_FOR' in request.META:
|
|
||||||
ip = request.META.get('HTTP_X_FORWARDED_FOR').split(', ')[0]
|
|
||||||
else:
|
|
||||||
ip = request.META.get('REMOTE_ADDR')
|
|
||||||
ip = ipaddress.ip_address(ip)
|
|
||||||
|
|
||||||
# Check against ranges
|
|
||||||
if hasattr(settings, 'LOGIN_EXEMPT_IP_RANGE'):
|
|
||||||
for ip_range in settings.LOGIN_EXEMPT_IP_RANGE:
|
|
||||||
net = ipaddress.ip_network(ip_range)
|
|
||||||
if ip in net:
|
|
||||||
return True
|
|
||||||
return False
|
|
||||||
|
|
|
||||||
|
|
@ -165,6 +165,3 @@ SITE_ID = 1
|
||||||
|
|
||||||
# Photologue
|
# Photologue
|
||||||
PHOTOLOGUE_GALLERY_SAMPLE_SIZE = 1
|
PHOTOLOGUE_GALLERY_SAMPLE_SIZE = 1
|
||||||
|
|
||||||
# IP range whitelist
|
|
||||||
LOGIN_EXEMPT_IP_RANGE = ["185.230.76.0/22", "2a0c:700::/32"]
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue