Implement MSC3069: Guest support on whoami (#9655)

This commit is contained in:
Travis Ralston 2021-09-29 04:32:45 -06:00 committed by GitHub
parent 5279b9161b
commit 8cef1ab2ac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 51 additions and 7 deletions

1
changelog.d/9655.feature Normal file
View File

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

View File

@ -878,9 +878,13 @@ class WhoamiRestServlet(RestServlet):
self.auth = hs.get_auth() self.auth = hs.get_auth()
async def on_GET(self, request: SynapseRequest) -> Tuple[int, JsonDict]: 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 # Appservices and similar accounts do not have device IDs
# that we can report on, so exclude them for compliance. # that we can report on, so exclude them for compliance.

View File

@ -470,13 +470,45 @@ class WhoamiTestCase(unittest.HomeserverTestCase):
register.register_servlets, register.register_servlets,
] ]
def default_config(self):
config = super().default_config()
config["allow_guest_access"] = True
return config
def test_GET_whoami(self): def test_GET_whoami(self):
device_id = "wouldgohere" device_id = "wouldgohere"
user_id = self.register_user("kermit", "test") user_id = self.register_user("kermit", "test")
tok = self.login("kermit", "test", device_id=device_id) tok = self.login("kermit", "test", device_id=device_id)
whoami = self.whoami(tok) whoami = self._whoami(tok)
self.assertEqual(whoami, {"user_id": user_id, "device_id": device_id}) 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): def test_GET_whoami_appservices(self):
user_id = "@as:test" user_id = "@as:test"
@ -491,11 +523,18 @@ class WhoamiTestCase(unittest.HomeserverTestCase):
) )
self.hs.get_datastore().services_cache.append(appservice) 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.assertEqual(
whoami,
{
"user_id": user_id,
# Unstable until MSC3069 enters spec
"org.matrix.msc3069.is_guest": False,
},
)
self.assertFalse(hasattr(whoami, "device_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) channel = self.make_request("GET", "account/whoami", {}, access_token=tok)
self.assertEqual(channel.code, 200) self.assertEqual(channel.code, 200)
return channel.json_body return channel.json_body