diff --git a/src/api/admin/rooms/ban.rs b/src/api/admin/rooms/ban.rs index c24ff7691..384934644 100644 --- a/src/api/admin/rooms/ban.rs +++ b/src/api/admin/rooms/ban.rs @@ -13,7 +13,7 @@ pub(crate) async fn ban_room( State(services): State, body: Ruma, ) -> Result { - let sender_user = body.identity.sender_user(); + let sender_user = body.identity.expect_sender_user()?; if !services.users.is_admin(sender_user).await { return Err!(Request(Forbidden("Only server administrators can use this endpoint"))); } diff --git a/src/api/admin/rooms/list.rs b/src/api/admin/rooms/list.rs index fe7fc0857..af81f2eec 100644 --- a/src/api/admin/rooms/list.rs +++ b/src/api/admin/rooms/list.rs @@ -13,7 +13,7 @@ pub(crate) async fn list_rooms( State(services): State, body: Ruma, ) -> Result { - let sender_user = body.identity.sender_user(); + let sender_user = body.identity.expect_sender_user()?; if !services.users.is_admin(sender_user).await { return Err!(Request(Forbidden("Only server administrators can use this endpoint"))); } diff --git a/src/api/client/account/mod.rs b/src/api/client/account/mod.rs index db3de3b38..44131e135 100644 --- a/src/api/client/account/mod.rs +++ b/src/api/client/account/mod.rs @@ -109,7 +109,11 @@ pub(crate) async fn change_password_route( ClientIp(client): ClientIp, body: Ruma, ) -> Result { - let identity = if let Some(user_id) = body.identity.as_ref().map(ClientIdentity::sender_user) + let identity = if let Some(user_id) = body + .identity + .as_ref() + .map(ClientIdentity::expect_sender_user) + .transpose()? { // A signed-in user is trying to change their password, prompt them for their // existing one @@ -251,7 +255,7 @@ pub(crate) async fn whoami_route( body: Ruma, ) -> Result { Ok( - assign!(whoami::v3::Response::new(body.identity.sender_user().to_owned(), false), { + assign!(whoami::v3::Response::new(body.identity.expect_sender_user()?.to_owned(), false), { device_id: body.identity.sender_device().map(ToOwned::to_owned), }), ) @@ -279,8 +283,8 @@ pub(crate) async fn deactivate_route( let sender_user = body .identity .as_ref() - .map(ClientIdentity::sender_user) - .ok_or_else(|| err!(Request(MissingToken("Missing access token."))))?; + .map(ClientIdentity::expect_sender_user) + .ok_or_else(|| err!(Request(MissingToken("Missing access token."))))??; // Prompt the user to confirm with their password using UIAA let _ = services diff --git a/src/api/client/account/threepid.rs b/src/api/client/account/threepid.rs index fe9d700f5..ed6bdbd63 100644 --- a/src/api/client/account/threepid.rs +++ b/src/api/client/account/threepid.rs @@ -22,7 +22,7 @@ pub(crate) async fn third_party_route( State(services): State, body: Ruma, ) -> Result { - let sender_user = body.identity.sender_user(); + let sender_user = body.identity.expect_sender_user()?; let mut threepids = vec![]; if let Some(email) = services @@ -58,8 +58,8 @@ pub(crate) async fn request_3pid_management_token_via_email_route( let sender_user = body .identity .as_ref() - .map(ClientIdentity::sender_user) - .ok_or_else(|| err!(Request(MissingToken("Missing access token."))))?; + .map(ClientIdentity::expect_sender_user) + .ok_or_else(|| err!(Request(MissingToken("Missing access token."))))??; if !services.threepid.email_requirement().may_change() { return Err!(Request(Forbidden("You may not change your email address."))); @@ -124,7 +124,7 @@ pub(crate) async fn add_3pid_route( .uiaa .authenticate_password( &body.auth, - Some(Identity::from_user_id(body.identity.sender_user())), + Some(Identity::from_user_id(body.identity.expect_sender_user()?)), ) .await?; @@ -136,7 +136,7 @@ pub(crate) async fn add_3pid_route( services .threepid - .associate_localpart_email(body.identity.sender_user().localpart(), &email) + .associate_localpart_email(body.identity.expect_sender_user()?.localpart(), &email) .await?; Ok(add_3pid::v3::Response::new()) @@ -157,7 +157,7 @@ pub(crate) async fn delete_3pid_route( if services .threepid - .disassociate_localpart_email(body.identity.sender_user().localpart()) + .disassociate_localpart_email(body.identity.expect_sender_user()?.localpart()) .await .is_none() { diff --git a/src/api/client/account_data.rs b/src/api/client/account_data.rs index c46aa0d02..60fec42ab 100644 --- a/src/api/client/account_data.rs +++ b/src/api/client/account_data.rs @@ -22,7 +22,7 @@ pub(crate) async fn set_global_account_data_route( State(services): State, body: Ruma, ) -> Result { - let sender_user = body.identity.sender_user(); + let sender_user = body.identity.expect_sender_user()?; if sender_user != body.user_id && !body.identity.is_appservice() { return Err!(Request(Forbidden("You cannot set account data for other users."))); @@ -47,7 +47,7 @@ pub(crate) async fn set_room_account_data_route( State(services): State, body: Ruma, ) -> Result { - let sender_user = body.identity.sender_user(); + let sender_user = body.identity.expect_sender_user()?; if sender_user != body.user_id && !body.identity.is_appservice() { return Err!(Request(Forbidden("You cannot set account data for other users."))); @@ -72,7 +72,7 @@ pub(crate) async fn get_global_account_data_route( State(services): State, body: Ruma, ) -> Result { - let sender_user = body.identity.sender_user(); + let sender_user = body.identity.expect_sender_user()?; if sender_user != body.user_id && !body.identity.is_appservice() { return Err!(Request(Forbidden("You cannot get account data of other users."))); @@ -94,7 +94,7 @@ pub(crate) async fn get_room_account_data_route( State(services): State, body: Ruma, ) -> Result { - let sender_user = body.identity.sender_user(); + let sender_user = body.identity.expect_sender_user()?; if sender_user != body.user_id && !body.identity.is_appservice() { return Err!(Request(Forbidden("You cannot get account data of other users."))); diff --git a/src/api/client/admin/suspend.rs b/src/api/client/admin/suspend.rs index b78c0beec..613d954a6 100644 --- a/src/api/client/admin/suspend.rs +++ b/src/api/client/admin/suspend.rs @@ -13,7 +13,7 @@ pub(crate) async fn get_suspended_status( body: Ruma, ) -> Result { let (admin, active) = join( - services.users.is_admin(body.identity.sender_user()), + services.users.is_admin(body.identity.expect_sender_user()?), services.users.is_active(&body.user_id), ) .await; @@ -38,7 +38,7 @@ pub(crate) async fn put_suspended_status( State(services): State, body: Ruma, ) -> Result { - let sender_user = body.identity.sender_user(); + let sender_user = body.identity.expect_sender_user()?; let (sender_admin, active, target_admin) = join3( services.users.is_admin(sender_user), diff --git a/src/api/client/alias.rs b/src/api/client/alias.rs index cb355cfd3..5d4dec081 100644 --- a/src/api/client/alias.rs +++ b/src/api/client/alias.rs @@ -11,7 +11,8 @@ pub(crate) async fn create_alias_route( State(services): State, body: Ruma, ) -> Result { - let sender_user = body.identity.sender_user(); + let sender_user = body.identity.expect_sender_user()?; + if services.users.is_suspended(sender_user).await? { return Err!(Request(UserSuspended("You cannot perform this action while suspended."))); } @@ -59,7 +60,8 @@ pub(crate) async fn delete_alias_route( State(services): State, body: Ruma, ) -> Result { - let sender_user = body.identity.sender_user(); + let sender_user = body.identity.expect_sender_user()?; + if services.users.is_suspended(sender_user).await? { return Err!(Request(UserSuspended("You cannot perform this action while suspended."))); } diff --git a/src/api/client/backup.rs b/src/api/client/backup.rs index 4c2501226..a9b21013b 100644 --- a/src/api/client/backup.rs +++ b/src/api/client/backup.rs @@ -25,7 +25,7 @@ pub(crate) async fn create_backup_version_route( ) -> Result { let version = services .key_backups - .create_backup(body.identity.sender_user(), &body.algorithm)?; + .create_backup(body.identity.expect_sender_user()?, &body.algorithm)?; Ok(create_backup_version::v3::Response::new(version)) } @@ -40,7 +40,7 @@ pub(crate) async fn update_backup_version_route( ) -> Result { services .key_backups - .update_backup(body.identity.sender_user(), &body.version, &body.algorithm) + .update_backup(body.identity.expect_sender_user()?, &body.version, &body.algorithm) .await?; Ok(update_backup_version::v3::Response::new()) @@ -53,13 +53,15 @@ pub(crate) async fn get_latest_backup_info_route( State(services): State, body: Ruma, ) -> Result { + let sender_user = body.identity.expect_sender_user()?; + let (version, algorithm) = services .key_backups - .get_latest_backup(body.identity.sender_user()) + .get_latest_backup(sender_user) .await .map_err(|_| err!(Request(NotFound("Key backup does not exist."))))?; - let (count, etag) = get_count_etag(&services, body.identity.sender_user(), &version).await; + let (count, etag) = get_count_etag(&services, sender_user, &version).await; Ok(get_latest_backup_info::v3::Response::new(algorithm, count, etag, version)) } @@ -71,16 +73,17 @@ pub(crate) async fn get_backup_info_route( State(services): State, body: Ruma, ) -> Result { + let sender_user = body.identity.expect_sender_user()?; + let algorithm = services .key_backups - .get_backup(body.identity.sender_user(), &body.version) + .get_backup(sender_user, &body.version) .await .map_err(|_| { err!(Request(NotFound("Key backup does not exist at version {:?}", body.version))) })?; - let (count, etag) = - get_count_etag(&services, body.identity.sender_user(), &body.version).await; + let (count, etag) = get_count_etag(&services, sender_user, &body.version).await; Ok(get_backup_info::v3::Response::new(algorithm, count, etag, body.version.clone())) } @@ -97,7 +100,7 @@ pub(crate) async fn delete_backup_version_route( ) -> Result { services .key_backups - .delete_backup(body.identity.sender_user(), &body.version) + .delete_backup(body.identity.expect_sender_user()?, &body.version) .await; Ok(delete_backup_version::v3::Response::new()) @@ -115,9 +118,11 @@ pub(crate) async fn add_backup_keys_route( State(services): State, body: Ruma, ) -> Result { + let sender_user = body.identity.expect_sender_user()?; + if services .key_backups - .get_latest_backup_version(body.identity.sender_user()) + .get_latest_backup_version(sender_user) .await .is_ok_and(|version| version != body.version) { @@ -130,19 +135,12 @@ pub(crate) async fn add_backup_keys_route( for (session_id, key_data) in &room.sessions { services .key_backups - .add_key( - body.identity.sender_user(), - &body.version, - room_id, - session_id, - key_data, - ) + .add_key(sender_user, &body.version, room_id, session_id, key_data) .await?; } } - let (count, etag) = - get_count_etag(&services, body.identity.sender_user(), &body.version).await; + let (count, etag) = get_count_etag(&services, sender_user, &body.version).await; Ok(add_backup_keys::v3::Response::new(etag, count)) } @@ -159,9 +157,11 @@ pub(crate) async fn add_backup_keys_for_room_route( State(services): State, body: Ruma, ) -> Result { + let sender_user = body.identity.expect_sender_user()?; + if services .key_backups - .get_latest_backup_version(body.identity.sender_user()) + .get_latest_backup_version(sender_user) .await .is_ok_and(|version| version != body.version) { @@ -173,18 +173,11 @@ pub(crate) async fn add_backup_keys_for_room_route( for (session_id, key_data) in &body.sessions { services .key_backups - .add_key( - body.identity.sender_user(), - &body.version, - &body.room_id, - session_id, - key_data, - ) + .add_key(sender_user, &body.version, &body.room_id, session_id, key_data) .await?; } - let (count, etag) = - get_count_etag(&services, body.identity.sender_user(), &body.version).await; + let (count, etag) = get_count_etag(&services, sender_user, &body.version).await; Ok(add_backup_keys_for_room::v3::Response::new(etag, count)) } @@ -201,9 +194,11 @@ pub(crate) async fn add_backup_keys_for_session_route( State(services): State, body: Ruma, ) -> Result { + let sender_user = body.identity.expect_sender_user()?; + if services .key_backups - .get_latest_backup_version(body.identity.sender_user()) + .get_latest_backup_version(sender_user) .await .is_ok_and(|version| version != body.version) { @@ -216,7 +211,7 @@ pub(crate) async fn add_backup_keys_for_session_route( let mut ok_to_replace = true; if let Some(old_key) = &services .key_backups - .get_session(body.identity.sender_user(), &body.version, &body.room_id, &body.session_id) + .get_session(sender_user, &body.version, &body.room_id, &body.session_id) .await .ok() { @@ -275,7 +270,7 @@ pub(crate) async fn add_backup_keys_for_session_route( services .key_backups .add_key( - body.identity.sender_user(), + sender_user, &body.version, &body.room_id, &body.session_id, @@ -284,8 +279,7 @@ pub(crate) async fn add_backup_keys_for_session_route( .await?; } - let (count, etag) = - get_count_etag(&services, body.identity.sender_user(), &body.version).await; + let (count, etag) = get_count_etag(&services, sender_user, &body.version).await; Ok(add_backup_keys_for_session::v3::Response::new(etag, count)) } @@ -299,7 +293,7 @@ pub(crate) async fn get_backup_keys_route( ) -> Result { let rooms = services .key_backups - .get_all(body.identity.sender_user(), &body.version) + .get_all(body.identity.expect_sender_user()?, &body.version) .await; Ok(get_backup_keys::v3::Response::new(rooms)) @@ -314,7 +308,7 @@ pub(crate) async fn get_backup_keys_for_room_route( ) -> Result { let sessions = services .key_backups - .get_room(body.identity.sender_user(), &body.version, &body.room_id) + .get_room(body.identity.expect_sender_user()?, &body.version, &body.room_id) .await; Ok(get_backup_keys_for_room::v3::Response::new(sessions)) @@ -329,7 +323,12 @@ pub(crate) async fn get_backup_keys_for_session_route( ) -> Result { let key_data = services .key_backups - .get_session(body.identity.sender_user(), &body.version, &body.room_id, &body.session_id) + .get_session( + body.identity.expect_sender_user()?, + &body.version, + &body.room_id, + &body.session_id, + ) .await .map_err(|_| { err!(Request(NotFound(debug_error!("Backup key not found for this user's session.")))) @@ -345,13 +344,14 @@ pub(crate) async fn delete_backup_keys_route( State(services): State, body: Ruma, ) -> Result { + let sender_user = body.identity.expect_sender_user()?; + services .key_backups - .delete_all_keys(body.identity.sender_user(), &body.version) + .delete_all_keys(sender_user, &body.version) .await; - let (count, etag) = - get_count_etag(&services, body.identity.sender_user(), &body.version).await; + let (count, etag) = get_count_etag(&services, sender_user, &body.version).await; Ok(delete_backup_keys::v3::Response::new(etag, count)) } @@ -363,13 +363,14 @@ pub(crate) async fn delete_backup_keys_for_room_route( State(services): State, body: Ruma, ) -> Result { + let sender_user = body.identity.expect_sender_user()?; + services .key_backups - .delete_room_keys(body.identity.sender_user(), &body.version, &body.room_id) + .delete_room_keys(sender_user, &body.version, &body.room_id) .await; - let (count, etag) = - get_count_etag(&services, body.identity.sender_user(), &body.version).await; + let (count, etag) = get_count_etag(&services, sender_user, &body.version).await; Ok(delete_backup_keys_for_room::v3::Response::new(etag, count)) } @@ -381,18 +382,14 @@ pub(crate) async fn delete_backup_keys_for_session_route( State(services): State, body: Ruma, ) -> Result { + let sender_user = body.identity.expect_sender_user()?; + services .key_backups - .delete_room_key( - body.identity.sender_user(), - &body.version, - &body.room_id, - &body.session_id, - ) + .delete_room_key(sender_user, &body.version, &body.room_id, &body.session_id) .await; - let (count, etag) = - get_count_etag(&services, body.identity.sender_user(), &body.version).await; + let (count, etag) = get_count_etag(&services, sender_user, &body.version).await; Ok(delete_backup_keys_for_session::v3::Response::new(etag, count)) } diff --git a/src/api/client/capabilities.rs b/src/api/client/capabilities.rs index 0a88790e3..593ac1cc9 100644 --- a/src/api/client/capabilities.rs +++ b/src/api/client/capabilities.rs @@ -48,7 +48,11 @@ pub(crate) async fn get_capabilities_route( json!({"enabled": services.config.forget_forced_upon_leave}), )?; - if services.users.is_admin(body.identity.sender_user()).await { + if services + .users + .is_admin(body.identity.expect_sender_user()?) + .await + { // Advertise suspension API capabilities.set("uk.timedout.msc4323", json!({"suspend": true, "lock": false}))?; } diff --git a/src/api/client/context.rs b/src/api/client/context.rs index f47432732..e6064f496 100644 --- a/src/api/client/context.rs +++ b/src/api/client/context.rs @@ -37,7 +37,7 @@ pub(crate) async fn get_context_route( State(services): State, body: Ruma, ) -> Result { - let sender_user = body.identity.sender_user(); + let sender_user = body.identity.expect_sender_user()?; let sender_device = body.identity.sender_device(); let room_id = &body.room_id; let event_id = &body.event_id; diff --git a/src/api/client/dehydrated_device.rs b/src/api/client/dehydrated_device.rs index a02aef537..6f0698999 100644 --- a/src/api/client/dehydrated_device.rs +++ b/src/api/client/dehydrated_device.rs @@ -29,7 +29,7 @@ pub(crate) async fn put_dehydrated_device_route( services .users - .set_dehydrated_device(body.identity.sender_user(), body.body) + .set_dehydrated_device(body.identity.expect_sender_user()?, body.body) .await?; Ok(put_dehydrated_device::Response::new(device_id)) @@ -44,7 +44,7 @@ pub(crate) async fn delete_dehydrated_device_route( ClientIp(client): ClientIp, body: Ruma, ) -> Result { - let sender_user = body.identity.sender_user(); + let sender_user = body.identity.expect_sender_user()?; let device_id = services.users.get_dehydrated_device_id(sender_user).await?; @@ -62,7 +62,7 @@ pub(crate) async fn get_dehydrated_device_route( ClientIp(client): ClientIp, body: Ruma, ) -> Result { - let sender_user = body.identity.sender_user(); + let sender_user = body.identity.expect_sender_user()?; let device = services.users.get_dehydrated_device(sender_user).await?; @@ -78,7 +78,7 @@ pub(crate) async fn get_dehydrated_events_route( ClientIp(client): ClientIp, body: Ruma, ) -> Result { - let sender_user = body.identity.sender_user(); + let sender_user = body.identity.expect_sender_user()?; let device_id = &body.body.device_id; let existing_id = services.users.get_dehydrated_device_id(sender_user).await; diff --git a/src/api/client/device.rs b/src/api/client/device.rs index bbfa4e2cc..bd9891045 100644 --- a/src/api/client/device.rs +++ b/src/api/client/device.rs @@ -21,7 +21,7 @@ pub(crate) async fn get_devices_route( ) -> Result { let devices: Vec = services .users - .all_devices_metadata(body.identity.sender_user()) + .all_devices_metadata(body.identity.expect_sender_user()?) .collect() .await; @@ -37,7 +37,7 @@ pub(crate) async fn get_device_route( ) -> Result { let device = services .users - .get_device_metadata(body.identity.sender_user(), &body.body.device_id) + .get_device_metadata(body.identity.expect_sender_user()?, &body.body.device_id) .await .map_err(|_| err!(Request(NotFound("Device not found."))))?; @@ -53,7 +53,7 @@ pub(crate) async fn update_device_route( ClientIp(client): ClientIp, body: Ruma, ) -> Result { - let sender_user = body.identity.sender_user(); + let sender_user = body.identity.expect_sender_user()?; let appservice = body.identity.appservice_info(); match services @@ -118,7 +118,7 @@ pub(crate) async fn delete_device_route( State(services): State, body: Ruma, ) -> Result { - let sender_user = body.identity.sender_user(); + let sender_user = body.identity.expect_sender_user()?; let appservice = body.identity.appservice_info(); // Appservices get to skip UIAA for this endpoint @@ -154,7 +154,7 @@ pub(crate) async fn delete_devices_route( State(services): State, body: Ruma, ) -> Result { - let sender_user = body.identity.sender_user(); + let sender_user = body.identity.expect_sender_user()?; let appservice = body.identity.appservice_info(); // Appservices get to skip UIAA for this endpoint diff --git a/src/api/client/directory.rs b/src/api/client/directory.rs index 7c07679f6..6b209b4eb 100644 --- a/src/api/client/directory.rs +++ b/src/api/client/directory.rs @@ -112,7 +112,7 @@ pub(crate) async fn set_room_visibility_route( ClientIp(client): ClientIp, body: Ruma, ) -> Result { - let sender_user = body.identity.sender_user(); + let sender_user = body.identity.expect_sender_user()?; if !services.rooms.metadata.exists(&body.room_id).await { // Return 404 if the room doesn't exist diff --git a/src/api/client/filter.rs b/src/api/client/filter.rs index 1d1a4b996..8594beeab 100644 --- a/src/api/client/filter.rs +++ b/src/api/client/filter.rs @@ -15,7 +15,7 @@ pub(crate) async fn get_filter_route( ) -> Result { services .users - .get_filter(body.identity.sender_user(), &body.filter_id) + .get_filter(body.identity.expect_sender_user()?, &body.filter_id) .await .map(get_filter::v3::Response::new) .map_err(|_| err!(Request(NotFound("Filter not found.")))) @@ -30,7 +30,7 @@ pub(crate) async fn create_filter_route( ) -> Result { let filter_id = services .users - .create_filter(body.identity.sender_user(), &body.filter); + .create_filter(body.identity.expect_sender_user()?, &body.filter); Ok(create_filter::v3::Response::new(filter_id)) } diff --git a/src/api/client/keys.rs b/src/api/client/keys.rs index 60db77995..be73fc800 100644 --- a/src/api/client/keys.rs +++ b/src/api/client/keys.rs @@ -41,7 +41,7 @@ pub(crate) async fn upload_keys_route( State(services): State, body: Ruma, ) -> Result { - let sender_user = body.identity.sender_user(); + let sender_user = body.identity.expect_sender_user()?; let sender_device = body.identity.expect_sender_device()?; for (key_id, one_time_key) in &body.one_time_keys { @@ -155,7 +155,7 @@ pub(crate) async fn get_keys_route( State(services): State, body: Ruma, ) -> Result { - let sender_user = body.identity.sender_user(); + let sender_user = body.identity.expect_sender_user()?; get_keys_helper( &services, @@ -192,7 +192,7 @@ pub(crate) async fn upload_signing_keys_route( State(services): State, body: Ruma, ) -> Result { - let sender_user = body.identity.sender_user(); + let sender_user = body.identity.expect_sender_user()?; if uiaa_needed_to_upload_keys( services, @@ -288,7 +288,7 @@ pub(crate) async fn upload_signatures_route( return Ok(upload_signatures::v3::Response::new()); } - let sender_user = body.identity.sender_user(); + let sender_user = body.identity.expect_sender_user()?; for (user_id, keys) in &body.signed_keys { for (key_id, key) in keys { @@ -341,7 +341,7 @@ pub(crate) async fn get_key_changes_route( State(services): State, body: Ruma, ) -> Result { - let sender_user = body.identity.sender_user(); + let sender_user = body.identity.expect_sender_user()?; let mut device_list_updates = HashSet::new(); diff --git a/src/api/client/media.rs b/src/api/client/media.rs index d3ef3f269..05b63358d 100644 --- a/src/api/client/media.rs +++ b/src/api/client/media.rs @@ -53,7 +53,7 @@ pub(crate) async fn create_content_route( ClientIp(client): ClientIp, body: Ruma, ) -> Result { - let user = body.identity.sender_user(); + let user = body.identity.expect_sender_user()?; if services.users.is_suspended(user).await? { return Err!(Request(UserSuspended("You cannot perform this action while suspended."))); } @@ -92,7 +92,7 @@ pub(crate) async fn get_content_thumbnail_route( ClientIp(client): ClientIp, body: Ruma, ) -> Result { - let user = body.identity.sender_user(); + let user = body.identity.expect_sender_user()?; let dim = Dim::from_ruma(body.width, body.height, body.method.clone())?; let mxc = Mxc { @@ -142,7 +142,7 @@ pub(crate) async fn get_content_route( ClientIp(client): ClientIp, body: Ruma, ) -> Result { - let user = body.identity.sender_user(); + let user = body.identity.expect_sender_user()?; let mxc = Mxc { server_name: &body.server_name, @@ -189,7 +189,7 @@ pub(crate) async fn get_content_as_filename_route( ClientIp(client): ClientIp, body: Ruma, ) -> Result { - let user = body.identity.sender_user(); + let user = body.identity.expect_sender_user()?; let mxc = Mxc { server_name: &body.server_name, @@ -240,7 +240,7 @@ pub(crate) async fn get_media_preview_route( ClientIp(client): ClientIp, body: Ruma, ) -> Result { - let sender_user = body.identity.sender_user(); + let sender_user = body.identity.expect_sender_user()?; let url = &body.url; let url = Url::parse(&body.url).map_err(|e| { diff --git a/src/api/client/media_legacy.rs b/src/api/client/media_legacy.rs index 1dad361aa..f6ceb3a54 100644 --- a/src/api/client/media_legacy.rs +++ b/src/api/client/media_legacy.rs @@ -56,7 +56,7 @@ pub(crate) async fn get_media_preview_legacy_route( ClientIp(client): ClientIp, body: Ruma, ) -> Result { - let sender_user = body.identity.sender_user(); + let sender_user = body.identity.expect_sender_user()?; let url = &body.url; let url = Url::parse(&body.url).map_err(|e| { diff --git a/src/api/client/membership/ban.rs b/src/api/client/membership/ban.rs index 394b6ed5e..77e0c9c4e 100644 --- a/src/api/client/membership/ban.rs +++ b/src/api/client/membership/ban.rs @@ -15,7 +15,7 @@ pub(crate) async fn ban_user_route( State(services): State, body: Ruma, ) -> Result { - let sender_user = body.identity.sender_user(); + let sender_user = body.identity.expect_sender_user()?; if sender_user == body.user_id { return Err!(Request(Forbidden("You cannot ban yourself."))); diff --git a/src/api/client/membership/forget.rs b/src/api/client/membership/forget.rs index d5ee38fce..61c84572f 100644 --- a/src/api/client/membership/forget.rs +++ b/src/api/client/membership/forget.rs @@ -18,7 +18,7 @@ pub(crate) async fn forget_room_route( State(services): State, body: Ruma, ) -> Result { - let user_id = body.identity.sender_user(); + let user_id = body.identity.expect_sender_user()?; let room_id = &body.room_id; let joined = services.rooms.state_cache.is_joined(user_id, room_id); diff --git a/src/api/client/membership/invite.rs b/src/api/client/membership/invite.rs index 74742968e..788f431ae 100644 --- a/src/api/client/membership/invite.rs +++ b/src/api/client/membership/invite.rs @@ -29,7 +29,7 @@ pub(crate) async fn invite_user_route( ClientIp(client): ClientIp, body: Ruma, ) -> Result { - let sender_user = body.identity.sender_user(); + let sender_user = body.identity.expect_sender_user()?; if services.users.is_suspended(sender_user).await? { return Err!(Request(UserSuspended("You cannot perform this action while suspended."))); } diff --git a/src/api/client/membership/join.rs b/src/api/client/membership/join.rs index d71b21a10..ba7f0eaa0 100644 --- a/src/api/client/membership/join.rs +++ b/src/api/client/membership/join.rs @@ -28,7 +28,7 @@ pub(crate) async fn join_room_by_id_route( ClientIp(client): ClientIp, body: Ruma, ) -> Result { - let sender_user = body.identity.sender_user(); + let sender_user = body.identity.expect_sender_user()?; if services.users.is_suspended(sender_user).await? { return Err!(Request(UserSuspended("You cannot perform this action while suspended."))); } @@ -97,7 +97,7 @@ pub(crate) async fn join_room_by_id_or_alias_route( ClientIp(client): ClientIp, body: Ruma, ) -> Result { - let sender_user = body.identity.sender_user(); + let sender_user = body.identity.expect_sender_user()?; let body = &body.body; if services.users.is_suspended(sender_user).await? { return Err!(Request(UserSuspended("You cannot perform this action while suspended."))); diff --git a/src/api/client/membership/kick.rs b/src/api/client/membership/kick.rs index e151e5180..33f73f560 100644 --- a/src/api/client/membership/kick.rs +++ b/src/api/client/membership/kick.rs @@ -15,7 +15,7 @@ pub(crate) async fn kick_user_route( State(services): State, body: Ruma, ) -> Result { - let sender_user = body.identity.sender_user(); + let sender_user = body.identity.expect_sender_user()?; if services.users.is_suspended(sender_user).await? { return Err!(Request(UserSuspended("You cannot perform this action while suspended."))); } diff --git a/src/api/client/membership/knock.rs b/src/api/client/membership/knock.rs index 0238d2741..988767046 100644 --- a/src/api/client/membership/knock.rs +++ b/src/api/client/membership/knock.rs @@ -51,7 +51,7 @@ pub(crate) async fn knock_room_route( ClientIp(client): ClientIp, body: Ruma, ) -> Result { - let sender_user = body.identity.sender_user(); + let sender_user = body.identity.expect_sender_user()?; let body = &body.body; if services.users.is_suspended(sender_user).await? { return Err!(Request(UserSuspended("You cannot perform this action while suspended."))); diff --git a/src/api/client/membership/leave.rs b/src/api/client/membership/leave.rs index 7a13f1f53..d4c9add00 100644 --- a/src/api/client/membership/leave.rs +++ b/src/api/client/membership/leave.rs @@ -32,10 +32,15 @@ pub(crate) async fn leave_room_route( State(services): State, body: Ruma, ) -> Result { - leave_room(&services, body.identity.sender_user(), &body.room_id, body.reason.clone()) - .boxed() - .await - .map(|()| leave_room::v3::Response::new()) + leave_room( + &services, + body.identity.expect_sender_user()?, + &body.room_id, + body.reason.clone(), + ) + .boxed() + .await + .map(|()| leave_room::v3::Response::new()) } // Make a user leave all their joined rooms, rescinds knocks, forgets all rooms, diff --git a/src/api/client/membership/members.rs b/src/api/client/membership/members.rs index 7f87e2d92..5c8d00829 100644 --- a/src/api/client/membership/members.rs +++ b/src/api/client/membership/members.rs @@ -30,7 +30,7 @@ pub(crate) async fn get_member_events_route( State(services): State, body: Ruma, ) -> Result { - let sender_user = body.identity.sender_user(); + let sender_user = body.identity.expect_sender_user()?; let membership = body.membership.as_ref(); let not_membership = body.not_membership.as_ref(); @@ -72,7 +72,7 @@ pub(crate) async fn joined_members_route( if !services .rooms .state_accessor - .user_can_see_state_events(body.identity.sender_user(), &body.room_id) + .user_can_see_state_events(body.identity.expect_sender_user()?, &body.room_id) .await { return Err!(Request(Forbidden("You don't have permission to view this room."))); diff --git a/src/api/client/membership/mod.rs b/src/api/client/membership/mod.rs index f6a6801d9..0b2ee55b3 100644 --- a/src/api/client/membership/mod.rs +++ b/src/api/client/membership/mod.rs @@ -40,7 +40,7 @@ pub(crate) async fn joined_rooms_route( let joined_rooms = services .rooms .state_cache - .rooms_joined(body.identity.sender_user()) + .rooms_joined(body.identity.expect_sender_user()?) .collect() .await; diff --git a/src/api/client/membership/unban.rs b/src/api/client/membership/unban.rs index be8c89134..13b41ee00 100644 --- a/src/api/client/membership/unban.rs +++ b/src/api/client/membership/unban.rs @@ -14,7 +14,7 @@ pub(crate) async fn unban_user_route( State(services): State, body: Ruma, ) -> Result { - let sender_user = body.identity.sender_user(); + let sender_user = body.identity.expect_sender_user()?; if services.users.is_suspended(sender_user).await? { return Err!(Request(UserSuspended("You cannot perform this action while suspended."))); } diff --git a/src/api/client/message.rs b/src/api/client/message.rs index 00f7f94d2..a2da36e48 100644 --- a/src/api/client/message.rs +++ b/src/api/client/message.rs @@ -75,7 +75,7 @@ pub(crate) async fn get_message_events_route( ClientIp(client_ip): ClientIp, body: Ruma, ) -> Result { - let sender_user = body.identity.sender_user(); + let sender_user = body.identity.expect_sender_user()?; let sender_device = body.identity.sender_device(); let room_id = &body.room_id; let filter = &body.filter; diff --git a/src/api/client/mutual_rooms.rs b/src/api/client/mutual_rooms.rs index cbec98d0e..a20a3c432 100644 --- a/src/api/client/mutual_rooms.rs +++ b/src/api/client/mutual_rooms.rs @@ -15,7 +15,7 @@ pub(crate) async fn get_mutual_rooms_route( State(services): State, body: Ruma, ) -> Result { - let sender_user = body.identity.sender_user(); + let sender_user = body.identity.expect_sender_user()?; if sender_user == body.user_id { return Err!(Request(Unknown("You cannot request rooms in common with yourself."))); diff --git a/src/api/client/openid.rs b/src/api/client/openid.rs index c3149d856..5a95d1672 100644 --- a/src/api/client/openid.rs +++ b/src/api/client/openid.rs @@ -16,7 +16,7 @@ pub(crate) async fn create_openid_token_route( State(services): State, body: Ruma, ) -> Result { - let sender_user = body.identity.sender_user(); + let sender_user = body.identity.expect_sender_user()?; if sender_user != body.user_id { return Err!(Request(InvalidParam( diff --git a/src/api/client/presence.rs b/src/api/client/presence.rs index 07622d028..ffa1760ba 100644 --- a/src/api/client/presence.rs +++ b/src/api/client/presence.rs @@ -16,23 +16,19 @@ pub(crate) async fn set_presence_route( State(services): State, body: Ruma, ) -> Result { + let sender_user = body.identity.expect_sender_user()?; + if !services.config.allow_local_presence { return Err!(Request(Forbidden("Presence is disabled on this server"))); } - if body.identity.sender_user() != body.user_id && !body.identity.is_appservice() { + if sender_user != body.user_id && !body.identity.is_appservice() { return Err!(Request(InvalidParam("Not allowed to set presence of other users"))); } services .presence - .set_presence( - body.identity.sender_user(), - &body.presence, - None, - None, - body.status_msg.clone(), - ) + .set_presence(sender_user, &body.presence, None, None, body.status_msg.clone()) .await?; Ok(set_presence::v3::Response::new()) @@ -55,7 +51,7 @@ pub(crate) async fn get_presence_route( let has_shared_rooms = services .rooms .state_cache - .user_sees_user(body.identity.sender_user(), &body.user_id) + .user_sees_user(body.identity.expect_sender_user()?, &body.user_id) .await; if has_shared_rooms { diff --git a/src/api/client/profile.rs b/src/api/client/profile.rs index 0e4f75088..e56e224cf 100644 --- a/src/api/client/profile.rs +++ b/src/api/client/profile.rs @@ -51,11 +51,11 @@ pub(crate) async fn set_profile_field_route( State(services): State, body: Ruma, ) -> Result { - if body.user_id != body.identity.sender_user() + if body.user_id != body.identity.expect_sender_user()? && !(body.identity.is_appservice() || services .admin - .user_is_admin(body.identity.sender_user()) + .user_is_admin(body.identity.expect_sender_user()?) .await) { return Err!(Request(Forbidden("You may not change other users' profile data."))); @@ -75,11 +75,11 @@ pub(crate) async fn delete_profile_field_route( State(services): State, body: Ruma, ) -> Result { - if body.user_id != body.identity.sender_user() + if body.user_id != body.identity.expect_sender_user()? && !(body.identity.is_appservice() || services .admin - .user_is_admin(body.identity.sender_user()) + .user_is_admin(body.identity.expect_sender_user()?) .await) { return Err!(Request(Forbidden("You may not change other users' profile data."))); diff --git a/src/api/client/push.rs b/src/api/client/push.rs index 47e79e65e..4c1dea302 100644 --- a/src/api/client/push.rs +++ b/src/api/client/push.rs @@ -30,7 +30,7 @@ pub(crate) async fn get_pushrules_all_route( State(services): State, body: Ruma, ) -> Result { - let sender_user = body.identity.sender_user(); + let sender_user = body.identity.expect_sender_user()?; let Some(content_value) = services .account_data @@ -101,7 +101,7 @@ pub(crate) async fn get_pushrules_global_route( State(services): State, body: Ruma, ) -> Result { - let sender_user = body.identity.sender_user(); + let sender_user = body.identity.expect_sender_user()?; let Some(content_value) = services .account_data @@ -189,7 +189,7 @@ pub(crate) async fn get_pushrule_route( State(services): State, body: Ruma, ) -> Result { - let sender_user = body.identity.sender_user(); + let sender_user = body.identity.expect_sender_user()?; // remove old deprecated mentions push rules as per MSC4210 #[allow(deprecated)] @@ -226,7 +226,7 @@ pub(crate) async fn set_pushrule_route( State(services): State, body: Ruma, ) -> Result { - let sender_user = body.identity.sender_user(); + let sender_user = body.identity.expect_sender_user()?; let body = &body.body; let mut account_data: PushRulesEvent = services .account_data @@ -282,7 +282,7 @@ pub(crate) async fn get_pushrule_actions_route( State(services): State, body: Ruma, ) -> Result { - let sender_user = body.identity.sender_user(); + let sender_user = body.identity.expect_sender_user()?; // remove old deprecated mentions push rules as per MSC4210 #[allow(deprecated)] @@ -316,7 +316,7 @@ pub(crate) async fn set_pushrule_actions_route( State(services): State, body: Ruma, ) -> Result { - let sender_user = body.identity.sender_user(); + let sender_user = body.identity.expect_sender_user()?; let mut account_data: PushRulesEvent = services .account_data @@ -349,7 +349,7 @@ pub(crate) async fn get_pushrule_enabled_route( State(services): State, body: Ruma, ) -> Result { - let sender_user = body.identity.sender_user(); + let sender_user = body.identity.expect_sender_user()?; // remove old deprecated mentions push rules as per MSC4210 #[allow(deprecated)] @@ -383,7 +383,7 @@ pub(crate) async fn set_pushrule_enabled_route( State(services): State, body: Ruma, ) -> Result { - let sender_user = body.identity.sender_user(); + let sender_user = body.identity.expect_sender_user()?; let mut account_data: PushRulesEvent = services .account_data @@ -416,7 +416,7 @@ pub(crate) async fn delete_pushrule_route( State(services): State, body: Ruma, ) -> Result { - let sender_user = body.identity.sender_user(); + let sender_user = body.identity.expect_sender_user()?; let mut account_data: PushRulesEvent = services .account_data @@ -458,7 +458,7 @@ pub(crate) async fn get_pushers_route( State(services): State, body: Ruma, ) -> Result { - let sender_user = body.identity.sender_user(); + let sender_user = body.identity.expect_sender_user()?; Ok(get_pushers::v3::Response::new(services.pusher.get_pushers(sender_user).await)) } @@ -472,7 +472,7 @@ pub(crate) async fn set_pushers_route( State(services): State, body: Ruma, ) -> Result { - let sender_user = body.identity.sender_user(); + let sender_user = body.identity.expect_sender_user()?; let sender_device = body.identity.expect_sender_device()?; services diff --git a/src/api/client/read_marker.rs b/src/api/client/read_marker.rs index bd31eca71..e1e66764a 100644 --- a/src/api/client/read_marker.rs +++ b/src/api/client/read_marker.rs @@ -26,7 +26,7 @@ pub(crate) async fn set_read_marker_route( State(services): State, body: Ruma, ) -> Result { - let sender_user = body.identity.sender_user(); + let sender_user = body.identity.expect_sender_user()?; if let Some(event) = &body.fully_read { let fully_read_event = FullyReadEvent::new(FullyReadEventContent::new(event.to_owned())); @@ -118,7 +118,8 @@ pub(crate) async fn create_receipt_route( ClientIp(client_ip): ClientIp, body: Ruma, ) -> Result { - let sender_user = body.identity.sender_user(); + let sender_user = body.identity.expect_sender_user()?; + services .users .update_device_last_seen(sender_user, body.identity.sender_device(), client_ip) diff --git a/src/api/client/redact.rs b/src/api/client/redact.rs index 0eb48bf0e..377b14f4c 100644 --- a/src/api/client/redact.rs +++ b/src/api/client/redact.rs @@ -17,7 +17,7 @@ pub(crate) async fn redact_event_route( ClientIp(client_ip): ClientIp, body: Ruma, ) -> Result { - let sender_user = body.identity.sender_user(); + let sender_user = body.identity.expect_sender_user()?; services .users .update_device_last_seen(sender_user, body.identity.sender_device(), client_ip) diff --git a/src/api/client/relations.rs b/src/api/client/relations.rs index 53ac0de1d..6d1bd4ffd 100644 --- a/src/api/client/relations.rs +++ b/src/api/client/relations.rs @@ -28,7 +28,7 @@ pub(crate) async fn get_relating_events_with_rel_type_and_event_type_route( ) -> Result { paginate_relations_with_filter( &services, - body.identity.sender_user(), + body.identity.expect_sender_user()?, &body.room_id, &body.event_id, body.event_type.clone().into(), @@ -56,7 +56,7 @@ pub(crate) async fn get_relating_events_with_rel_type_route( ) -> Result { paginate_relations_with_filter( &services, - body.identity.sender_user(), + body.identity.expect_sender_user()?, &body.room_id, &body.event_id, None, @@ -84,7 +84,7 @@ pub(crate) async fn get_relating_events_route( ) -> Result { paginate_relations_with_filter( &services, - body.identity.sender_user(), + body.identity.expect_sender_user()?, &body.room_id, &body.event_id, None, diff --git a/src/api/client/report.rs b/src/api/client/report.rs index 5fe1b4a24..a7567c113 100644 --- a/src/api/client/report.rs +++ b/src/api/client/report.rs @@ -36,7 +36,7 @@ pub(crate) async fn report_room_route( ClientIp(client): ClientIp, body: Ruma, ) -> Result { - let sender_user = body.identity.sender_user(); + let sender_user = body.identity.expect_sender_user()?; if services.users.is_suspended(sender_user).await? { return Err!(Request(UserSuspended("You cannot perform this action while suspended."))); } @@ -92,7 +92,7 @@ pub(crate) async fn report_event_route( body: Ruma, ) -> Result { // user authentication - let sender_user = body.identity.sender_user(); + let sender_user = body.identity.expect_sender_user()?; if services.users.is_suspended(sender_user).await? { return Err!(Request(UserSuspended("You cannot perform this action while suspended."))); } @@ -135,7 +135,7 @@ pub(crate) async fn report_user_route( ClientIp(client): ClientIp, body: Ruma, ) -> Result { - let sender_user = body.identity.sender_user(); + let sender_user = body.identity.expect_sender_user()?; if services.users.is_suspended(sender_user).await? { return Err!(Request(UserSuspended("You cannot perform this action while suspended."))); diff --git a/src/api/client/room/aliases.rs b/src/api/client/room/aliases.rs index ac13e7e34..f5a5a57c0 100644 --- a/src/api/client/room/aliases.rs +++ b/src/api/client/room/aliases.rs @@ -15,7 +15,7 @@ pub(crate) async fn get_room_aliases_route( State(services): State, body: Ruma, ) -> Result { - let sender_user = body.identity.sender_user(); + let sender_user = body.identity.expect_sender_user()?; if !services .rooms diff --git a/src/api/client/room/create.rs b/src/api/client/room/create.rs index 80f60ae25..259fb9a12 100644 --- a/src/api/client/room/create.rs +++ b/src/api/client/room/create.rs @@ -61,7 +61,7 @@ pub(crate) async fn create_room_route( ) -> Result { use create_room::v3::RoomPreset; - let sender_user = body.identity.sender_user(); + let sender_user = body.identity.expect_sender_user()?; if !services.globals.allow_room_creation() && !body.identity.is_appservice() diff --git a/src/api/client/room/event.rs b/src/api/client/room/event.rs index 3c3a920ae..1040269a6 100644 --- a/src/api/client/room/event.rs +++ b/src/api/client/room/event.rs @@ -12,6 +12,7 @@ pub(crate) async fn get_room_event_route( State(ref services): State, ref body: Ruma, ) -> Result { + let sender_user = body.identity.expect_sender_user()?; let event_id = &body.event_id; let room_id = &body.room_id; @@ -24,25 +25,25 @@ pub(crate) async fn get_room_event_route( let visible = services .rooms .state_accessor - .user_can_see_event(body.identity.sender_user(), room_id, event_id) + .user_can_see_event(sender_user, room_id, event_id) .map(Ok); let (mut event, visible) = try_join(event, visible).await?; - if !visible || is_ignored_pdu(services, &event, body.identity.sender_user()).await? { + if !visible || is_ignored_pdu(services, &event, sender_user).await? { return Err!(Request(Forbidden("You don't have permission to view this event."))); } if let Err(e) = services .rooms .pdu_metadata - .add_bundled_aggregations_to_pdu(body.identity.sender_user(), &mut event) + .add_bundled_aggregations_to_pdu(sender_user, &mut event) .await { debug_warn!("Failed to add bundled aggregations to event: {e}"); } - event.set_unsigned(Some(body.identity.sender_user())); + event.set_unsigned(Some(body.identity.expect_sender_user()?)); Ok(get_room_event::v3::Response::new(event.into_format())) } diff --git a/src/api/client/room/initial_sync.rs b/src/api/client/room/initial_sync.rs index f3f925f33..393557223 100644 --- a/src/api/client/room/initial_sync.rs +++ b/src/api/client/room/initial_sync.rs @@ -17,12 +17,13 @@ pub(crate) async fn room_initial_sync_route( State(services): State, body: Ruma, ) -> Result { + let sender_user = body.identity.expect_sender_user()?; let room_id = &body.room_id; if !services .rooms .state_accessor - .user_can_see_state_events(body.identity.sender_user(), room_id) + .user_can_see_state_events(sender_user, room_id) .await { return Err!(Request(Forbidden("No room preview available."))); @@ -31,7 +32,7 @@ pub(crate) async fn room_initial_sync_route( let membership = services .rooms .state_cache - .user_membership(body.identity.sender_user(), room_id) + .user_membership(sender_user, room_id) .map(Ok); let visibility = services.rooms.directory.visibility(room_id).map(Ok); @@ -52,11 +53,11 @@ pub(crate) async fn room_initial_sync_route( .pdus_rev(room_id, None) .try_take(limit) .and_then(async |mut pdu| { - pdu.1.set_unsigned(Some(body.identity.sender_user())); + pdu.1.set_unsigned(Some(sender_user)); if let Err(e) = services .rooms .pdu_metadata - .add_bundled_aggregations_to_pdu(body.identity.sender_user(), &mut pdu.1) + .add_bundled_aggregations_to_pdu(sender_user, &mut pdu.1) .await { debug_warn!("Failed to add bundled aggregations: {e}"); diff --git a/src/api/client/room/summary.rs b/src/api/client/room/summary.rs index 581b1de7d..80c2e2f70 100644 --- a/src/api/client/room/summary.rs +++ b/src/api/client/room/summary.rs @@ -29,7 +29,10 @@ pub(crate) async fn get_room_summary( .rooms .summary .get_room_summary_for_user( - body.identity.as_ref().map(ClientIdentity::sender_user), + body.identity + .as_ref() + .map(ClientIdentity::expect_sender_user) + .transpose()?, &room_id, &servers, ) diff --git a/src/api/client/room/upgrade.rs b/src/api/client/room/upgrade.rs index a3fd0c967..eb1c243fc 100644 --- a/src/api/client/room/upgrade.rs +++ b/src/api/client/room/upgrade.rs @@ -277,7 +277,7 @@ pub(crate) async fn upgrade_room_route( State(services): State, body: Ruma, ) -> Result { - let sender_user = body.identity.sender_user(); + let sender_user = body.identity.expect_sender_user()?; let (supported, forbid_unstable, is_unstable) = ( services.server.supported_room_version(&body.new_version), diff --git a/src/api/client/search.rs b/src/api/client/search.rs index 811500845..5e27fdbd9 100644 --- a/src/api/client/search.rs +++ b/src/api/client/search.rs @@ -43,7 +43,7 @@ pub(crate) async fn search_events_route( State(services): State, body: Ruma, ) -> Result { - let sender_user = body.identity.sender_user(); + let sender_user = body.identity.expect_sender_user()?; let next_batch = body.next_batch.as_deref(); let mut result_categories = ResultCategories::new(); diff --git a/src/api/client/send.rs b/src/api/client/send.rs index e58a914e2..7a34c496d 100644 --- a/src/api/client/send.rs +++ b/src/api/client/send.rs @@ -22,7 +22,7 @@ pub(crate) async fn send_message_event_route( ClientIp(client_ip): ClientIp, body: Ruma, ) -> Result { - let sender_user = body.identity.sender_user(); + let sender_user = body.identity.expect_sender_user()?; let sender_device = body.identity.sender_device(); if services.users.is_suspended(sender_user).await? { diff --git a/src/api/client/session.rs b/src/api/client/session.rs index 4cb90678b..881b740f5 100644 --- a/src/api/client/session.rs +++ b/src/api/client/session.rs @@ -254,7 +254,7 @@ pub(crate) async fn login_token_route( return Err!(Request(Forbidden("Login via an existing session is not enabled"))); } - let sender_user = body.identity.sender_user(); + let sender_user = body.identity.expect_sender_user()?; // Prompt the user to confirm with their password using UIAA let _ = services @@ -286,7 +286,7 @@ pub(crate) async fn logout_route( ClientIp(client): ClientIp, body: Ruma, ) -> Result { - let sender_user = body.identity.sender_user(); + let sender_user = body.identity.expect_sender_user()?; let sender_device = body.identity.expect_sender_device()?; services @@ -334,7 +334,7 @@ pub(crate) async fn logout_all_route( ClientIp(client): ClientIp, body: Ruma, ) -> Result { - let sender_user = body.identity.sender_user(); + let sender_user = body.identity.expect_sender_user()?; services .users .all_device_ids(sender_user) diff --git a/src/api/client/space.rs b/src/api/client/space.rs index 6873aa969..ed23f5897 100644 --- a/src/api/client/space.rs +++ b/src/api/client/space.rs @@ -27,7 +27,7 @@ pub(crate) async fn get_hierarchy_route( .rooms .summary .get_room_hierarchy_for_user( - body.identity.sender_user(), + body.identity.expect_sender_user()?, body.room_id.clone(), max_depth, body.suggested_only, diff --git a/src/api/client/state.rs b/src/api/client/state.rs index 95c399391..1067501d3 100644 --- a/src/api/client/state.rs +++ b/src/api/client/state.rs @@ -38,7 +38,7 @@ pub(crate) async fn send_state_event_for_key_route( ClientIp(ip): ClientIp, body: Ruma, ) -> Result { - let sender_user = body.identity.sender_user(); + let sender_user = body.identity.expect_sender_user()?; services .users .update_device_last_seen(sender_user, body.identity.sender_device(), ip) @@ -91,7 +91,7 @@ pub(crate) async fn get_state_events_route( State(services): State, body: Ruma, ) -> Result { - let sender_user = body.identity.sender_user(); + let sender_user = body.identity.expect_sender_user()?; if !services .rooms @@ -125,7 +125,7 @@ pub(crate) async fn get_state_event_for_key_route( State(services): State, body: Ruma, ) -> Result { - let sender_user = body.identity.sender_user(); + let sender_user = body.identity.expect_sender_user()?; if !services .rooms diff --git a/src/api/client/sync/v3/mod.rs b/src/api/client/sync/v3/mod.rs index c32ed22cd..8863ad54a 100644 --- a/src/api/client/sync/v3/mod.rs +++ b/src/api/client/sync/v3/mod.rs @@ -181,7 +181,7 @@ pub(crate) async fn sync_events_route( ClientIp(client_ip): ClientIp, body: Ruma, ) -> Result { - let sender_user = body.identity.sender_user(); + let sender_user = body.identity.expect_sender_user()?; let sender_device = body.identity.expect_sender_device()?; // Presence update @@ -226,7 +226,7 @@ pub(crate) async fn build_sync_events( services: &Services, body: &Ruma, ) -> Result { - let syncing_user = body.identity.sender_user(); + let syncing_user = body.identity.sender_user().expect("should have a user"); let syncing_device = body.identity.sender_device().expect("should have a device"); let current_count = services.globals.current_count()?; diff --git a/src/api/client/sync/v5.rs b/src/api/client/sync/v5.rs index 900619c5f..9bbcbab71 100644 --- a/src/api/client/sync/v5.rs +++ b/src/api/client/sync/v5.rs @@ -70,7 +70,7 @@ pub(crate) async fn sync_events_v5_route( body: Ruma, ) -> Result { debug_assert!(DEFAULT_BUMP_TYPES.is_sorted(), "DEFAULT_BUMP_TYPES is not sorted"); - let sender_user = body.identity.sender_user(); + let sender_user = body.identity.expect_sender_user()?; let sender_device = body.identity.expect_sender_device()?; services diff --git a/src/api/client/tag.rs b/src/api/client/tag.rs index f9a8bd6d5..7a7c11f79 100644 --- a/src/api/client/tag.rs +++ b/src/api/client/tag.rs @@ -21,7 +21,7 @@ pub(crate) async fn update_tag_route( State(services): State, body: Ruma, ) -> Result { - let sender_user = body.identity.sender_user(); + let sender_user = body.identity.expect_sender_user()?; let mut tags_event = services .account_data @@ -56,7 +56,7 @@ pub(crate) async fn delete_tag_route( State(services): State, body: Ruma, ) -> Result { - let sender_user = body.identity.sender_user(); + let sender_user = body.identity.expect_sender_user()?; let mut tags_event = services .account_data @@ -88,7 +88,7 @@ pub(crate) async fn get_tags_route( State(services): State, body: Ruma, ) -> Result { - let sender_user = body.identity.sender_user(); + let sender_user = body.identity.expect_sender_user()?; let tags_event = services .account_data diff --git a/src/api/client/threads.rs b/src/api/client/threads.rs index 97f9f5b73..a6cd50ba9 100644 --- a/src/api/client/threads.rs +++ b/src/api/client/threads.rs @@ -16,6 +16,8 @@ pub(crate) async fn get_threads_route( State(services): State, ref body: Ruma, ) -> Result { + let sender_user = body.identity.expect_sender_user()?; + // Use limit or else 10, with maximum 100 let limit = body .limit @@ -34,14 +36,14 @@ pub(crate) async fn get_threads_route( let threads: Vec<(PduCount, PduEvent)> = services .rooms .threads - .threads_until(body.identity.sender_user(), &body.room_id, from, &body.include) + .threads_until(sender_user, &body.room_id, from, &body.include) .await? .take(limit) .filter_map(|(count, pdu)| async move { services .rooms .state_accessor - .user_can_see_event(body.identity.sender_user(), &body.room_id, &pdu.event_id) + .user_can_see_event(sender_user, &body.room_id, &pdu.event_id) .await .then_some((count, pdu)) }) @@ -49,7 +51,7 @@ pub(crate) async fn get_threads_route( if let Err(e) = services .rooms .pdu_metadata - .add_bundled_aggregations_to_pdu(body.identity.sender_user(), &mut pdu) + .add_bundled_aggregations_to_pdu(sender_user, &mut pdu) .await { debug_warn!("Failed to add bundled aggregations to thread: {e}"); diff --git a/src/api/client/to_device.rs b/src/api/client/to_device.rs index 3aac3b58b..1659d8c00 100644 --- a/src/api/client/to_device.rs +++ b/src/api/client/to_device.rs @@ -22,7 +22,7 @@ pub(crate) async fn send_event_to_device_route( State(services): State, body: Ruma, ) -> Result { - let sender_user = body.identity.sender_user(); + let sender_user = body.identity.expect_sender_user()?; let sender_device = body.identity.sender_device(); // Check if this is a new transaction id diff --git a/src/api/client/typing.rs b/src/api/client/typing.rs index 5bc1fa465..4cc0dc6a0 100644 --- a/src/api/client/typing.rs +++ b/src/api/client/typing.rs @@ -14,7 +14,8 @@ pub(crate) async fn create_typing_event_route( body: Ruma, ) -> Result { use create_typing_event::v3::Typing; - let sender_user = body.identity.sender_user(); + let sender_user = body.identity.expect_sender_user()?; + services .users .update_device_last_seen(sender_user, body.identity.sender_device(), ip) diff --git a/src/api/client/user_directory.rs b/src/api/client/user_directory.rs index d04dfabd8..18c02163b 100644 --- a/src/api/client/user_directory.rs +++ b/src/api/client/user_directory.rs @@ -26,7 +26,7 @@ pub(crate) async fn search_users_route( State(services): State, body: Ruma, ) -> Result { - let sender_user = body.identity.sender_user(); + let sender_user = body.identity.expect_sender_user()?; let limit = usize::try_from(body.limit) .map_or(LIMIT_DEFAULT, usize::from) .min(LIMIT_MAX); diff --git a/src/api/client/voip.rs b/src/api/client/voip.rs index 0f6c5cbdf..b214836ae 100644 --- a/src/api/client/voip.rs +++ b/src/api/client/voip.rs @@ -33,7 +33,8 @@ pub(crate) async fn turn_server_route( ) .expect("time is valid"); - let username: String = format!("{}:{}", expiry.get(), body.identity.sender_user()); + let username: String = + format!("{}:{}", expiry.get(), body.identity.expect_sender_user()?); let mut mac = HmacSha1::new_from_slice(turn_secret.as_bytes()) .expect("HMAC can take key of any size"); diff --git a/src/api/router/auth.rs b/src/api/router/auth.rs index 3cbeb6ccb..ed3dcec42 100644 --- a/src/api/router/auth.rs +++ b/src/api/router/auth.rs @@ -33,10 +33,17 @@ pub(crate) enum ClientIdentity { } impl ClientIdentity { - pub(crate) fn sender_user(&self) -> &UserId { + pub(crate) fn sender_user(&self) -> Option<&UserId> { match self { | Self::User { sender_user, .. } | Self::Appservice { sender_user, .. } => - sender_user, + Some(sender_user), + } + } + + pub(crate) fn expect_sender_user(&self) -> Result<&UserId> { + match self { + | Self::User { sender_user, .. } | Self::Appservice { sender_user, .. } => + Ok(sender_user), } } @@ -48,9 +55,11 @@ impl ClientIdentity { } pub(crate) fn expect_sender_device(&self) -> Result<&DeviceId> { - self.sender_device().ok_or_else(|| { - err!(Request(Forbidden("Appservices must masquerade to use this endpoint."))) - }) + match self { + | Self::User { sender_device, .. } => Ok(sender_device), + | Self::Appservice { .. } => + Err!(Request(Forbidden("Appservices must masquerade to use this endpoint."))), + } } pub(crate) fn appservice_info(&self) -> Option<&RegistrationInfo> {