From 0abbb871558e56c1ed282e657844abfb562028fc Mon Sep 17 00:00:00 2001 From: Mateusz Mandera Date: Tue, 16 Nov 2021 16:04:04 +0100 Subject: [PATCH] auth: Include user_id in the params returned at the end of mobile flow. The user id is a very useful piece of information that the mobile client should have access to - instead of only getting the email. This makes it much simpler to impleent clients that might be robust to changes in email address. --- templates/zerver/api/changelog.md | 6 ++++++ version.py | 2 +- zerver/tests/test_auth_backends.py | 9 +++++++-- zerver/views/auth.py | 1 + 4 files changed, 15 insertions(+), 3 deletions(-) diff --git a/templates/zerver/api/changelog.md b/templates/zerver/api/changelog.md index 536340f135..5df9588c29 100644 --- a/templates/zerver/api/changelog.md +++ b/templates/zerver/api/changelog.md @@ -20,6 +20,12 @@ format used by the Zulip server that they are interacting with. ## Changes in Zulip 5.0 +**Feature level 108** + +* In the mobile application authentication flow, the authenticated + user's `user_id` is now included in the parameters encoded in the + final `zulip://` redirect URL. + **Feature level 107** * [`POST /register`](/api/register-queue), [`PATCH /settings`](/api/update-settings), [`PATCH /realm/user_settings_defaults`](/api/update-realm-user-settings-defaults): diff --git a/version.py b/version.py index 57ec384a5c..dcef96954f 100644 --- a/version.py +++ b/version.py @@ -33,7 +33,7 @@ DESKTOP_WARNING_VERSION = "5.4.3" # Changes should be accompanied by documentation explaining what the # new level means in templates/zerver/api/changelog.md, as well as # "**Changes**" entries in the endpoint's documentation in `zulip.yaml`. -API_FEATURE_LEVEL = 107 +API_FEATURE_LEVEL = 108 # Bump the minor PROVISION_VERSION to indicate that folks should provision # only when going from an old version of the code to a newer version. Bump diff --git a/zerver/tests/test_auth_backends.py b/zerver/tests/test_auth_backends.py index 107c8c392c..7370ca9c5f 100644 --- a/zerver/tests/test_auth_backends.py +++ b/zerver/tests/test_auth_backends.py @@ -1200,7 +1200,10 @@ class SocialAuthBase(DesktopFlowTestingLib, ZulipTestCase): def test_social_auth_mobile_success(self) -> None: mobile_flow_otp = "1234abcd" * 8 - account_data_dict = self.get_account_data_dict(email=self.email, name="Full Name") + hamlet = self.example_user("hamlet") + account_data_dict = self.get_account_data_dict( + email=hamlet.delivery_email, name="Full Name" + ) self.assert_length(mail.outbox, 0) self.user_profile.date_joined = timezone_now() - datetime.timedelta( seconds=JUST_CREATED_THRESHOLD + 1 @@ -1231,7 +1234,9 @@ class SocialAuthBase(DesktopFlowTestingLib, ZulipTestCase): query_params = urllib.parse.parse_qs(parsed_url.query) self.assertEqual(parsed_url.scheme, "zulip") self.assertEqual(query_params["realm"], ["http://zulip.testserver"]) - self.assertEqual(query_params["email"], [self.example_email("hamlet")]) + self.assertEqual(query_params["email"], [hamlet.delivery_email]) + self.assertEqual(query_params["user_id"], [str(hamlet.id)]) + 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) diff --git a/zerver/views/auth.py b/zerver/views/auth.py index 6a52e865c2..853237d045 100644 --- a/zerver/views/auth.py +++ b/zerver/views/auth.py @@ -382,6 +382,7 @@ def create_response_for_otp_flow( params = { encrypted_key_field_name: otp_encrypt_api_key(key, otp), "email": user_profile.delivery_email, + "user_id": user_profile.id, "realm": realm_uri, } # We can't use HttpResponseRedirect, since it only allows HTTP(S) URLs