Repository: zulip/zulip

Issue:
Include user ID in fetch-api-key response
We should add one additional property to the response of the fetch-api-key endpoint (https://chat.zulip.org/api/fetch-api-key): the user's user ID.

See [chat thread](https://chat.zulip.org/#narrow/stream/378-api-design/topic/user.20ID.20in.20fetch-api-key/near/1540592) for motivation and discussion.



Discussion:
Hello @zulip/server-api, @zulip/server-authentication members, this issue was labeled with the "area: authentication", "area: api" labels, so you may want to check it out!

<!-- areaLabelAddition -->


Resolving patch:
diff --git a/version.py b/version.py
--- a/version.py
+++ b/version.py
@@ -33,7 +33,7 @@
 # Changes should be accompanied by documentation explaining what the
 # new level means in api_docs/changelog.md, as well as "**Changes**"
 # entries in the endpoint's documentation in `zulip.yaml`.
-API_FEATURE_LEVEL = 170
+API_FEATURE_LEVEL = 171
 
 # 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/views/auth.py b/zerver/views/auth.py
--- a/zerver/views/auth.py
+++ b/zerver/views/auth.py
@@ -1000,7 +1000,10 @@ def api_fetch_api_key(
 
     api_key = process_api_key_fetch_authenticate_result(request, user_profile)
 
-    return json_success(request, data={"api_key": api_key, "email": user_profile.delivery_email})
+    return json_success(
+        request,
+        data={"api_key": api_key, "email": user_profile.delivery_email, "user_id": user_profile.id},
+    )
 
 
 def get_auth_backends_data(request: HttpRequest) -> Dict[str, Any]:
diff --git a/zerver/views/development/dev_login.py b/zerver/views/development/dev_login.py
--- a/zerver/views/development/dev_login.py
+++ b/zerver/views/development/dev_login.py
@@ -135,7 +135,10 @@ def api_dev_fetch_api_key(request: HttpRequest, username: str = REQ()) -> HttpRe
 
     do_login(request, user_profile)
     api_key = get_api_key(user_profile)
-    return json_success(request, data={"api_key": api_key, "email": user_profile.delivery_email})
+    return json_success(
+        request,
+        data={"api_key": api_key, "email": user_profile.delivery_email, "user_id": user_profile.id},
+    )
 
 
 @csrf_exempt


Test patch:
diff --git a/zerver/tests/test_auth_backends.py b/zerver/tests/test_auth_backends.py
--- a/zerver/tests/test_auth_backends.py
+++ b/zerver/tests/test_auth_backends.py
@@ -4460,7 +4460,8 @@ def test_success(self) -> None:
             "/api/v1/fetch_api_key",
             dict(username=self.email, password=initial_password(self.email)),
         )
-        self.assert_json_success(result)
+        json_response = self.assert_json_success(result)
+        self.assertEqual(json_response["user_id"], self.user_profile.id)
 
     def test_invalid_email(self) -> None:
         result = self.client_post(
@@ -4500,7 +4501,8 @@ def test_ldap_auth_email_auth_disabled_success(self) -> None:
                 "/api/v1/fetch_api_key",
                 dict(username=self.example_email("hamlet"), password=self.ldap_password("hamlet")),
             )
-        self.assert_json_success(result)
+        json_response = self.assert_json_success(result)
+        self.assertEqual(json_response["user_id"], self.user_profile.id)
 
     @override_settings(
         AUTHENTICATION_BACKENDS=("zproject.backends.ZulipLDAPAuthBackend",),
@@ -4530,7 +4532,8 @@ def test_ldap_auth_email_auth_organization_restriction(self) -> None:
             "/api/v1/fetch_api_key",
             dict(username="hamlet", password=self.ldap_password("hamlet")),
         )
-        self.assert_json_success(result)
+        json_response = self.assert_json_success(result)
+        self.assertEqual(json_response["user_id"], self.user_profile.id)
 
     @override_settings(
         AUTHENTICATION_BACKENDS=("zproject.backends.ZulipLDAPAuthBackend",),
@@ -4663,6 +4666,7 @@ def test_success(self) -> None:
         result = self.client_post("/api/v1/dev_fetch_api_key", dict(username=self.email))
         data = self.assert_json_success(result)
         self.assertEqual(data["email"], self.email)
+        self.assertEqual(data["user_id"], self.user_profile.id)
         user_api_keys = get_all_api_keys(self.user_profile)
         self.assertIn(data["api_key"], user_api_keys)