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

Commit

Permalink
Add a new admin API to create a new device for a user. (#15611)
Browse files Browse the repository at this point in the history
This allows an external service (e.g. the matrix-authentication-service)
to create devices for users.
  • Loading branch information
sandhose authored May 17, 2023
1 parent 4ee82c0 commit 41b9def
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog.d/15611.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add a new admin API to create a new device for a user.
27 changes: 27 additions & 0 deletions docs/admin_api/user_admin_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,33 @@ The following fields are returned in the JSON response body:

- `total` - Total number of user's devices.

### Create a device

Creates a new device for a specific `user_id` and `device_id`. Does nothing if the `device_id`
exists already.

The API is:

```
POST /_synapse/admin/v2/users/<user_id>/devices
{
"device_id": "QBUAZIFURK"
}
```

An empty JSON dict is returned.

**Parameters**

The following parameters should be set in the URL:

- `user_id` - fully qualified: for example, `@user:server.com`.

The following fields are required in the JSON request body:

- `device_id` - The device ID to create.

### Delete multiple devices
Deletes the given devices for a specific `user_id`, and invalidates
any access token associated with them.
Expand Down
29 changes: 29 additions & 0 deletions synapse/rest/admin/devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,35 @@ async def on_GET(
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):
"""
Expand Down

0 comments on commit 41b9def

Please sign in to comment.