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.

registration: Don't require LDAP auth for externally-verified users.

Alex Vandiver0903ea766f65
diff --git a/zerver/tests/test_auth_backends.py b/zerver/tests/test_auth_backends.py
index f1d40c9f77a..4f31fe8100b 100644
--- a/zerver/tests/test_auth_backends.py
+++ b/zerver/tests/test_auth_backends.py
@@ -1920,6 +1920,54 @@ class SocialAuthBase(DesktopFlowTestingLib, ZulipTestCase, ABC):
],
)
+ @override_settings(TERMS_OF_SERVICE_VERSION=None)
+ def test_social_auth_with_ldap_auth_registration_user_in_ldap(self) -> None:
+ """
+ This test checks that in configurations that use the LDAP authentication
+ backend and a social backend, a user who exists in the LDAP directory
+ can create an account via the social backend, with their identity
+ having been verified by the social backend.
+ """
+ self.init_default_ldap_database()
+ email = "[email protected]"
+ name = "Social Fullname"
+ # The name of this user in the LDAP directory, which is expected
+ # to take precedence over the name provided by the social backend.
+ ldap_name = "New LDAP fullname"
+ realm = get_realm("zulip")
+ subdomain = "zulip"
+ ldap_user_attr_map = {"full_name": "cn"}
+
+ backend_path = f"zproject.backends.{self.BACKEND_CLASS.__name__}"
+ with self.settings(
+ POPULATE_PROFILE_VIA_LDAP=True,
+ LDAP_EMAIL_ATTR="mail",
+ AUTH_LDAP_USER_ATTR_MAP=ldap_user_attr_map,
+ AUTHENTICATION_BACKENDS=(
+ backend_path,
+ "zproject.backends.ZulipLDAPAuthBackend",
+ "zproject.backends.ZulipDummyBackend",
+ ),
+ ):
+ account_data_dict = self.get_account_data_dict(email=email, name=name)
+ result = self.social_auth_test(
+ account_data_dict,
+ expect_choose_email_screen=True,
+ subdomain=subdomain,
+ is_signup=True,
+ )
+ # The full name is populated from LDAP, so the registration form
+ # is skipped and the account is created directly.
+ self.stage_two_of_registration(
+ result,
+ realm,
+ subdomain,
+ email,
+ name,
+ ldap_name,
+ skip_registration_form=True,
+ )
+
def test_social_auth_complete(self) -> None:
def mock_process_error(backend: BaseOAuth2, data: Mapping[str, object]) -> None:
raise AuthFailed(backend, "Not found")
diff --git a/zerver/views/registration.py b/zerver/views/registration.py
index f9e61014150..11f3c92ee30 100644
--- a/zerver/views/registration.py
+++ b/zerver/views/registration.py
@@ -743,49 +743,46 @@ def registration_helper(
# with LDAP and we need to carefully decide whether they should be permitted to proceed
# with account creation anyway or be stopped. There are three scenarios to consider:
#
- # 1. EmailAuthBackend is enabled for the realm. That explicitly means that a user
+ # 1. The user came here through one of the ExternalAuthMethods. Their identity has
+ # already been verified by the external authentication method, so LDAP password
+ # verification is not applicable and account creation can proceed.
+ # 2. EmailAuthBackend is enabled for the realm. That explicitly means that a user
# with a valid confirmation link should be able to create an account, because
# they were invited or organization permissions allowed sign up.
- # 2. EmailAuthBackend is disabled - that means the organization wants to be authenticating
- # users with an external source (LDAP or one of the ExternalAuthMethods). If the user
- # came here through one of the ExternalAuthMethods, their identity can be considered
- # verified and account creation can proceed.
# 3. EmailAuthBackend is disabled and the user did not come here through an ExternalAuthMethod.
# That means they came here by entering their email address on the registration page
# and clicking the confirmation link received. That means their identity needs to be
# verified with LDAP - and that has just failed above. Thus the account should NOT be
# created.
#
- if email_auth_enabled(realm):
- can_use_different_backend = True
# We can identify the user came here through an ExternalAuthMethod by password_required
# being set to False on the PreregistrationUser object.
- elif len(get_external_method_dicts(realm)) > 0 and not password_required:
- can_use_different_backend = True
- else:
- can_use_different_backend = False
-
- if settings.LDAP_APPEND_DOMAIN:
- # In LDAP_APPEND_DOMAIN configurations, we don't allow making a non-LDAP account
- # if the email matches the ldap domain.
- can_use_different_backend = can_use_different_backend and (
- not email_belongs_to_ldap(realm, email)
- )
- if return_data.get("no_matching_ldap_user") and can_use_different_backend:
- # If both the LDAP and Email or Social auth backends are
- # enabled, and there's no matching user in the LDAP
- # directory then the intent is to create a user in the
- # realm with their email outside the LDAP organization
- # (with e.g. a password stored in the Zulip database,
- # not LDAP). So we fall through and create the new
- # account.
+ if len(get_external_method_dicts(realm)) > 0 and not password_required:
+ # Scenario 1: fall through and create the new account.
pass
else:
- # TODO: This probably isn't going to give a
- # user-friendly error message, but it doesn't
- # particularly matter, because the registration form
- # is hidden for most users.
- return HttpResponseRedirect(reverse("login", query={"email": email}))
+ can_use_different_backend = email_auth_enabled(realm)
+ if settings.LDAP_APPEND_DOMAIN:
+ # In LDAP_APPEND_DOMAIN configurations, we don't allow making a non-LDAP account
+ # if the email matches the ldap domain.
+ can_use_different_backend = can_use_different_backend and (
+ not email_belongs_to_ldap(realm, email)
+ )
+ if return_data.get("no_matching_ldap_user") and can_use_different_backend:
+ # Scenario 2: If both the LDAP and Email auth backends are
+ # enabled, and there's no matching user in the LDAP
+ # directory then the intent is to create a user in the
+ # realm with their email outside the LDAP organization
+ # (with e.g. a password stored in the Zulip database,
+ # not LDAP). So we fall through and create the new
+ # account.
+ pass
+ else:
+ # TODO: This probably isn't going to give a
+ # user-friendly error message, but it doesn't
+ # particularly matter, because the registration form
+ # is hidden for most users.
+ return HttpResponseRedirect(reverse("login", query={"email": email}))
else:
assert isinstance(user, UserProfile)
user_profile = user