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

Commit b3a757e

Browse files
authored
Support MSC2033: Device ID on whoami (#9918)
* Fix no-access-token bug in deactivation tests * Support MSC2033: Device ID on whoami * Test for appservices too MSC: matrix-org/matrix-spec-proposals#2033 The MSC has passed FCP, which means stable endpoints can be used.
1 parent b7186c6 commit b3a757e

File tree

3 files changed

+51
-2
lines changed

3 files changed

+51
-2
lines changed

changelog.d/9918.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add support for [MSC2033](https://github.com/matrix-org/matrix-doc/pull/2033): `device_id` on `/account/whoami`.

synapse/rest/client/v2_alpha/account.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -884,7 +884,14 @@ def __init__(self, hs):
884884
async def on_GET(self, request):
885885
requester = await self.auth.get_user_by_req(request)
886886

887-
return 200, {"user_id": requester.user.to_string()}
887+
response = {"user_id": requester.user.to_string()}
888+
889+
# Appservices and similar accounts do not have device IDs
890+
# that we can report on, so exclude them for compliance.
891+
if requester.device_id is not None:
892+
response["device_id"] = requester.device_id
893+
894+
return 200, response
888895

889896

890897
def register_servlets(hs, http_server):

tests/rest/client/v2_alpha/test_account.py

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import synapse.rest.admin
2525
from synapse.api.constants import LoginType, Membership
2626
from synapse.api.errors import Codes, HttpResponseException
27+
from synapse.appservice import ApplicationService
2728
from synapse.rest.client.v1 import login, room
2829
from synapse.rest.client.v2_alpha import account, register
2930
from synapse.rest.synapse.client.password_reset import PasswordResetSubmitTokenResource
@@ -397,7 +398,7 @@ def test_deactivate_account(self):
397398
self.assertTrue(self.get_success(store.get_user_deactivated_status(user_id)))
398399

399400
# Check that this access token has been invalidated.
400-
channel = self.make_request("GET", "account/whoami")
401+
channel = self.make_request("GET", "account/whoami", access_token=tok)
401402
self.assertEqual(channel.code, 401)
402403

403404
def test_pending_invites(self):
@@ -458,6 +459,46 @@ def deactivate(self, user_id, tok):
458459
self.assertEqual(channel.code, 200)
459460

460461

462+
class WhoamiTestCase(unittest.HomeserverTestCase):
463+
464+
servlets = [
465+
synapse.rest.admin.register_servlets_for_client_rest_resource,
466+
login.register_servlets,
467+
account.register_servlets,
468+
register.register_servlets,
469+
]
470+
471+
def test_GET_whoami(self):
472+
device_id = "wouldgohere"
473+
user_id = self.register_user("kermit", "test")
474+
tok = self.login("kermit", "test", device_id=device_id)
475+
476+
whoami = self.whoami(tok)
477+
self.assertEqual(whoami, {"user_id": user_id, "device_id": device_id})
478+
479+
def test_GET_whoami_appservices(self):
480+
user_id = "@as:test"
481+
as_token = "i_am_an_app_service"
482+
483+
appservice = ApplicationService(
484+
as_token,
485+
self.hs.config.server_name,
486+
id="1234",
487+
namespaces={"users": [{"regex": user_id, "exclusive": True}]},
488+
sender=user_id,
489+
)
490+
self.hs.get_datastore().services_cache.append(appservice)
491+
492+
whoami = self.whoami(as_token)
493+
self.assertEqual(whoami, {"user_id": user_id})
494+
self.assertFalse(hasattr(whoami, "device_id"))
495+
496+
def whoami(self, tok):
497+
channel = self.make_request("GET", "account/whoami", {}, access_token=tok)
498+
self.assertEqual(channel.code, 200)
499+
return channel.json_body
500+
501+
461502
class ThreepidEmailRestTestCase(unittest.HomeserverTestCase):
462503

463504
servlets = [

0 commit comments

Comments
 (0)