I would like to register the IP address of the user in my Django application, especially for logging in, logging out, and failed login events. I use Django's built-in functions as follows:
from django.contrib.auth.signals import user_logged_in, user_logged_out, user_login_failed from ipware.ip import get_ip import logging logger = logging.getLogger(__name__) def log_logged_in(sender, user, request, **kwargs): logger.info("%s User %s successfully logged in" % (get_ip(request), user)) def log_logged_out(sender, user, request, **kwargs): logger.info("%s User %s successfully logged out" % (get_ip(request), user)) def log_login_failed(sender, credentials, **kwargs): logger.warning("%s Authentication failure for user %s" % ("...IP...", credentials['username'])) user_logged_in.connect(log_logged_in) user_logged_out.connect(log_logged_out) user_login_failed.connect(log_login_failed)
The problem is that I did not find a way to get the IP for the user_login_failed signal, since this function does not have request in the parameters ( https://docs.djangoproject.com/en/1.7/ref/contrib/auth/#module-django. contrib.auth.signals ). The credentials parameter is a dictionary that contains only the username and password fields.
How can I get the IP address for this signal?
Many thanks for your help.
source share