add some accessors to Ar for common patterns

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-10-24 12:03:56 +00:00
parent 60cc07134f
commit ee92a33a4d
17 changed files with 109 additions and 147 deletions

View file

@ -18,12 +18,10 @@ use crate::Ruma;
pub(crate) async fn get_backfill_route(
State(services): State<crate::State>, body: Ruma<get_backfill::v1::Request>,
) -> Result<get_backfill::v1::Response> {
let origin = body.origin.as_ref().expect("server is authenticated");
services
.rooms
.event_handler
.acl_check(origin, &body.room_id)
.acl_check(body.origin(), &body.room_id)
.await?;
if !services
@ -33,7 +31,7 @@ pub(crate) async fn get_backfill_route(
.await && !services
.rooms
.state_cache
.server_in_room(origin, &body.room_id)
.server_in_room(body.origin(), &body.room_id)
.await
{
return Err!(Request(Forbidden("Server is not in room.")));
@ -59,6 +57,7 @@ pub(crate) async fn get_backfill_route(
.try_into()
.expect("UInt could not be converted to usize");
let origin = body.origin();
let pdus = services
.rooms
.timeline

View file

@ -13,8 +13,6 @@ use crate::Ruma;
pub(crate) async fn get_event_route(
State(services): State<crate::State>, body: Ruma<get_event::v1::Request>,
) -> Result<get_event::v1::Response> {
let origin = body.origin.as_ref().expect("server is authenticated");
let event = services
.rooms
.timeline
@ -37,7 +35,7 @@ pub(crate) async fn get_event_route(
.await && !services
.rooms
.state_cache
.server_in_room(origin, room_id)
.server_in_room(body.origin(), room_id)
.await
{
return Err!(Request(Forbidden("Server is not in room.")));
@ -46,7 +44,7 @@ pub(crate) async fn get_event_route(
if !services
.rooms
.state_accessor
.server_can_see_event(origin, room_id, &body.event_id)
.server_can_see_event(body.origin(), room_id, &body.event_id)
.await?
{
return Err!(Request(Forbidden("Server is not allowed to see event.")));

View file

@ -18,12 +18,10 @@ use crate::Ruma;
pub(crate) async fn get_event_authorization_route(
State(services): State<crate::State>, body: Ruma<get_event_authorization::v1::Request>,
) -> Result<get_event_authorization::v1::Response> {
let origin = body.origin.as_ref().expect("server is authenticated");
services
.rooms
.event_handler
.acl_check(origin, &body.room_id)
.acl_check(body.origin(), &body.room_id)
.await?;
if !services
@ -33,7 +31,7 @@ pub(crate) async fn get_event_authorization_route(
.await && !services
.rooms
.state_cache
.server_in_room(origin, &body.room_id)
.server_in_room(body.origin(), &body.room_id)
.await
{
return Err(Error::BadRequest(ErrorKind::forbidden(), "Server is not in room."));

View file

@ -13,12 +13,10 @@ use crate::Ruma;
pub(crate) async fn get_missing_events_route(
State(services): State<crate::State>, body: Ruma<get_missing_events::v1::Request>,
) -> Result<get_missing_events::v1::Response> {
let origin = body.origin.as_ref().expect("server is authenticated");
services
.rooms
.event_handler
.acl_check(origin, &body.room_id)
.acl_check(body.origin(), &body.room_id)
.await?;
if !services
@ -28,7 +26,7 @@ pub(crate) async fn get_missing_events_route(
.await && !services
.rooms
.state_cache
.server_in_room(origin, &body.room_id)
.server_in_room(body.origin(), &body.room_id)
.await
{
return Err(Error::BadRequest(ErrorKind::forbidden(), "Server is not in room"));
@ -71,7 +69,7 @@ pub(crate) async fn get_missing_events_route(
if !services
.rooms
.state_accessor
.server_can_see_event(origin, &body.room_id, &queued_events[i])
.server_can_see_event(body.origin(), &body.room_id, &queued_events[i])
.await?
{
i = i.saturating_add(1);

View file

@ -10,13 +10,11 @@ use crate::{Error, Result, Ruma};
pub(crate) async fn get_hierarchy_route(
State(services): State<crate::State>, body: Ruma<get_hierarchy::v1::Request>,
) -> Result<get_hierarchy::v1::Response> {
let origin = body.origin.as_ref().expect("server is authenticated");
if services.rooms.metadata.exists(&body.room_id).await {
services
.rooms
.spaces
.get_federation_hierarchy(&body.room_id, origin, body.suggested_only)
.get_federation_hierarchy(&body.room_id, body.origin(), body.suggested_only)
.await
} else {
Err(Error::BadRequest(ErrorKind::NotFound, "Room does not exist."))

View file

@ -18,13 +18,11 @@ pub(crate) async fn create_invite_route(
State(services): State<crate::State>, InsecureClientIp(client): InsecureClientIp,
body: Ruma<create_invite::v2::Request>,
) -> Result<create_invite::v2::Response> {
let origin = body.origin.as_ref().expect("server is authenticated");
// ACL check origin
services
.rooms
.event_handler
.acl_check(origin, &body.room_id)
.acl_check(body.origin(), &body.room_id)
.await?;
if !services
@ -55,10 +53,11 @@ pub(crate) async fn create_invite_route(
.globals
.config
.forbidden_remote_server_names
.contains(origin)
.contains(body.origin())
{
warn!(
"Received federated/remote invite from banned server {origin} for room ID {}. Rejecting.",
"Received federated/remote invite from banned server {} for room ID {}. Rejecting.",
body.origin(),
body.room_id
);

View file

@ -30,8 +30,7 @@ pub(crate) async fn create_join_event_template_route(
return Err(Error::BadRequest(ErrorKind::NotFound, "Room is unknown to this server."));
}
let origin = body.origin.as_ref().expect("server is authenticated");
if body.user_id.server_name() != origin {
if body.user_id.server_name() != body.origin() {
return Err(Error::BadRequest(
ErrorKind::InvalidParam,
"Not allowed to join on behalf of another server/user",
@ -42,19 +41,21 @@ pub(crate) async fn create_join_event_template_route(
services
.rooms
.event_handler
.acl_check(origin, &body.room_id)
.acl_check(body.origin(), &body.room_id)
.await?;
if services
.globals
.config
.forbidden_remote_server_names
.contains(origin)
.contains(body.origin())
{
warn!(
"Server {origin} for remote user {} tried joining room ID {} which has a server name that is globally \
"Server {} for remote user {} tried joining room ID {} which has a server name that is globally \
forbidden. Rejecting.",
&body.user_id, &body.room_id,
body.origin(),
&body.user_id,
&body.room_id,
);
return Err(Error::BadRequest(
ErrorKind::forbidden(),

View file

@ -19,8 +19,7 @@ pub(crate) async fn create_leave_event_template_route(
return Err(Error::BadRequest(ErrorKind::NotFound, "Room is unknown to this server."));
}
let origin = body.origin.as_ref().expect("server is authenticated");
if body.user_id.server_name() != origin {
if body.user_id.server_name() != body.origin() {
return Err(Error::BadRequest(
ErrorKind::InvalidParam,
"Not allowed to leave on behalf of another server/user",
@ -31,7 +30,7 @@ pub(crate) async fn create_leave_event_template_route(
services
.rooms
.event_handler
.acl_check(origin, &body.room_id)
.acl_check(body.origin(), &body.room_id)
.await?;
let room_version_id = services.rooms.state.get_room_version(&body.room_id).await?;

View file

@ -41,9 +41,7 @@ pub(crate) async fn send_transaction_message_route(
State(services): State<crate::State>, InsecureClientIp(client): InsecureClientIp,
body: Ruma<send_transaction_message::v1::Request>,
) -> Result<send_transaction_message::v1::Response> {
let origin = body.origin.as_ref().expect("server is authenticated");
if *origin != body.body.origin {
if body.origin() != body.body.origin {
return Err!(Request(Forbidden(
"Not allowed to send transactions on behalf of other servers"
)));
@ -67,19 +65,19 @@ pub(crate) async fn send_transaction_message_route(
edus = ?body.edus.len(),
elapsed = ?txn_start_time.elapsed(),
id = ?body.transaction_id,
origin =?body.origin,
origin =?body.origin(),
"Starting txn",
);
let resolved_map = handle_pdus(&services, &client, &body.pdus, origin, &txn_start_time).await?;
handle_edus(&services, &client, &body.edus, origin).await;
let resolved_map = handle_pdus(&services, &client, &body.pdus, body.origin(), &txn_start_time).await?;
handle_edus(&services, &client, &body.edus, body.origin()).await;
debug!(
pdus = ?body.pdus.len(),
edus = ?body.edus.len(),
elapsed = ?txn_start_time.elapsed(),
id = ?body.transaction_id,
origin =?body.origin,
origin =?body.origin(),
"Finished txn",
);

View file

@ -217,16 +217,15 @@ async fn create_join_event(
pub(crate) async fn create_join_event_v1_route(
State(services): State<crate::State>, body: Ruma<create_join_event::v1::Request>,
) -> Result<create_join_event::v1::Response> {
let origin = body.origin.as_ref().expect("server is authenticated");
if services
.globals
.config
.forbidden_remote_server_names
.contains(origin)
.contains(body.origin())
{
warn!(
"Server {origin} tried joining room ID {} who has a server name that is globally forbidden. Rejecting.",
"Server {} tried joining room ID {} who has a server name that is globally forbidden. Rejecting.",
body.origin(),
&body.room_id,
);
return Err(Error::BadRequest(
@ -243,8 +242,8 @@ pub(crate) async fn create_join_event_v1_route(
.contains(&server.to_owned())
{
warn!(
"Server {origin} tried joining room ID {} which has a server name that is globally forbidden. \
Rejecting.",
"Server {} tried joining room ID {} which has a server name that is globally forbidden. Rejecting.",
body.origin(),
&body.room_id,
);
return Err(Error::BadRequest(
@ -254,7 +253,7 @@ pub(crate) async fn create_join_event_v1_route(
}
}
let room_state = create_join_event(&services, origin, &body.room_id, &body.pdu).await?;
let room_state = create_join_event(&services, body.origin(), &body.room_id, &body.pdu).await?;
Ok(create_join_event::v1::Response {
room_state,
@ -267,13 +266,11 @@ pub(crate) async fn create_join_event_v1_route(
pub(crate) async fn create_join_event_v2_route(
State(services): State<crate::State>, body: Ruma<create_join_event::v2::Request>,
) -> Result<create_join_event::v2::Response> {
let origin = body.origin.as_ref().expect("server is authenticated");
if services
.globals
.config
.forbidden_remote_server_names
.contains(origin)
.contains(body.origin())
{
return Err(Error::BadRequest(
ErrorKind::forbidden(),
@ -299,7 +296,7 @@ pub(crate) async fn create_join_event_v2_route(
auth_chain,
state,
event,
} = create_join_event(&services, origin, &body.room_id, &body.pdu).await?;
} = create_join_event(&services, body.origin(), &body.room_id, &body.pdu).await?;
let room_state = create_join_event::v2::RoomState {
members_omitted: false,
auth_chain,

View file

@ -8,7 +8,7 @@ use ruma::{
room::member::{MembershipState, RoomMemberEventContent},
StateEventType,
},
OwnedServerName, OwnedUserId, RoomId, ServerName,
OwnedUserId, RoomId, ServerName,
};
use serde_json::value::RawValue as RawJsonValue;
@ -23,9 +23,7 @@ use crate::{
pub(crate) async fn create_leave_event_v1_route(
State(services): State<crate::State>, body: Ruma<create_leave_event::v1::Request>,
) -> Result<create_leave_event::v1::Response> {
let origin = body.origin.as_ref().expect("server is authenticated");
create_leave_event(&services, origin, &body.room_id, &body.pdu).await?;
create_leave_event(&services, body.origin(), &body.room_id, &body.pdu).await?;
Ok(create_leave_event::v1::Response::new())
}
@ -36,9 +34,7 @@ pub(crate) async fn create_leave_event_v1_route(
pub(crate) async fn create_leave_event_v2_route(
State(services): State<crate::State>, body: Ruma<create_leave_event::v2::Request>,
) -> Result<create_leave_event::v2::Response> {
let origin = body.origin.as_ref().expect("server is authenticated");
create_leave_event(&services, origin, &body.room_id, &body.pdu).await?;
create_leave_event(&services, body.origin(), &body.room_id, &body.pdu).await?;
Ok(create_leave_event::v2::Response::new())
}
@ -139,16 +135,6 @@ async fn create_leave_event(
));
}
let origin: OwnedServerName = serde_json::from_value(
serde_json::to_value(
value
.get("origin")
.ok_or_else(|| Error::BadRequest(ErrorKind::InvalidParam, "Event missing origin property."))?,
)
.expect("CanonicalJson is valid json value"),
)
.map_err(|_| Error::BadRequest(ErrorKind::InvalidParam, "origin is not a server name."))?;
let mutex_lock = services
.rooms
.event_handler
@ -159,7 +145,7 @@ async fn create_leave_event(
let pdu_id: Vec<u8> = services
.rooms
.event_handler
.handle_incoming_pdu(&origin, room_id, &event_id, value, true)
.handle_incoming_pdu(origin, room_id, &event_id, value, true)
.await?
.ok_or_else(|| Error::BadRequest(ErrorKind::InvalidParam, "Could not accept as timeline event."))?;

View file

@ -13,12 +13,10 @@ use crate::Ruma;
pub(crate) async fn get_room_state_route(
State(services): State<crate::State>, body: Ruma<get_room_state::v1::Request>,
) -> Result<get_room_state::v1::Response> {
let origin = body.origin.as_ref().expect("server is authenticated");
services
.rooms
.event_handler
.acl_check(origin, &body.room_id)
.acl_check(body.origin(), &body.room_id)
.await?;
if !services
@ -28,7 +26,7 @@ pub(crate) async fn get_room_state_route(
.await && !services
.rooms
.state_cache
.server_in_room(origin, &body.room_id)
.server_in_room(body.origin(), &body.room_id)
.await
{
return Err!(Request(Forbidden("Server is not in room.")));

View file

@ -14,12 +14,10 @@ use crate::{Result, Ruma};
pub(crate) async fn get_room_state_ids_route(
State(services): State<crate::State>, body: Ruma<get_room_state_ids::v1::Request>,
) -> Result<get_room_state_ids::v1::Response> {
let origin = body.origin.as_ref().expect("server is authenticated");
services
.rooms
.event_handler
.acl_check(origin, &body.room_id)
.acl_check(body.origin(), &body.room_id)
.await?;
if !services
@ -29,7 +27,7 @@ pub(crate) async fn get_room_state_ids_route(
.await && !services
.rooms
.state_cache
.server_in_room(origin, &body.room_id)
.server_in_room(body.origin(), &body.room_id)
.await
{
return Err!(Request(Forbidden("Server is not in room.")));

View file

@ -27,8 +27,6 @@ pub(crate) async fn get_devices_route(
));
}
let origin = body.origin.as_ref().expect("server is authenticated");
let user_id = &body.user_id;
Ok(get_devices::v1::Response {
user_id: user_id.clone(),
@ -66,12 +64,12 @@ pub(crate) async fn get_devices_route(
.await,
master_key: services
.users
.get_master_key(None, &body.user_id, &|u| u.server_name() == origin)
.get_master_key(None, &body.user_id, &|u| u.server_name() == body.origin())
.await
.ok(),
self_signing_key: services
.users
.get_self_signing_key(None, &body.user_id, &|u| u.server_name() == origin)
.get_self_signing_key(None, &body.user_id, &|u| u.server_name() == body.origin())
.await
.ok(),
})