summary refs log tree commit diff
path: root/synapse/rest/admin/devices.py
diff options
context:
space:
mode:
authorQuentin Gliech <quenting@element.io>2023-05-17 16:39:06 +0200
committerGitHub <noreply@github.com>2023-05-17 14:39:06 +0000
commit41b9def9f2c02118796e147f63abf23bc2d7dc04 (patch)
tree8059221dc29f8931f0661ee59dfc7f1d004de36f /synapse/rest/admin/devices.py
parentApply url_preview_url_blacklist to oEmbed and pre-cached images (#15601) (diff)
downloadsynapse-41b9def9f2c02118796e147f63abf23bc2d7dc04.tar.xz
Add a new admin API to create a new device for a user. (#15611)
This allows an external service (e.g. the matrix-authentication-service)
to create devices for users.
Diffstat (limited to 'synapse/rest/admin/devices.py')
-rw-r--r--synapse/rest/admin/devices.py29
1 files changed, 29 insertions, 0 deletions
diff --git a/synapse/rest/admin/devices.py b/synapse/rest/admin/devices.py
index 3b2f2d9abb..11ebed9bfd 100644
--- a/synapse/rest/admin/devices.py
+++ b/synapse/rest/admin/devices.py
@@ -137,6 +137,35 @@ class DevicesRestServlet(RestServlet):
         devices = await self.device_handler.get_devices_by_user(target_user.to_string())
         return HTTPStatus.OK, {"devices": devices, "total": len(devices)}
 
+    async def on_POST(
+        self, request: SynapseRequest, user_id: str
+    ) -> Tuple[int, JsonDict]:
+        """Creates a new device for the user."""
+        await assert_requester_is_admin(self.auth, request)
+
+        target_user = UserID.from_string(user_id)
+        if not self.is_mine(target_user):
+            raise SynapseError(
+                HTTPStatus.BAD_REQUEST, "Can only create devices for local users"
+            )
+
+        u = await self.store.get_user_by_id(target_user.to_string())
+        if u is None:
+            raise NotFoundError("Unknown user")
+
+        body = parse_json_object_from_request(request)
+        device_id = body.get("device_id")
+        if not device_id:
+            raise SynapseError(HTTPStatus.BAD_REQUEST, "Missing device_id")
+        if not isinstance(device_id, str):
+            raise SynapseError(HTTPStatus.BAD_REQUEST, "device_id must be a string")
+
+        await self.device_handler.check_device_registered(
+            user_id=user_id, device_id=device_id
+        )
+
+        return HTTPStatus.CREATED, {}
+
 
 class DeleteDevicesRestServlet(RestServlet):
     """