diff --git a/axes/admin.py b/axes/admin.py index 8ab2218..3d22657 100644 --- a/axes/admin.py +++ b/axes/admin.py @@ -1,13 +1,32 @@ from django.contrib import admin -from axes.models import AccessAttempt, AccessLog + +from axes.models import AccessLog +from axes.models import AccessAttempt class AccessAttemptAdmin(admin.ModelAdmin): - list_display = ('attempt_time', 'ip_address', 'user_agent', 'path_info', - 'failures_since_start') - list_filter = ['attempt_time', 'ip_address', 'path_info'] - search_fields = ['ip_address', 'user_agent', 'path_info'] + list_display = ( + 'attempt_time', + 'ip_address', + 'user_agent', + 'path_info', + 'failures_since_start', + ) + + list_filter = [ + 'attempt_time', + 'ip_address', + 'path_info', + ] + + search_fields = [ + 'ip_address', + 'user_agent', + 'path_info', + ] + date_hierarchy = 'attempt_time' + fieldsets = ( (None, { 'fields': ('path_info', 'failures_since_start') @@ -22,12 +41,31 @@ class AccessAttemptAdmin(admin.ModelAdmin): admin.site.register(AccessAttempt, AccessAttemptAdmin) + class AccessLogAdmin(admin.ModelAdmin): - list_display = ('attempt_time','logout_time', 'ip_address', - 'user_agent', 'path_info') - list_filter = ['attempt_time', 'logout_time', 'ip_address', 'path_info'] - search_fields = ['ip_address', 'user_agent', 'path_info'] + list_display = ( + 'attempt_time', + 'logout_time', + 'ip_address', + 'user_agent', + 'path_info', + ) + + list_filter = [ + 'attempt_time', + 'logout_time', + 'ip_address', + 'path_info', + ] + + search_fields = [ + 'ip_address', + 'user_agent', + 'path_info', + ] + date_hierarchy = 'attempt_time' + fieldsets = ( (None, { 'fields': ('path_info',) @@ -37,4 +75,4 @@ class AccessLogAdmin(admin.ModelAdmin): }) ) -admin.site.register(AccessLog, AccessLogAdmin) \ No newline at end of file +admin.site.register(AccessLog, AccessLogAdmin) diff --git a/axes/decorators.py b/axes/decorators.py index 834f13d..75b63f3 100644 --- a/axes/decorators.py +++ b/axes/decorators.py @@ -94,15 +94,15 @@ def query2str(items): def ip_in_whitelist(ip): if IP_WHITELIST is not None: return ip in IP_WHITELIST - else: - return False + + return False def ip_in_blacklist(ip): if IP_BLACKLIST is not None: return ip in IP_BLACKLIST - else: - return False + + return False log = logging.getLogger(LOGGER) @@ -110,6 +110,7 @@ if VERBOSE: log.info('AXES: BEGIN LOG') log.info('Using django-axes ' + axes.get_version()) + def is_user_lockable(request): """ Check if the user has a profile with nolockout If so, then return the value to see if this user is special @@ -133,9 +134,9 @@ def is_user_lockable(request): else: return True + def get_user_attempts(request): - """ - Returns access attempt record if it exists. + """Returns access attempt record if it exists. Otherwise return None. """ ip = get_ip(request) @@ -152,7 +153,7 @@ def get_user_attempts(request): ip_address=ip, username=username, trusted=True ) - if len(attempts) == 0: + if not attempts: params = {'ip_address': ip, 'trusted': False} if USE_USER_AGENT: params['user_agent'] = ua @@ -218,11 +219,20 @@ def watch_login(func): not response.has_header('location') and response.status_code != 302 ) - log_access_request(request, login_unsuccessful) + + access_log = AccessLog.objects.create( + user_agent=request.META.get('HTTP_USER_AGENT', ''), + ip_address=get_ip(request), + username=request.POST.get('username', None), + http_accept=request.META.get('HTTP_ACCEPT', ''), + path_info=request.META.get('PATH_INFO', ''), + trusted=not login_unsuccessful, + ) if check_request(request, login_unsuccessful): return response return lockout_response(request) + return response return decorated_login @@ -268,18 +278,6 @@ def is_already_locked(request): return False -def log_access_request(request, login_unsuccessful): - """ Log the access attempt """ - access_log = AccessLog() - access_log.user_agent = request.META.get('HTTP_USER_AGENT', '') - access_log.ip_address = get_ip(request) - access_log.username = request.POST.get('username', None) - access_log.http_accept = request.META.get('HTTP_ACCEPT', '') - access_log.path_info = request.META.get('PATH_INFO', '') - access_log.trusted = not login_unsuccessful - access_log.save() - - def check_request(request, login_unsuccessful): ip_address = get_ip(request) username = request.POST.get('username', None) diff --git a/axes/middleware.py b/axes/middleware.py index cc85c52..9edab69 100644 --- a/axes/middleware.py +++ b/axes/middleware.py @@ -1,31 +1,11 @@ -from django.contrib import admin from django.contrib.auth import views as auth_views + from axes.decorators import watch_login class FailedLoginMiddleware(object): - def __init__(self, *args, **kwargs): super(FailedLoginMiddleware, self).__init__(*args, **kwargs) - # watch the admin login page - admin.site.login = watch_login(admin.site.login) - - # and the regular auth login page - auth_views.login = watch_login(auth_views.login) - - -class FailedAdminLoginMiddleware(object): - def __init__(self, *args, **kwargs): - super(FailedAdminLoginMiddleware, self).__init__(*args, **kwargs) - - # watch the admin login page - admin.site.login = watch_login(admin.site.login) - - -class FailedAuthLoginMiddleware(object): - def __init__(self, *args, **kwargs): - super(FailedAuthLoginMiddleware, self).__init__(*args, **kwargs) - - # watch the admin login page + # watch the auth login auth_views.login = watch_login(auth_views.login)