Răsfoiți Sursa

[Django_axes_warning] adding axes beckend and middleware

ayush.goyal 4 ani în urmă
părinte
comite
85e0a16179

+ 1 - 1
desktop/core/src/desktop/auth/views.py

@@ -137,7 +137,7 @@ def dt_login(request, from_modal=False):
     first_user = first_user_form and first_user_form.is_valid()
 
     if first_user or not is_first_login_ever:
-      auth_form = AuthenticationForm(data=request.POST)
+      auth_form = AuthenticationForm(request=request, data=request.POST)
 
       if auth_form.is_valid():
         # Must login by using the AuthenticationForm. It provides 'backend' on the User object.

+ 9 - 2
desktop/core/src/desktop/lib/django_test_util.py

@@ -18,6 +18,7 @@
 import logging
 import re
 import json
+import sys
 
 import django.test.client
 import nose.tools
@@ -26,6 +27,11 @@ from useradmin.models import User, Group, Organization
 
 from desktop.conf import ENABLE_ORGANIZATIONS
 
+if sys.version_info[0] > 2:
+  from unittest.mock import Mock
+else:
+  from mock import Mock
+
 
 class Client(django.test.client.Client):
   """
@@ -46,13 +52,14 @@ def assert_ok_response(response):
   return response
 
 
-def make_logged_in_client(username="test", password="test", is_superuser=True, recreate=False, groupname=None, is_admin=False):
+def make_logged_in_client(username="test", password="test", is_superuser=True, recreate=False, groupname=None, is_admin=False, request=None):
   """
   Create a client with a user already logged in.
 
   Sometimes we recreate the user, because some tests like to mess with is_active and such.
   Note: could be combined with backend.create_user and other standart utils.
   """
+  request = Mock()
   try:
     user = User.objects.get(username=username)
     if recreate:
@@ -83,7 +90,7 @@ def make_logged_in_client(username="test", password="test", is_superuser=True, r
       user.save()
 
   c = Client()
-  ret = c.login(username=username, password=password)
+  ret = c.login(username=username, password=password, request=request)
 
   assert ret, "Login failed (user '%s')." % username
   return c

+ 7 - 1
desktop/core/src/desktop/require_login_test.py

@@ -20,11 +20,17 @@
 # This test uses "nose"-style testing (no need for a TestCase),
 # and nose-style assertions.
 
+import sys
 from nose.tools import *
 
 from django.test.client import Client
 import django
 
+if sys.version_info[0] > 2:
+  from unittest.mock import Mock
+else:
+  from mock import Mock
+
 
 def test_require_login():
   c = Client()
@@ -35,7 +41,7 @@ def test_require_login():
   assert_equal("/hue/accounts/login?next=/profile", response["Location"])
 
   # AllowAllBackend should let us in.
-  c.login(username="test", password="test")
+  c.login(request=Mock(), username="test", password="test")
   # And now we shouldn't need to be redirected.
   response = c.get('/', follow=True)
   assert_equal(200, response.status_code)

+ 7 - 0
desktop/core/src/desktop/settings.py

@@ -523,6 +523,10 @@ if desktop.conf.DEMO_ENABLED.get():
 else:
   AUTHENTICATION_BACKENDS = tuple(desktop.conf.AUTH.BACKEND.get())
 
+# AxesBackend should be the first backend in the AUTHENTICATION_BACKENDS list.
+if sys.version_info[0] > 2:
+  AUTHENTICATION_BACKENDS = ('axes.backends.AxesBackend',) + AUTHENTICATION_BACKENDS
+
 EMAIL_HOST = desktop.conf.SMTP.HOST.get()
 EMAIL_PORT = desktop.conf.SMTP.PORT.get()
 EMAIL_HOST_USER = desktop.conf.SMTP.USER.get()
@@ -836,6 +840,9 @@ MODULES_TO_PATCH = (
     'django.utils.cache',
 )
 
+if sys.version_info[0] > 2:
+  MIDDLEWARE.append('axes.middleware.AxesMiddleware')  # AxesMiddleware should be the last middleware in the MIDDLEWARE list.
+
 try:
   import hashlib
   hashlib.md5()