sso: utility to fetch client ip address

(django-axes + django-simple-captcha) with sso is working at this point.

Signed-off-by: Joseph Nuthalpati <njoseph@thoughtworks.com>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
Joseph Nuthalpati 2017-09-05 14:31:57 +05:30 committed by James Valleroy
parent f330d09ec9
commit 03e1006dc3
No known key found for this signature in database
GPG Key ID: 77C0C75E7B650808
2 changed files with 12 additions and 3 deletions

View File

@ -243,6 +243,7 @@ def configure_django():
},
],
AXES_LOCKOUT_URL='locked',
AXES_BEHIND_REVERSE_PROXY=True,
CACHES={'default':
{'BACKEND': 'django.core.cache.backends.dummy.DummyCache'}},
CAPTCHA_FONT_PATH=['/usr/share/fonts/truetype/ttf-bitstream-vera/Vera.ttf'],

View File

@ -20,6 +20,7 @@ Views for the Single Sign On module of Plinth
import os
import urllib
import logging
from .forms import AuthenticationForm
@ -38,6 +39,8 @@ PRIVATE_KEY_FILE_NAME = 'privkey.pem'
SSO_COOKIE_NAME = 'auth_pubtkt'
KEYS_DIRECTORY = '/etc/apache2/auth-pubtkt-keys'
logger = logging.getLogger(__name__)
def set_ticket_cookie(user, response):
"""Generate and set a mod_auth_pubtkt as a cookie in the provided
@ -79,7 +82,7 @@ class CaptchaLoginView(LoginView):
if request.POST:
if request.user.is_authenticated:
ip = get_ip_address_from_request(request)
reset() # TODO reset(ip=ip)
reset(ip=ip)
return set_ticket_cookie(request.user, response)
else:
return response
@ -87,8 +90,13 @@ class CaptchaLoginView(LoginView):
def get_ip_address_from_request(request):
# TODO Not sure if this is the right way to get the client ip
return request.META['HTTP_X_FORWARDED_FOR']
x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
if x_forwarded_for:
ip = x_forwarded_for.split(',')[0]
else:
ip = request.META.get('REMOTE_ADDR')
logger.warning("IP address is " + ip)
return ip
class SSOLogoutView(LogoutView):