From 36d92cdefff08da6a4f14eba4b320252ccb7f217 Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Fri, 19 Mar 2021 12:17:33 -0600 Subject: [PATCH 01/11] Enable MSC3069: Guest support on whoami --- synapse/rest/client/v2_alpha/account.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/synapse/rest/client/v2_alpha/account.py b/synapse/rest/client/v2_alpha/account.py index adf1d397282f..95886dfb810e 100644 --- a/synapse/rest/client/v2_alpha/account.py +++ b/synapse/rest/client/v2_alpha/account.py @@ -879,9 +879,14 @@ def __init__(self, hs): self.auth = hs.get_auth() async def on_GET(self, request): - requester = await self.auth.get_user_by_req(request) + requester = await self.auth.get_user_by_req(request, allow_guest=True) + + return 200, { + "user_id": requester.user.to_string(), - return 200, {"user_id": requester.user.to_string()} + # MSC: https://github.com/matrix-org/matrix-doc/pull/3069 + "org.matrix.msc3069.is_guest": bool(requester.is_guest), + } def register_servlets(hs, http_server): From 677351d54048b6f6a6d8c14a40c94e9aaf16cd62 Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Fri, 19 Mar 2021 12:19:28 -0600 Subject: [PATCH 02/11] changelog --- changelog.d/9655.feature | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/9655.feature diff --git a/changelog.d/9655.feature b/changelog.d/9655.feature new file mode 100644 index 000000000000..70cac230d848 --- /dev/null +++ b/changelog.d/9655.feature @@ -0,0 +1 @@ +Add [MSC3069](https://github.com/matrix-org/matrix-doc/pull/3069) support to `/account/whoami`. \ No newline at end of file From b87eacfd7faa9bdefccf9a584228b3171fb009a9 Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Fri, 19 Mar 2021 12:22:47 -0600 Subject: [PATCH 03/11] Appease the linter --- synapse/rest/client/v2_alpha/account.py | 1 - 1 file changed, 1 deletion(-) diff --git a/synapse/rest/client/v2_alpha/account.py b/synapse/rest/client/v2_alpha/account.py index 95886dfb810e..79fa7dd09a45 100644 --- a/synapse/rest/client/v2_alpha/account.py +++ b/synapse/rest/client/v2_alpha/account.py @@ -883,7 +883,6 @@ async def on_GET(self, request): return 200, { "user_id": requester.user.to_string(), - # MSC: https://github.com/matrix-org/matrix-doc/pull/3069 "org.matrix.msc3069.is_guest": bool(requester.is_guest), } From ad9dae9f75df62dac7e21bae09b095cfd5785a80 Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Fri, 2 Apr 2021 19:47:01 -0600 Subject: [PATCH 04/11] Add whoami tests --- tests/rest/client/v2_alpha/test_account.py | 27 ++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/tests/rest/client/v2_alpha/test_account.py b/tests/rest/client/v2_alpha/test_account.py index e72b61963d11..ff825c106b2e 100644 --- a/tests/rest/client/v2_alpha/test_account.py +++ b/tests/rest/client/v2_alpha/test_account.py @@ -386,6 +386,26 @@ def make_homeserver(self, reactor, clock): self.hs = self.setup_test_homeserver() return self.hs + def test_GET_whoami(self): + user_id = self.register_user("kermit", "test") + tok = self.login("kermit", "test") + + whoami = self.whoami(tok) + self.assertObjectHasAttributes({"user_id": user_id}, whoami) + + @override_config({"allow_guest_access": True}) + def test_GET_whoami_guests(self): + channel = self.make_request(b"POST", self.url + b"?kind=guest", b"{}") + tok = channel.json_body["access_token"] + user_id = channel.json_body["user_id"] + + whoami = self.whoami(tok) + self.assertObjectHasAttributes({ + "user_id": user_id, + # Unstable until MSC3069 enters spec + "org.matrix.msc3069.is_guest": True, + }, whoami) + def test_deactivate_account(self): user_id = self.register_user("kermit", "test") tok = self.login("kermit", "test") @@ -458,6 +478,13 @@ def deactivate(self, user_id, tok): ) self.assertEqual(channel.code, 200) + def whoami(self, tok): + channel = self.make_request( + "GET", "account/whoami", {}, access_token=tok + ) + self.assertEqual(channel.code, 200) + return channel.json_body + class ThreepidEmailRestTestCase(unittest.HomeserverTestCase): From 734e59b5979d75de0fcf5191c6bf039ebc7d6bf1 Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Fri, 2 Apr 2021 19:47:09 -0600 Subject: [PATCH 05/11] Fix whoami test for deactivation --- tests/rest/client/v2_alpha/test_account.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/rest/client/v2_alpha/test_account.py b/tests/rest/client/v2_alpha/test_account.py index ff825c106b2e..979118154ba4 100644 --- a/tests/rest/client/v2_alpha/test_account.py +++ b/tests/rest/client/v2_alpha/test_account.py @@ -418,7 +418,7 @@ def test_deactivate_account(self): self.assertTrue(self.get_success(store.get_user_deactivated_status(user_id))) # Check that this access token has been invalidated. - channel = self.make_request("GET", "account/whoami") + channel = self.make_request("GET", "account/whoami", access_token=tok) self.assertEqual(channel.code, 401) def test_pending_invites(self): From 210ad56d3be5cf073f86b06598a884d7b4d6d1dc Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Fri, 2 Apr 2021 19:51:38 -0600 Subject: [PATCH 06/11] Appease the strict linter --- tests/rest/client/v2_alpha/test_account.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/tests/rest/client/v2_alpha/test_account.py b/tests/rest/client/v2_alpha/test_account.py index 979118154ba4..f57adecba706 100644 --- a/tests/rest/client/v2_alpha/test_account.py +++ b/tests/rest/client/v2_alpha/test_account.py @@ -400,11 +400,14 @@ def test_GET_whoami_guests(self): user_id = channel.json_body["user_id"] whoami = self.whoami(tok) - self.assertObjectHasAttributes({ - "user_id": user_id, - # Unstable until MSC3069 enters spec - "org.matrix.msc3069.is_guest": True, - }, whoami) + self.assertObjectHasAttributes( + { + "user_id": user_id, + # Unstable until MSC3069 enters spec + "org.matrix.msc3069.is_guest": True, + }, + whoami, + ) def test_deactivate_account(self): user_id = self.register_user("kermit", "test") @@ -479,9 +482,7 @@ def deactivate(self, user_id, tok): self.assertEqual(channel.code, 200) def whoami(self, tok): - channel = self.make_request( - "GET", "account/whoami", {}, access_token=tok - ) + channel = self.make_request("GET", "account/whoami", {}, access_token=tok) self.assertEqual(channel.code, 200) return channel.json_body From dec2423d6b50ed1554024a260aaf2ed28ee56f9d Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Fri, 2 Apr 2021 20:49:27 -0600 Subject: [PATCH 07/11] Fix tests? --- tests/rest/client/v2_alpha/test_account.py | 55 +++++++++++++--------- 1 file changed, 32 insertions(+), 23 deletions(-) diff --git a/tests/rest/client/v2_alpha/test_account.py b/tests/rest/client/v2_alpha/test_account.py index f57adecba706..aaa3bdc6b08d 100644 --- a/tests/rest/client/v2_alpha/test_account.py +++ b/tests/rest/client/v2_alpha/test_account.py @@ -386,29 +386,6 @@ def make_homeserver(self, reactor, clock): self.hs = self.setup_test_homeserver() return self.hs - def test_GET_whoami(self): - user_id = self.register_user("kermit", "test") - tok = self.login("kermit", "test") - - whoami = self.whoami(tok) - self.assertObjectHasAttributes({"user_id": user_id}, whoami) - - @override_config({"allow_guest_access": True}) - def test_GET_whoami_guests(self): - channel = self.make_request(b"POST", self.url + b"?kind=guest", b"{}") - tok = channel.json_body["access_token"] - user_id = channel.json_body["user_id"] - - whoami = self.whoami(tok) - self.assertObjectHasAttributes( - { - "user_id": user_id, - # Unstable until MSC3069 enters spec - "org.matrix.msc3069.is_guest": True, - }, - whoami, - ) - def test_deactivate_account(self): user_id = self.register_user("kermit", "test") tok = self.login("kermit", "test") @@ -481,6 +458,38 @@ def deactivate(self, user_id, tok): ) self.assertEqual(channel.code, 200) +class WhoamiTestCase(unittest.HomeserverTestCase): + + servlets = [ + synapse.rest.admin.register_servlets_for_client_rest_resource, + login.register_servlets, + account.register_servlets, + room.register_servlets, + ] + + def test_GET_whoami(self): + user_id = self.register_user("kermit", "test") + tok = self.login("kermit", "test") + + whoami = self.whoami(tok) + self.assertObjectHasAttributes({"user_id": user_id}, whoami) + + @override_config({"allow_guest_access": True}) + def test_GET_whoami_guests(self): + channel = self.make_request(b"POST", b"/_matrix/client/r0/register?kind=guest", b"{}") + tok = channel.json_body["access_token"] + user_id = channel.json_body["user_id"] + + whoami = self.whoami(tok) + self.assertObjectHasAttributes( + { + "user_id": user_id, + # Unstable until MSC3069 enters spec + "org.matrix.msc3069.is_guest": True, + }, + whoami, + ) + def whoami(self, tok): channel = self.make_request("GET", "account/whoami", {}, access_token=tok) self.assertEqual(channel.code, 200) From 3551d35963d1167c634975974fb9f5917067517c Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Fri, 2 Apr 2021 20:56:39 -0600 Subject: [PATCH 08/11] This linter is strict --- tests/rest/client/v2_alpha/test_account.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/rest/client/v2_alpha/test_account.py b/tests/rest/client/v2_alpha/test_account.py index aaa3bdc6b08d..22c1049a7346 100644 --- a/tests/rest/client/v2_alpha/test_account.py +++ b/tests/rest/client/v2_alpha/test_account.py @@ -458,6 +458,7 @@ def deactivate(self, user_id, tok): ) self.assertEqual(channel.code, 200) + class WhoamiTestCase(unittest.HomeserverTestCase): servlets = [ @@ -476,7 +477,9 @@ def test_GET_whoami(self): @override_config({"allow_guest_access": True}) def test_GET_whoami_guests(self): - channel = self.make_request(b"POST", b"/_matrix/client/r0/register?kind=guest", b"{}") + channel = self.make_request( + b"POST", b"/_matrix/client/r0/register?kind=guest", b"{}" + ) tok = channel.json_body["access_token"] user_id = channel.json_body["user_id"] From 549ed22eb89a817de5e501c766d0c489cd7e0316 Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Mon, 27 Sep 2021 20:12:47 -0600 Subject: [PATCH 09/11] Re-implement post merge --- synapse/rest/client/account.py | 8 ++++++-- tests/rest/client/test_account.py | 29 +++++++++++++++++++++++++---- 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/synapse/rest/client/account.py b/synapse/rest/client/account.py index 6a7608d60b97..bacb82833022 100644 --- a/synapse/rest/client/account.py +++ b/synapse/rest/client/account.py @@ -878,9 +878,13 @@ def __init__(self, hs: "HomeServer"): self.auth = hs.get_auth() async def on_GET(self, request: SynapseRequest) -> Tuple[int, JsonDict]: - requester = await self.auth.get_user_by_req(request) + requester = await self.auth.get_user_by_req(request, allow_guest=True) - response = {"user_id": requester.user.to_string()} + response = { + "user_id": requester.user.to_string(), + # MSC: https://github.com/matrix-org/matrix-doc/pull/3069 + "org.matrix.msc3069.is_guest": bool(requester.is_guest), + } # Appservices and similar accounts do not have device IDs # that we can report on, so exclude them for compliance. diff --git a/tests/rest/client/test_account.py b/tests/rest/client/test_account.py index 9e9e953cf4b2..a1c57203e984 100644 --- a/tests/rest/client/test_account.py +++ b/tests/rest/client/test_account.py @@ -475,8 +475,29 @@ def test_GET_whoami(self): user_id = self.register_user("kermit", "test") tok = self.login("kermit", "test", device_id=device_id) - whoami = self.whoami(tok) - self.assertEqual(whoami, {"user_id": user_id, "device_id": device_id}) + whoami = self._whoami(tok) + self.assertEqual(whoami, { + "user_id": user_id, + "device_id": device_id, + # Unstable until MSC3069 enters spec + "org.matrix.msc3069.is_guest": False, + }) + + def test_GET_whoami_guests(self): + channel = self.make_request( + b"POST", b"/_matrix/client/r0/register?kind=guest", b"{}" + ) + tok = channel.json_body["access_token"] + user_id = channel.json_body["user_id"] + device_id = channel.json_body["device_id"] + + whoami = self._whoami(tok) + self.assertEqual(whoami, { + "user_id": user_id, + "device_id": device_id, + # Unstable until MSC3069 enters spec + "org.matrix.msc3069.is_guest": True, + }) def test_GET_whoami_appservices(self): user_id = "@as:test" @@ -491,11 +512,11 @@ def test_GET_whoami_appservices(self): ) self.hs.get_datastore().services_cache.append(appservice) - whoami = self.whoami(as_token) + whoami = self._whoami(as_token) self.assertEqual(whoami, {"user_id": user_id}) self.assertFalse(hasattr(whoami, "device_id")) - def whoami(self, tok): + def _whoami(self, tok): channel = self.make_request("GET", "account/whoami", {}, access_token=tok) self.assertEqual(channel.code, 200) return channel.json_body From 02dc9e96eb64d520e5aa7166b7fd83428af0a578 Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Tue, 28 Sep 2021 15:28:53 -0600 Subject: [PATCH 10/11] Appease the linter --- tests/rest/client/test_account.py | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/tests/rest/client/test_account.py b/tests/rest/client/test_account.py index a1c57203e984..b2fe39f2f1be 100644 --- a/tests/rest/client/test_account.py +++ b/tests/rest/client/test_account.py @@ -476,12 +476,15 @@ def test_GET_whoami(self): tok = self.login("kermit", "test", device_id=device_id) whoami = self._whoami(tok) - self.assertEqual(whoami, { - "user_id": user_id, - "device_id": device_id, - # Unstable until MSC3069 enters spec - "org.matrix.msc3069.is_guest": False, - }) + self.assertEqual( + whoami, + { + "user_id": user_id, + "device_id": device_id, + # Unstable until MSC3069 enters spec + "org.matrix.msc3069.is_guest": False, + }, + ) def test_GET_whoami_guests(self): channel = self.make_request( @@ -492,12 +495,15 @@ def test_GET_whoami_guests(self): device_id = channel.json_body["device_id"] whoami = self._whoami(tok) - self.assertEqual(whoami, { - "user_id": user_id, - "device_id": device_id, - # Unstable until MSC3069 enters spec - "org.matrix.msc3069.is_guest": True, - }) + self.assertEqual( + whoami, + { + "user_id": user_id, + "device_id": device_id, + # Unstable until MSC3069 enters spec + "org.matrix.msc3069.is_guest": True, + }, + ) def test_GET_whoami_appservices(self): user_id = "@as:test" From 6ab943b9e6588d981b720fd7d1110c68b06ba078 Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Tue, 28 Sep 2021 15:56:02 -0600 Subject: [PATCH 11/11] Fix tests? --- tests/rest/client/test_account.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/tests/rest/client/test_account.py b/tests/rest/client/test_account.py index b2fe39f2f1be..64b0b8458b6e 100644 --- a/tests/rest/client/test_account.py +++ b/tests/rest/client/test_account.py @@ -470,6 +470,11 @@ class WhoamiTestCase(unittest.HomeserverTestCase): register.register_servlets, ] + def default_config(self): + config = super().default_config() + config["allow_guest_access"] = True + return config + def test_GET_whoami(self): device_id = "wouldgohere" user_id = self.register_user("kermit", "test") @@ -519,7 +524,14 @@ def test_GET_whoami_appservices(self): self.hs.get_datastore().services_cache.append(appservice) whoami = self._whoami(as_token) - self.assertEqual(whoami, {"user_id": user_id}) + self.assertEqual( + whoami, + { + "user_id": user_id, + # Unstable until MSC3069 enters spec + "org.matrix.msc3069.is_guest": False, + }, + ) self.assertFalse(hasattr(whoami, "device_id")) def _whoami(self, tok):