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.

mobile: Add support for alternative mobile URI.

Tim April8e2a79095d29
diff --git a/zerver/tests/test_auth_backends.py b/zerver/tests/test_auth_backends.py
index 8494cbe49d9..e24183f7ea5 100644
--- a/zerver/tests/test_auth_backends.py
+++ b/zerver/tests/test_auth_backends.py
@@ -2797,6 +2797,27 @@ class GoogleAuthBackendTest(SocialAuthBase):
"warning",
)])
+ def test_social_auth_mobile_realm_uri(self) -> None:
+ mobile_flow_otp = '1234abcd' * 8
+ account_data_dict = self.get_account_data_dict(email=self.email, name='Full Name')
+
+ with self.settings(REALM_MOBILE_REMAP_URIS={'http://zulip.testserver': 'http://zulip-mobile.testserver'}):
+ result = self.social_auth_test(account_data_dict, subdomain='zulip',
+ expect_choose_email_screen=True,
+ alternative_start_url="/accounts/login/google/",
+ mobile_flow_otp=mobile_flow_otp)
+
+ self.assertEqual(result.status_code, 302)
+ redirect_url = result['Location']
+ parsed_url = urllib.parse.urlparse(redirect_url)
+ query_params = urllib.parse.parse_qs(parsed_url.query)
+ self.assertEqual(parsed_url.scheme, 'zulip')
+ self.assertEqual(query_params["realm"], ['http://zulip-mobile.testserver'])
+ self.assertEqual(query_params["email"], [self.example_email("hamlet")])
+ encrypted_api_key = query_params["otp_encrypted_api_key"][0]
+ hamlet_api_keys = get_all_api_keys(self.example_user('hamlet'))
+ self.assertIn(otp_decrypt_api_key(encrypted_api_key, mobile_flow_otp), hamlet_api_keys)
+
def test_social_auth_mobile_success_legacy_url(self) -> None:
mobile_flow_otp = '1234abcd' * 8
account_data_dict = self.get_account_data_dict(email=self.email, name='Full Name')
diff --git a/zerver/views/auth.py b/zerver/views/auth.py
index ba47660966d..3081a14d994 100644
--- a/zerver/views/auth.py
+++ b/zerver/views/auth.py
@@ -321,10 +321,18 @@ def finish_mobile_flow(request: HttpRequest, user_profile: UserProfile, otp: str
def create_response_for_otp_flow(key: str, otp: str, user_profile: UserProfile,
encrypted_key_field_name: str) -> HttpResponse:
+ realm_uri = user_profile.realm.uri
+
+ # Check if the mobile URI is overridden in settings, if so, replace it
+ # This block should only apply to the mobile flow, so we if add others, this
+ # needs to be conditional.
+ if realm_uri in settings.REALM_MOBILE_REMAP_URIS:
+ realm_uri = settings.REALM_MOBILE_REMAP_URIS[realm_uri]
+
params = {
encrypted_key_field_name: otp_encrypt_api_key(key, otp),
'email': user_profile.delivery_email,
- 'realm': user_profile.realm.uri,
+ 'realm': realm_uri,
}
# We can't use HttpResponseRedirect, since it only allows HTTP(S) URLs
response = HttpResponse(status=302)
diff --git a/zproject/default_settings.py b/zproject/default_settings.py
index b5f956cadf6..a2df5643543 100644
--- a/zproject/default_settings.py
+++ b/zproject/default_settings.py
@@ -241,6 +241,12 @@ SYSTEM_ONLY_REALMS = {"zulip"}
# The values will also be added to ALLOWED_HOSTS.
REALM_HOSTS: Dict[str, str] = {}
+# Map used to rewrite the URIs for certain realms during mobile
+# authentication. This, combined with adding the relevant hosts to
+# ALLOWED_HOSTS, can be used for environments where security policies
+# mean that a different hostname must be used for mobile access.
+REALM_MOBILE_REMAP_URIS: Dict[str, str] = {}
+
# Whether the server is using the Pgroonga full-text search
# backend. Plan is to turn this on for everyone after further
# testing.