1
0
Fork 0
mirror of https://gitlab.com/famedly/conduit.git synced 2024-06-17 16:38:18 +02:00
conduit/src/api/client_server/device.rs

170 lines
5.2 KiB
Rust
Raw Normal View History

2022-10-05 20:34:31 +02:00
use crate::{services, utils, Error, Result, Ruma};
2020-07-30 18:14:47 +02:00
use ruma::api::client::{
2022-02-18 15:33:14 +01:00
device::{self, delete_device, delete_devices, get_device, get_devices, update_device},
2020-07-30 18:14:47 +02:00
error::ErrorKind,
2022-02-18 15:33:14 +01:00
uiaa::{AuthFlow, AuthType, UiaaInfo},
2020-07-30 18:14:47 +02:00
};
use super::SESSION_ID_LENGTH;
2021-08-31 19:14:37 +02:00
/// # `GET /_matrix/client/r0/devices`
///
/// Get metadata on all devices of the sender user.
pub async fn get_devices_route(
2022-02-18 15:33:14 +01:00
body: Ruma<get_devices::v3::Request>,
) -> Result<get_devices::v3::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
2020-07-30 18:14:47 +02:00
let devices: Vec<device::Device> = services()
2020-07-30 18:14:47 +02:00
.users
.all_devices_metadata(sender_user)
2020-07-30 18:14:47 +02:00
.filter_map(|r| r.ok()) // Filter out buggy devices
.collect();
2020-07-30 18:14:47 +02:00
2022-02-18 15:33:14 +01:00
Ok(get_devices::v3::Response { devices })
2020-07-30 18:14:47 +02:00
}
2021-08-31 19:14:37 +02:00
/// # `GET /_matrix/client/r0/devices/{deviceId}`
///
/// Get metadata on a single device of the sender user.
pub async fn get_device_route(
2022-12-14 13:09:10 +01:00
body: Ruma<get_device::v3::Request>,
2022-02-18 15:33:14 +01:00
) -> Result<get_device::v3::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
2020-07-30 18:14:47 +02:00
let device = services()
2020-07-30 18:14:47 +02:00
.users
.get_device_metadata(sender_user, &body.body.device_id)?
2020-07-30 18:14:47 +02:00
.ok_or(Error::BadRequest(ErrorKind::NotFound, "Device not found."))?;
2022-02-18 15:33:14 +01:00
Ok(get_device::v3::Response { device })
2020-07-30 18:14:47 +02:00
}
2021-08-31 19:14:37 +02:00
/// # `PUT /_matrix/client/r0/devices/{deviceId}`
///
/// Updates the metadata on a given device of the sender user.
pub async fn update_device_route(
2022-12-14 13:09:10 +01:00
body: Ruma<update_device::v3::Request>,
2022-02-18 15:33:14 +01:00
) -> Result<update_device::v3::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
2020-07-30 18:14:47 +02:00
let mut device = services()
2020-07-30 18:14:47 +02:00
.users
.get_device_metadata(sender_user, &body.device_id)?
2020-07-30 18:14:47 +02:00
.ok_or(Error::BadRequest(ErrorKind::NotFound, "Device not found."))?;
device.display_name = body.display_name.clone();
2022-10-05 20:34:31 +02:00
services()
.users
.update_device_metadata(sender_user, &body.device_id, &device)?;
2020-07-30 18:14:47 +02:00
2022-02-18 15:33:14 +01:00
Ok(update_device::v3::Response {})
2020-07-30 18:14:47 +02:00
}
/// # `DELETE /_matrix/client/r0/devices/{deviceId}`
2021-08-31 19:14:37 +02:00
///
/// Deletes the given device.
///
/// - Requires UIAA to verify user password
/// - Invalidates access token
/// - Deletes device metadata (device id, device display name, last seen ip, last seen ts)
/// - Forgets to-device events
/// - Triggers device list updates
pub async fn delete_device_route(
2022-12-14 13:09:10 +01:00
body: Ruma<delete_device::v3::Request>,
2022-02-18 15:33:14 +01:00
) -> Result<delete_device::v3::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
let sender_device = body.sender_device.as_ref().expect("user is authenticated");
2020-07-30 18:14:47 +02:00
// UIAA
let mut uiaainfo = UiaaInfo {
flows: vec![AuthFlow {
stages: vec![AuthType::Password],
2020-07-30 18:14:47 +02:00
}],
completed: Vec::new(),
params: Default::default(),
session: None,
auth_error: None,
};
if let Some(auth) = &body.auth {
2022-10-05 20:34:31 +02:00
let (worked, uiaainfo) =
services()
.uiaa
.try_auth(sender_user, sender_device, auth, &uiaainfo)?;
2020-07-30 18:14:47 +02:00
if !worked {
return Err(Error::Uiaa(uiaainfo));
}
// Success!
} else if let Some(json) = body.json_body {
uiaainfo.session = Some(utils::random_string(SESSION_ID_LENGTH));
2022-10-05 20:34:31 +02:00
services()
.uiaa
.create(sender_user, sender_device, &uiaainfo, &json)?;
return Err(Error::Uiaa(uiaainfo));
2020-07-30 18:14:47 +02:00
} else {
return Err(Error::BadRequest(ErrorKind::NotJson, "Not json."));
2020-07-30 18:14:47 +02:00
}
2022-10-05 20:34:31 +02:00
services()
.users
.remove_device(sender_user, &body.device_id)?;
2022-02-18 15:33:14 +01:00
Ok(delete_device::v3::Response {})
2020-07-30 18:14:47 +02:00
}
2021-08-31 19:14:37 +02:00
/// # `PUT /_matrix/client/r0/devices/{deviceId}`
///
/// Deletes the given device.
///
/// - Requires UIAA to verify user password
///
/// For each device:
/// - Invalidates access token
/// - Deletes device metadata (device id, device display name, last seen ip, last seen ts)
/// - Forgets to-device events
/// - Triggers device list updates
pub async fn delete_devices_route(
2022-12-14 13:09:10 +01:00
body: Ruma<delete_devices::v3::Request>,
2022-02-18 15:33:14 +01:00
) -> Result<delete_devices::v3::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
let sender_device = body.sender_device.as_ref().expect("user is authenticated");
2020-07-30 18:14:47 +02:00
// UIAA
let mut uiaainfo = UiaaInfo {
flows: vec![AuthFlow {
stages: vec![AuthType::Password],
2020-07-30 18:14:47 +02:00
}],
completed: Vec::new(),
params: Default::default(),
session: None,
auth_error: None,
};
if let Some(auth) = &body.auth {
2022-10-05 20:34:31 +02:00
let (worked, uiaainfo) =
services()
.uiaa
.try_auth(sender_user, sender_device, auth, &uiaainfo)?;
2020-07-30 18:14:47 +02:00
if !worked {
return Err(Error::Uiaa(uiaainfo));
}
// Success!
} else if let Some(json) = body.json_body {
uiaainfo.session = Some(utils::random_string(SESSION_ID_LENGTH));
2022-10-05 20:34:31 +02:00
services()
.uiaa
.create(sender_user, sender_device, &uiaainfo, &json)?;
return Err(Error::Uiaa(uiaainfo));
2020-07-30 18:14:47 +02:00
} else {
return Err(Error::BadRequest(ErrorKind::NotJson, "Not json."));
2020-07-30 18:14:47 +02:00
}
for device_id in &body.devices {
services().users.remove_device(sender_user, device_id)?
2020-07-30 18:14:47 +02:00
}
2022-02-18 15:33:14 +01:00
Ok(delete_devices::v3::Response {})
2020-07-30 18:14:47 +02:00
}