diff antispam/receivers.py @ 690:988782c6ce6c

For #48, rework blocking code to use fail2ban.
author Brian Neal <bgneal@gmail.com>
date Sun, 01 Sep 2013 00:15:42 -0500
parents
children 9d6c2ed2f348
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/antispam/receivers.py	Sun Sep 01 00:15:42 2013 -0500
@@ -0,0 +1,36 @@
+""" receivers.py - Signal receivers for login related events.
+
+We log these events so that fail2ban can perform rate limiting.
+
+"""
+import logging
+
+from django.contrib.auth.signals import (user_logged_in, user_logged_out,
+        user_login_failed)
+
+
+# Get the auth logger that is monitored by fail2ban:
+logger = logging.getLogger('auth')
+
+
+def login_callback(sender, request, user, **kwargs):
+    """Signal callback function for a user logging in."""
+    logger.info('User login signal: %s', user.username)
+
+
+def logout_callback(sender, request, user, **kwargs):
+    """Signal callback function for a user logging in."""
+
+    if user:
+        logger.info('User logout signal: %s', user.username)
+
+def login_failed_callback(sender, credentials, **kwargs):
+    """Signal callback for a login failure event."""
+    logger.error('User login failed signal from %s: %s', sender,
+                 credentials.get('username'))
+
+
+user_logged_in.connect(login_callback, dispatch_uid='antispam.receivers')
+user_logged_out.connect(logout_callback, dispatch_uid='antispam.receivers')
+user_login_failed.connect(login_failed_callback,
+                          dispatch_uid='antispam.receivers')