Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit 8cef1ab

Browse files
authored
Implement MSC3069: Guest support on whoami (#9655)
1 parent 5279b91 commit 8cef1ab

File tree

3 files changed

+51
-7
lines changed

3 files changed

+51
-7
lines changed

changelog.d/9655.feature

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add [MSC3069](https://github.com/matrix-org/matrix-doc/pull/3069) support to `/account/whoami`.

synapse/rest/client/account.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -878,9 +878,13 @@ def __init__(self, hs: "HomeServer"):
878878
self.auth = hs.get_auth()
879879

880880
async def on_GET(self, request: SynapseRequest) -> Tuple[int, JsonDict]:
881-
requester = await self.auth.get_user_by_req(request)
881+
requester = await self.auth.get_user_by_req(request, allow_guest=True)
882882

883-
response = {"user_id": requester.user.to_string()}
883+
response = {
884+
"user_id": requester.user.to_string(),
885+
# MSC: https://github.com/matrix-org/matrix-doc/pull/3069
886+
"org.matrix.msc3069.is_guest": bool(requester.is_guest),
887+
}
884888

885889
# Appservices and similar accounts do not have device IDs
886890
# that we can report on, so exclude them for compliance.

tests/rest/client/test_account.py

+44-5
Original file line numberDiff line numberDiff line change
@@ -470,13 +470,45 @@ class WhoamiTestCase(unittest.HomeserverTestCase):
470470
register.register_servlets,
471471
]
472472

473+
def default_config(self):
474+
config = super().default_config()
475+
config["allow_guest_access"] = True
476+
return config
477+
473478
def test_GET_whoami(self):
474479
device_id = "wouldgohere"
475480
user_id = self.register_user("kermit", "test")
476481
tok = self.login("kermit", "test", device_id=device_id)
477482

478-
whoami = self.whoami(tok)
479-
self.assertEqual(whoami, {"user_id": user_id, "device_id": device_id})
483+
whoami = self._whoami(tok)
484+
self.assertEqual(
485+
whoami,
486+
{
487+
"user_id": user_id,
488+
"device_id": device_id,
489+
# Unstable until MSC3069 enters spec
490+
"org.matrix.msc3069.is_guest": False,
491+
},
492+
)
493+
494+
def test_GET_whoami_guests(self):
495+
channel = self.make_request(
496+
b"POST", b"/_matrix/client/r0/register?kind=guest", b"{}"
497+
)
498+
tok = channel.json_body["access_token"]
499+
user_id = channel.json_body["user_id"]
500+
device_id = channel.json_body["device_id"]
501+
502+
whoami = self._whoami(tok)
503+
self.assertEqual(
504+
whoami,
505+
{
506+
"user_id": user_id,
507+
"device_id": device_id,
508+
# Unstable until MSC3069 enters spec
509+
"org.matrix.msc3069.is_guest": True,
510+
},
511+
)
480512

481513
def test_GET_whoami_appservices(self):
482514
user_id = "@as:test"
@@ -491,11 +523,18 @@ def test_GET_whoami_appservices(self):
491523
)
492524
self.hs.get_datastore().services_cache.append(appservice)
493525

494-
whoami = self.whoami(as_token)
495-
self.assertEqual(whoami, {"user_id": user_id})
526+
whoami = self._whoami(as_token)
527+
self.assertEqual(
528+
whoami,
529+
{
530+
"user_id": user_id,
531+
# Unstable until MSC3069 enters spec
532+
"org.matrix.msc3069.is_guest": False,
533+
},
534+
)
496535
self.assertFalse(hasattr(whoami, "device_id"))
497536

498-
def whoami(self, tok):
537+
def _whoami(self, tok):
499538
channel = self.make_request("GET", "account/whoami", {}, access_token=tok)
500539
self.assertEqual(channel.code, 200)
501540
return channel.json_body

0 commit comments

Comments
 (0)