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: Extract process_api_key_fetch_authenticate_result function.

Mateusz Mandera3a1d974cee1a
diff --git a/zerver/views/auth.py b/zerver/views/auth.py
index 390fdf3dd3c..4b701cabfbc 100644
--- a/zerver/views/auth.py
+++ b/zerver/views/auth.py
@@ -891,25 +891,9 @@ def start_two_factor_auth(
return two_fa_view(request, **kwargs)
-@csrf_exempt
-@require_post
-@has_request_variables
-def api_fetch_api_key(
- request: HttpRequest, username: str = REQ(), password: str = REQ()
-) -> HttpResponse:
- return_data: Dict[str, bool] = {}
-
- realm = get_realm_from_request(request)
- if realm is None:
- raise InvalidSubdomainError()
-
- if not ldap_auth_enabled(realm=realm):
- # In case we don't authenticate against LDAP, check for a valid
- # email. LDAP backend can authenticate against a non-email.
- validate_login_email(username)
- user_profile = authenticate(
- request=request, username=username, password=password, realm=realm, return_data=return_data
- )
+def process_api_key_fetch_authenticate_result(
+ request: HttpRequest, user_profile: Optional[UserProfile], return_data: Dict[str, bool]
+) -> str:
if return_data.get("inactive_user"):
raise UserDeactivatedError()
if return_data.get("inactive_realm"):
@@ -936,6 +920,34 @@ def api_fetch_api_key(
RequestNotes.get_notes(request).requestor_for_logs = user_profile.format_requestor_for_logs()
api_key = get_api_key(user_profile)
+ return api_key
+
+
+@csrf_exempt
+@require_post
+@has_request_variables
+def api_fetch_api_key(
+ request: HttpRequest, username: str = REQ(), password: str = REQ()
+) -> HttpResponse:
+ return_data: Dict[str, bool] = {}
+
+ realm = get_realm_from_request(request)
+ if realm is None:
+ raise InvalidSubdomainError()
+
+ if not ldap_auth_enabled(realm=realm):
+ # In case we don't authenticate against LDAP, check for a valid
+ # email. LDAP backend can authenticate against a non-email.
+ validate_login_email(username)
+ user_profile = authenticate(
+ request=request, username=username, password=password, realm=realm, return_data=return_data
+ )
+ if user_profile is not None:
+ assert isinstance(user_profile, UserProfile)
+
+ api_key = process_api_key_fetch_authenticate_result(request, user_profile, return_data)
+ assert user_profile is not None
+
return json_success(request, data={"api_key": api_key, "email": user_profile.delivery_email})