Browse Source

Don't throw exception if misago auth backend is called during social auth

Rafał Pitoń 7 years ago
parent
commit
ebdc383b67
2 changed files with 36 additions and 0 deletions
  1. 5 0
      misago/users/authbackends.py
  2. 31 0
      misago/users/tests/test_auth_backend.py

+ 5 - 0
misago/users/authbackends.py

@@ -10,6 +10,11 @@ class MisagoBackend(ModelBackend):
         if kwargs.get('email'):
             username = kwargs['email']  # Bias to email if it was passed explictly
 
+        if not username or not password:
+            # If no username or password was given, skip rest of this auth
+            # This may happen if we are during different auth flow (eg. OAuth/JWT)
+            return None
+
         try:
             user = UserModel.objects.get_by_username_or_email(username)
         except UserModel.DoesNotExist:

+ 31 - 0
misago/users/tests/test_auth_backend.py

@@ -34,6 +34,28 @@ class MisagoBackendTests(TestCase):
 
         self.assertEqual(user, self.user)
 
+    def test_authenticate_username_and_email(self):
+        """auth authenticates with email and skips username"""
+        user = backend.authenticate(
+            None,
+            username=self.user.username,
+            password=self.password,
+            email=self.user.email
+        )
+
+        self.assertEqual(user, self.user)
+
+    def test_authenticate_wrong_username_and_email(self):
+        """auth authenticates with email and invalid username"""
+        user = backend.authenticate(
+            None,
+            username='skipped-username',
+            password=self.password,
+            email=self.user.email
+        )
+
+        self.assertEqual(user, self.user)
+
     def test_authenticate_invalid_credential(self):
         """auth handles invalid credentials"""
         user = backend.authenticate(
@@ -67,6 +89,15 @@ class MisagoBackendTests(TestCase):
 
         self.assertIsNone(user)
 
+    def test_authenticate_no_data(self):
+        """auth has no errors if no recognised credentials are provided"""
+        self.user.is_active = False
+        self.user.save()
+
+        user = backend.authenticate(None)
+
+        self.assertIsNone(user)
+
     def test_get_user_valid_pk(self):
         """auth backend returns active user for pk given"""
         self.assertEqual(backend.get_user(self.user.pk), self.user)