Skip to content
This repository has been 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/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Comment on lines +885 to +886
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For future reference: I'd have preferred this to be behind an opt-in config flag.

(see https://matrix-org.github.io/synapse/latest/development/experimental_features.html)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not really a feature, but more of a very tedious spec bug fix.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nevertheless, I'd prefer Synapse not to expose the unstable-named flag without people explicitly opting into it.

}

# Appservices and similar accounts do not have device IDs
# that we can report on, so exclude them for compliance.
Expand Down
49 changes: 44 additions & 5 deletions tests/rest/client/test_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,13 +470,45 @@ 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")
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"
Expand All @@ -491,11 +523,18 @@ 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})
whoami = self._whoami(as_token)
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):
def _whoami(self, tok):
channel = self.make_request("GET", "account/whoami", {}, access_token=tok)
self.assertEqual(channel.code, 200)
return channel.json_body
Expand Down