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 get_api_key_fetch_authenticate_failure.

Mateusz Mandera75b44337a810
diff --git a/zerver/views/auth.py b/zerver/views/auth.py
index 4b701cabfbc..ccbe79f5706 100644
--- a/zerver/views/auth.py
+++ b/zerver/views/auth.py
@@ -892,19 +892,8 @@ def start_two_factor_auth(
def process_api_key_fetch_authenticate_result(
- request: HttpRequest, user_profile: Optional[UserProfile], return_data: Dict[str, bool]
+ request: HttpRequest, user_profile: UserProfile, return_data: Dict[str, bool]
) -> str:
- if return_data.get("inactive_user"):
- raise UserDeactivatedError()
- if return_data.get("inactive_realm"):
- raise RealmDeactivatedError()
- if return_data.get("password_auth_disabled"):
- raise PasswordAuthDisabledError()
- if return_data.get("password_reset_needed"):
- raise PasswordResetRequiredError()
- if user_profile is None:
- raise AuthenticationFailedError()
-
assert user_profile.is_authenticated
# Maybe sending 'user_logged_in' signal is the better approach:
@@ -923,6 +912,19 @@ def process_api_key_fetch_authenticate_result(
return api_key
+def get_api_key_fetch_authenticate_failure(return_data: Dict[str, bool]) -> JsonableError:
+ if return_data.get("inactive_user"):
+ return UserDeactivatedError()
+ if return_data.get("inactive_realm"):
+ return RealmDeactivatedError()
+ if return_data.get("password_auth_disabled"):
+ return PasswordAuthDisabledError()
+ if return_data.get("password_reset_needed"):
+ return PasswordResetRequiredError()
+
+ return AuthenticationFailedError()
+
+
@csrf_exempt
@require_post
@has_request_variables
@@ -942,11 +944,12 @@ def api_fetch_api_key(
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)
+ if user_profile is None:
+ raise get_api_key_fetch_authenticate_failure(return_data)
+
+ 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})