Mercurial > public > sg101
annotate antispam/receivers.py @ 934:9d6c2ed2f348
Add unit test for antispam signal handlers.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Thu, 16 Apr 2015 20:49:19 -0500 |
parents | 988782c6ce6c |
children |
rev | line source |
---|---|
bgneal@690 | 1 """ receivers.py - Signal receivers for login related events. |
bgneal@690 | 2 |
bgneal@690 | 3 We log these events so that fail2ban can perform rate limiting. |
bgneal@690 | 4 |
bgneal@690 | 5 """ |
bgneal@690 | 6 import logging |
bgneal@690 | 7 |
bgneal@690 | 8 from django.contrib.auth.signals import (user_logged_in, user_logged_out, |
bgneal@690 | 9 user_login_failed) |
bgneal@690 | 10 |
bgneal@690 | 11 |
bgneal@690 | 12 # Get the auth logger that is monitored by fail2ban: |
bgneal@690 | 13 logger = logging.getLogger('auth') |
bgneal@690 | 14 |
bgneal@690 | 15 |
bgneal@690 | 16 def login_callback(sender, request, user, **kwargs): |
bgneal@690 | 17 """Signal callback function for a user logging in.""" |
bgneal@690 | 18 logger.info('User login signal: %s', user.username) |
bgneal@690 | 19 |
bgneal@690 | 20 |
bgneal@690 | 21 def logout_callback(sender, request, user, **kwargs): |
bgneal@690 | 22 """Signal callback function for a user logging in.""" |
bgneal@690 | 23 |
bgneal@690 | 24 if user: |
bgneal@690 | 25 logger.info('User logout signal: %s', user.username) |
bgneal@690 | 26 |
bgneal@934 | 27 |
bgneal@690 | 28 def login_failed_callback(sender, credentials, **kwargs): |
bgneal@690 | 29 """Signal callback for a login failure event.""" |
bgneal@690 | 30 logger.error('User login failed signal from %s: %s', sender, |
bgneal@690 | 31 credentials.get('username')) |
bgneal@690 | 32 |
bgneal@690 | 33 |
bgneal@690 | 34 user_logged_in.connect(login_callback, dispatch_uid='antispam.receivers') |
bgneal@690 | 35 user_logged_out.connect(logout_callback, dispatch_uid='antispam.receivers') |
bgneal@690 | 36 user_login_failed.connect(login_failed_callback, |
bgneal@690 | 37 dispatch_uid='antispam.receivers') |