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

Support MSC2033: Device ID on whoami #9918

Merged
merged 9 commits into from
Jul 27, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions synapse/rest/client/v2_alpha/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -884,10 +884,14 @@ def __init__(self, hs):
async def on_GET(self, request):
requester = await self.auth.get_user_by_req(request)

return 200, {
"user_id": requester.user.to_string(),
"device_id": requester.device_id,
}
response = {"user_id": requester.user.to_string()}

# Appservices and similar accounts do not have device IDs
# that we can report on, so exclude them for compliance.
if requester.device_id is not None:
response["device_id"] = requester.device_id

return 200, response


def register_servlets(hs, http_server):
Expand Down
18 changes: 18 additions & 0 deletions tests/rest/client/v2_alpha/test_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import synapse.rest.admin
from synapse.api.constants import LoginType, Membership
from synapse.api.errors import Codes, HttpResponseException
from synapse.appservice import ApplicationService
from synapse.rest.client.v1 import login, room
from synapse.rest.client.v2_alpha import account, register
from synapse.rest.synapse.client.password_reset import PasswordResetSubmitTokenResource
Expand Down Expand Up @@ -475,6 +476,23 @@ def test_GET_whoami(self):
whoami = self.whoami(tok)
self.assertEqual(whoami, {"user_id": user_id, "device_id": device_id})
babolivier marked this conversation as resolved.
Show resolved Hide resolved

def test_GET_whoami_appservices(self):
user_id = "@as_user_kermit:test"
as_token = "i_am_an_app_service"

appservice = ApplicationService(
as_token,
self.hs.config.server_name,
id="1234",
namespaces={"users": [{"regex": r"@as_user.*", "exclusive": True}]},
turt2live marked this conversation as resolved.
Show resolved Hide resolved
sender="@as:test",
)
self.hs.get_datastore().services_cache.append(appservice)

whoami = self.whoami(as_token)
self.assertEqual(whoami, {"user_id": user_id})
self.assertFalse(hasattr(whoami, "device_id"))

def whoami(self, tok):
channel = self.make_request("GET", "account/whoami", {}, access_token=tok)
self.assertEqual(channel.code, 200)
Expand Down