Skip to main content

tuwunel_api/server/
user.rs

1use axum::extract::State;
2use futures::{FutureExt, StreamExt, TryFutureExt, future::join4};
3use ruma::{
4	UserId,
5	api::{
6		client::device::Device,
7		federation::{
8			device::get_devices::{self, v1::UserDevice},
9			keys::{claim_keys, get_keys},
10		},
11	},
12	uint,
13};
14use tuwunel_core::{Err, Result, utils::future::TryExtExt};
15
16use crate::{
17	Ruma,
18	client::{claim_keys_helper, get_keys_helper},
19};
20
21/// # `GET /_matrix/federation/v1/user/devices/{userId}`
22///
23/// Gets information on all devices of the user.
24pub(crate) async fn get_devices_route(
25	State(services): State<crate::State>,
26	body: Ruma<get_devices::v1::Request>,
27) -> Result<get_devices::v1::Response> {
28	let user_id = &body.user_id;
29	if !services.globals.user_is_local(user_id) {
30		return Err!(Request(InvalidParam("Tried to access user from other server.")));
31	}
32
33	let allowed_signatures = |u: &UserId| u.server_name() == body.origin();
34
35	let master_key = services
36		.users
37		.get_master_key(None, user_id, &allowed_signatures)
38		.ok();
39
40	let self_signing_key = services
41		.users
42		.get_self_signing_key(None, user_id, &allowed_signatures)
43		.ok();
44
45	let stream_id = services
46		.users
47		.get_devicelist_version(user_id)
48		.map_ok(TryInto::try_into)
49		.map_ok(Result::ok)
50		.ok();
51
52	let devices = services
53		.users
54		.all_devices_metadata(user_id)
55		.filter_map(async |Device { device_id, display_name, .. }: Device| {
56			let device_display_name = services
57				.config
58				.allow_device_name_federation
59				.then_some(display_name)
60				.flatten()
61				.or_else(|| Some(device_id.as_str().into()));
62
63			services
64				.users
65				.get_device_keys(user_id, &device_id)
66				.map_ok(|keys| UserDevice {
67					device_id: device_id.clone(),
68					device_display_name,
69					keys,
70				})
71				.map(Result::ok)
72				.await
73		})
74		.collect::<Vec<_>>();
75
76	let (stream_id, master_key, self_signing_key, devices) =
77		join4(stream_id, master_key, self_signing_key, devices)
78			.boxed()
79			.await;
80
81	Ok(get_devices::v1::Response {
82		user_id: body.body.user_id,
83		stream_id: stream_id.flatten().unwrap_or_else(|| uint!(0)),
84		devices,
85		self_signing_key,
86		master_key,
87	})
88}
89
90/// # `POST /_matrix/federation/v1/user/keys/query`
91///
92/// Gets devices and identity keys for the given users.
93pub(crate) async fn get_keys_route(
94	State(services): State<crate::State>,
95	body: Ruma<get_keys::v1::Request>,
96) -> Result<get_keys::v1::Response> {
97	if body
98		.device_keys
99		.iter()
100		.any(|(u, _)| !services.globals.user_is_local(u))
101	{
102		return Err!(Request(InvalidParam("User does not belong to this server.")));
103	}
104
105	let result = get_keys_helper(
106		&services,
107		None,
108		&body.device_keys,
109		|u| Some(u.server_name()) == body.origin.as_deref(),
110		services.config.allow_device_name_federation,
111	)
112	.await?;
113
114	Ok(get_keys::v1::Response {
115		device_keys: result.device_keys,
116		master_keys: result.master_keys,
117		self_signing_keys: result.self_signing_keys,
118	})
119}
120
121/// # `POST /_matrix/federation/v1/user/keys/claim`
122///
123/// Claims one-time keys.
124pub(crate) async fn claim_keys_route(
125	State(services): State<crate::State>,
126	body: Ruma<claim_keys::v1::Request>,
127) -> Result<claim_keys::v1::Response> {
128	if body
129		.one_time_keys
130		.iter()
131		.any(|(u, _)| !services.globals.user_is_local(u))
132	{
133		return Err!(Request(InvalidParam("Tried to access user from other server.")));
134	}
135
136	let result = claim_keys_helper(&services, &body.one_time_keys).await?;
137
138	Ok(claim_keys::v1::Response { one_time_keys: result.one_time_keys })
139}