Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

auth: Handle user_profile.check_password raising PasswordTooWeakError.

Dinesh00e0b63c6cec
diff --git a/zerver/tests/test_auth_backends.py b/zerver/tests/test_auth_backends.py
index 0baa458fbd5..f8a573e13d7 100644
--- a/zerver/tests/test_auth_backends.py
+++ b/zerver/tests/test_auth_backends.py
@@ -342,6 +342,29 @@ class AuthBackendTest(ZulipTestCase):
)
)
+ def test_email_auth_backend_password_hasher_change(self) -> None:
+ user_profile = self.example_user("hamlet")
+ password = "a_password_of_22_chars"
+
+ with self.settings(PASSWORD_HASHERS=("django.contrib.auth.hashers.PBKDF2PasswordHasher",)):
+ user_profile.set_password(password)
+ user_profile.save()
+
+ with self.settings(
+ PASSWORD_HASHERS=(
+ "django.contrib.auth.hashers.Argon2PasswordHasher",
+ "django.contrib.auth.hashers.PBKDF2PasswordHasher",
+ ),
+ PASSWORD_MIN_LENGTH=30,
+ ), self.assertRaises(JsonableError) as m:
+ EmailAuthBackend().authenticate(
+ request=mock.MagicMock(),
+ username=self.example_email("hamlet"),
+ password=password,
+ realm=get_realm("zulip"),
+ )
+ self.assertEqual(str(m.exception), "You need to reset your password.")
+
def test_login_preview(self) -> None:
# Test preview=true displays organization login page
# instead of redirecting to app
diff --git a/zproject/backends.py b/zproject/backends.py
index 7836a79f174..ae218c61b83 100644
--- a/zproject/backends.py
+++ b/zproject/backends.py
@@ -77,6 +77,7 @@ from zerver.models import (
DisposableEmailError,
DomainNotAllowedForRealmError,
EmailContainsPlusError,
+ PasswordTooWeakError,
PreregistrationUser,
Realm,
UserProfile,
@@ -402,7 +403,15 @@ class EmailAuthBackend(ZulipAuthMixin):
user_profile = common_get_active_user(username, realm, return_data=return_data)
if user_profile is None:
return None
- if user_profile.check_password(password):
+
+ try:
+ is_password_correct = user_profile.check_password(password)
+ except PasswordTooWeakError:
+ # In some rare cases when password hasher is changed and the user has
+ # a weak password, PasswordTooWeakError will be raised.
+ raise JsonableError(_('You need to reset your password.'))
+
+ if is_password_correct:
return user_profile
return None