diff antispam/decorators.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 89b240fe9297
children 4a49d4ac319f
line wrap: on
line diff
--- a/antispam/decorators.py	Sat Aug 31 14:50:03 2013 -0500
+++ b/antispam/decorators.py	Sun Sep 01 00:15:42 2013 -0500
@@ -2,33 +2,19 @@
 This module contains decorators for the antispam application.
 
 """
-from datetime import timedelta
 import json
 from functools import wraps
+import logging
 
-from django.shortcuts import render
 
-from antispam.rate_limit import RateLimiter, RateLimiterUnavailable
-
-
-def rate_limit(count=10, interval=timedelta(minutes=1),
-        lockout=timedelta(hours=8)):
+def log_auth_failures(auth_type):
 
     def decorator(fn):
+        logger = logging.getLogger('auth')
 
         @wraps(fn)
         def wrapped(request, *args, **kwargs):
 
-            ip = request.META.get('REMOTE_ADDR')
-            try:
-                rate_limiter = RateLimiter(ip, count, interval, lockout)
-                if rate_limiter.is_blocked():
-                    return render(request, 'antispam/blocked.html', status=403)
-
-            except RateLimiterUnavailable:
-                # just call the function and return the result
-                return fn(request, *args, **kwargs)
-
             response = fn(request, *args, **kwargs)
 
             if request.method == 'POST':
@@ -45,13 +31,12 @@
                     success = json_resp['success']
 
                 if not success:
-                    try:
-                        blocked = rate_limiter.incr()
-                    except RateLimiterUnavailable:
-                        blocked = False
-
-                    if blocked:
-                        return render(request, 'antispam/blocked.html', status=403)
+                    username = request.POST.get('username')
+                    username = username if username else '(None)'
+                    logger.error("%s failure from [%s] for %s",
+                            auth_type,
+                            request.META.get('REMOTE_ADDR', '?'),
+                            username)
 
             return response