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

Implement MSC3069: Guest support on whoami #9655

Merged
merged 13 commits into from
Sep 29, 2021
1 change: 1 addition & 0 deletions changelog.d/9655.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add [MSC3069](https://github.com/matrix-org/matrix-doc/pull/3069) support to `/account/whoami`.
8 changes: 6 additions & 2 deletions synapse/rest/client/v2_alpha/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -883,9 +883,13 @@ 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):
Expand Down
42 changes: 41 additions & 1 deletion tests/rest/client/v2_alpha/test_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,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):
Expand Down Expand Up @@ -459,6 +459,46 @@ 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)
return channel.json_body


class ThreepidEmailRestTestCase(unittest.HomeserverTestCase):

servlets = [
Expand Down