bgneal@690: """ receivers.py - Signal receivers for login related events. bgneal@690: bgneal@690: We log these events so that fail2ban can perform rate limiting. bgneal@690: bgneal@690: """ bgneal@690: import logging bgneal@690: bgneal@690: from django.contrib.auth.signals import (user_logged_in, user_logged_out, bgneal@690: user_login_failed) bgneal@690: bgneal@690: bgneal@690: # Get the auth logger that is monitored by fail2ban: bgneal@690: logger = logging.getLogger('auth') bgneal@690: bgneal@690: bgneal@690: def login_callback(sender, request, user, **kwargs): bgneal@690: """Signal callback function for a user logging in.""" bgneal@690: logger.info('User login signal: %s', user.username) bgneal@690: bgneal@690: bgneal@690: def logout_callback(sender, request, user, **kwargs): bgneal@690: """Signal callback function for a user logging in.""" bgneal@690: bgneal@690: if user: bgneal@690: logger.info('User logout signal: %s', user.username) bgneal@690: bgneal@690: def login_failed_callback(sender, credentials, **kwargs): bgneal@690: """Signal callback for a login failure event.""" bgneal@690: logger.error('User login failed signal from %s: %s', sender, bgneal@690: credentials.get('username')) bgneal@690: bgneal@690: bgneal@690: user_logged_in.connect(login_callback, dispatch_uid='antispam.receivers') bgneal@690: user_logged_out.connect(logout_callback, dispatch_uid='antispam.receivers') bgneal@690: user_login_failed.connect(login_failed_callback, bgneal@690: dispatch_uid='antispam.receivers')