Update ruma to latest, fix unstable origin feature in ruma
This commit is contained in:
parent
49f6ab503a
commit
96dd3b2880
13 changed files with 577 additions and 234 deletions
|
@ -107,7 +107,7 @@ pub async fn get_backup_route(
|
|||
)]
|
||||
pub async fn delete_backup_route(
|
||||
db: State<'_, Database>,
|
||||
body: Ruma<delete_backup::Request>,
|
||||
body: Ruma<delete_backup::Request<'_>>,
|
||||
) -> ConduitResult<delete_backup::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
|
||||
|
@ -158,7 +158,7 @@ pub async fn add_backup_keys_route(
|
|||
)]
|
||||
pub async fn add_backup_key_sessions_route(
|
||||
db: State<'_, Database>,
|
||||
body: Ruma<add_backup_key_sessions::Request>,
|
||||
body: Ruma<add_backup_key_sessions::Request<'_>>,
|
||||
) -> ConduitResult<add_backup_key_sessions::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
|
||||
|
@ -189,7 +189,7 @@ pub async fn add_backup_key_sessions_route(
|
|||
)]
|
||||
pub async fn add_backup_key_session_route(
|
||||
db: State<'_, Database>,
|
||||
body: Ruma<add_backup_key_session::Request>,
|
||||
body: Ruma<add_backup_key_session::Request<'_>>,
|
||||
) -> ConduitResult<add_backup_key_session::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
|
||||
|
@ -232,7 +232,7 @@ pub async fn get_backup_keys_route(
|
|||
)]
|
||||
pub async fn get_backup_key_sessions_route(
|
||||
db: State<'_, Database>,
|
||||
body: Ruma<get_backup_key_sessions::Request>,
|
||||
body: Ruma<get_backup_key_sessions::Request<'_>>,
|
||||
) -> ConduitResult<get_backup_key_sessions::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
|
||||
|
@ -249,13 +249,14 @@ pub async fn get_backup_key_sessions_route(
|
|||
)]
|
||||
pub async fn get_backup_key_session_route(
|
||||
db: State<'_, Database>,
|
||||
body: Ruma<get_backup_key_session::Request>,
|
||||
body: Ruma<get_backup_key_session::Request<'_>>,
|
||||
) -> ConduitResult<get_backup_key_session::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
|
||||
let key_data =
|
||||
db.key_backups
|
||||
.get_session(&sender_user, &body.version, &body.room_id, &body.session_id)?;
|
||||
let key_data = db
|
||||
.key_backups
|
||||
.get_session(&sender_user, &body.version, &body.room_id, &body.session_id)?
|
||||
.ok_or_else(|| Error::BadDatabase("Backup key not found for this user's session"))?;
|
||||
|
||||
Ok(get_backup_key_session::Response { key_data }.into())
|
||||
}
|
||||
|
@ -266,7 +267,7 @@ pub async fn get_backup_key_session_route(
|
|||
)]
|
||||
pub async fn delete_backup_keys_route(
|
||||
db: State<'_, Database>,
|
||||
body: Ruma<delete_backup_keys::Request>,
|
||||
body: Ruma<delete_backup_keys::Request<'_>>,
|
||||
) -> ConduitResult<delete_backup_keys::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
|
||||
|
@ -288,7 +289,7 @@ pub async fn delete_backup_keys_route(
|
|||
)]
|
||||
pub async fn delete_backup_key_sessions_route(
|
||||
db: State<'_, Database>,
|
||||
body: Ruma<delete_backup_key_sessions::Request>,
|
||||
body: Ruma<delete_backup_key_sessions::Request<'_>>,
|
||||
) -> ConduitResult<delete_backup_key_sessions::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
|
||||
|
@ -310,7 +311,7 @@ pub async fn delete_backup_key_sessions_route(
|
|||
)]
|
||||
pub async fn delete_backup_key_session_route(
|
||||
db: State<'_, Database>,
|
||||
body: Ruma<delete_backup_key_session::Request>,
|
||||
body: Ruma<delete_backup_key_session::Request<'_>>,
|
||||
) -> ConduitResult<delete_backup_key_session::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
|
||||
|
|
|
@ -22,11 +22,11 @@ pub async fn get_capabilities_route() -> ConduitResult<get_capabilities::Respons
|
|||
|
||||
Ok(get_capabilities::Response {
|
||||
capabilities: get_capabilities::Capabilities {
|
||||
change_password: None, // None means it is possible
|
||||
room_versions: Some(get_capabilities::RoomVersionsCapability {
|
||||
default: "6".to_owned(),
|
||||
change_password: get_capabilities::ChangePasswordCapability::default(), // enabled by default
|
||||
room_versions: get_capabilities::RoomVersionsCapability {
|
||||
default: RoomVersionId::Version6,
|
||||
available,
|
||||
}),
|
||||
},
|
||||
custom_capabilities: BTreeMap::new(),
|
||||
},
|
||||
}
|
||||
|
|
|
@ -9,10 +9,10 @@ pub async fn get_filter_route() -> ConduitResult<get_filter::Response> {
|
|||
// TODO
|
||||
Ok(get_filter::Response::new(filter::IncomingFilterDefinition {
|
||||
event_fields: None,
|
||||
event_format: None,
|
||||
account_data: None,
|
||||
room: None,
|
||||
presence: None,
|
||||
event_format: filter::EventFormat::default(),
|
||||
account_data: filter::IncomingFilter::default(),
|
||||
room: filter::IncomingRoomFilter::default(),
|
||||
presence: filter::IncomingFilter::default(),
|
||||
})
|
||||
.into())
|
||||
}
|
||||
|
|
|
@ -11,7 +11,7 @@ use ruma::{
|
|||
uiaa::{AuthFlow, UiaaInfo},
|
||||
},
|
||||
},
|
||||
encryption::IncomingUnsignedDeviceInfo,
|
||||
encryption::UnsignedDeviceInfo,
|
||||
};
|
||||
use std::collections::{BTreeMap, HashSet};
|
||||
|
||||
|
@ -24,7 +24,7 @@ use rocket::{get, post};
|
|||
)]
|
||||
pub async fn upload_keys_route(
|
||||
db: State<'_, Database>,
|
||||
body: Ruma<upload_keys::Request<'_>>,
|
||||
body: Ruma<upload_keys::Request>,
|
||||
) -> ConduitResult<upload_keys::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");
|
||||
|
@ -94,7 +94,7 @@ pub async fn get_keys_route(
|
|||
Error::bad_database("all_device_keys contained nonexistent device.")
|
||||
})?;
|
||||
|
||||
keys.unsigned = IncomingUnsignedDeviceInfo {
|
||||
keys.unsigned = UnsignedDeviceInfo {
|
||||
device_display_name: metadata.display_name,
|
||||
};
|
||||
|
||||
|
@ -113,7 +113,7 @@ pub async fn get_keys_route(
|
|||
),
|
||||
)?;
|
||||
|
||||
keys.unsigned = IncomingUnsignedDeviceInfo {
|
||||
keys.unsigned = UnsignedDeviceInfo {
|
||||
device_display_name: metadata.display_name,
|
||||
};
|
||||
|
||||
|
|
|
@ -39,7 +39,7 @@ pub async fn create_content_route(
|
|||
db.media.create(
|
||||
mxc.clone(),
|
||||
&body.filename.as_deref(),
|
||||
&body.content_type,
|
||||
body.content_type.as_deref().unwrap_or("img"), // TODO this is now optional handle
|
||||
&body.file,
|
||||
)?;
|
||||
|
||||
|
@ -66,8 +66,8 @@ pub async fn get_content_route(
|
|||
{
|
||||
Ok(get_content::Response {
|
||||
file,
|
||||
content_type,
|
||||
content_disposition: filename.unwrap_or_default(), // TODO: Spec says this should be optional
|
||||
content_type: Some(content_type),
|
||||
content_disposition: filename,
|
||||
}
|
||||
.into())
|
||||
} else if &*body.server_name != db.globals.server_name() && body.allow_remote {
|
||||
|
@ -84,8 +84,11 @@ pub async fn get_content_route(
|
|||
|
||||
db.media.create(
|
||||
mxc,
|
||||
&Some(&get_content_response.content_disposition),
|
||||
&get_content_response.content_type,
|
||||
&get_content_response.content_disposition.as_deref(),
|
||||
get_content_response // TODO this is now optional handle
|
||||
.content_type
|
||||
.as_deref()
|
||||
.unwrap_or("img"),
|
||||
&get_content_response.file,
|
||||
)?;
|
||||
|
||||
|
@ -116,7 +119,11 @@ pub async fn get_content_thumbnail_route(
|
|||
.try_into()
|
||||
.map_err(|_| Error::BadRequest(ErrorKind::InvalidParam, "Width is invalid."))?,
|
||||
)? {
|
||||
Ok(get_content_thumbnail::Response { file, content_type }.into())
|
||||
Ok(get_content_thumbnail::Response {
|
||||
file,
|
||||
content_type: Some(content_type),
|
||||
}
|
||||
.into())
|
||||
} else if &*body.server_name != db.globals.server_name() && body.allow_remote {
|
||||
let get_thumbnail_response = server_server::send_request(
|
||||
&db.globals,
|
||||
|
@ -135,7 +142,10 @@ pub async fn get_content_thumbnail_route(
|
|||
db.media.upload_thumbnail(
|
||||
mxc,
|
||||
&None,
|
||||
&get_thumbnail_response.content_type,
|
||||
get_thumbnail_response
|
||||
.content_type
|
||||
.as_deref()
|
||||
.unwrap_or("img"), // TODO now optional, deal with it somehow
|
||||
body.width.try_into().expect("all UInts are valid u32s"),
|
||||
body.height.try_into().expect("all UInts are valid u32s"),
|
||||
&get_thumbnail_response.file,
|
||||
|
|
|
@ -23,7 +23,11 @@ use ruma::{
|
|||
};
|
||||
use state_res::StateEvent;
|
||||
use std::{
|
||||
collections::BTreeMap, collections::HashMap, collections::HashSet, convert::TryFrom, iter,
|
||||
collections::BTreeMap,
|
||||
collections::HashMap,
|
||||
collections::HashSet,
|
||||
convert::{TryFrom, TryInto},
|
||||
iter,
|
||||
sync::Arc,
|
||||
};
|
||||
|
||||
|
@ -509,31 +513,42 @@ async fn join_room_by_id_helper(
|
|||
.expect("event is valid, we just created it"),
|
||||
);
|
||||
|
||||
// TODO fixup CanonicalJsonValue
|
||||
// use that instead of serde_json::Map... maybe?
|
||||
let mut canon_json_stub =
|
||||
serde_json::from_value(join_event_stub_value).expect("json Value is canonical JSON");
|
||||
// Generate event id
|
||||
let event_id = EventId::try_from(&*format!(
|
||||
"${}",
|
||||
ruma::signatures::reference_hash(&join_event_stub_value)
|
||||
ruma::signatures::reference_hash(&canon_json_stub, &RoomVersionId::Version6)
|
||||
.expect("ruma can calculate reference hashes")
|
||||
))
|
||||
.expect("ruma's reference hashes are valid event ids");
|
||||
|
||||
// We don't leave the event id into the pdu because that's only allowed in v1 or v2 rooms
|
||||
let join_event_stub = join_event_stub_value.as_object_mut().unwrap();
|
||||
join_event_stub.remove("event_id");
|
||||
// let join_event_stub = join_event_stub_value.as_object_mut().unwrap();
|
||||
// join_event_stub.remove("event_id");
|
||||
|
||||
canon_json_stub.remove("event_id");
|
||||
|
||||
ruma::signatures::hash_and_sign_event(
|
||||
db.globals.server_name().as_str(),
|
||||
db.globals.keypair(),
|
||||
&mut join_event_stub_value,
|
||||
&mut canon_json_stub,
|
||||
&RoomVersionId::Version6,
|
||||
)
|
||||
.expect("event is valid, we just created it");
|
||||
|
||||
// Add event_id back
|
||||
let join_event_stub = join_event_stub_value.as_object_mut().unwrap();
|
||||
join_event_stub.insert("event_id".to_owned(), event_id.to_string().into());
|
||||
canon_json_stub.insert(
|
||||
"event_id".to_owned(),
|
||||
serde_json::json!(event_id)
|
||||
.try_into()
|
||||
.expect("EventId is a valid CanonicalJsonValue"),
|
||||
);
|
||||
|
||||
// It has enough fields to be called a proper event now
|
||||
let join_event = join_event_stub_value;
|
||||
let join_event = canon_json_stub;
|
||||
|
||||
let send_join_response = server_server::send_request(
|
||||
&db.globals,
|
||||
|
@ -541,27 +556,31 @@ async fn join_room_by_id_helper(
|
|||
federation::membership::create_join_event::v2::Request {
|
||||
room_id,
|
||||
event_id: &event_id,
|
||||
pdu_stub: PduEvent::convert_to_outgoing_federation_event(join_event.clone()),
|
||||
pdu_stub: PduEvent::convert_to_outgoing_federation_event(
|
||||
serde_json::to_value(&join_event)
|
||||
.expect("we just validated and ser/de this event"),
|
||||
),
|
||||
},
|
||||
)
|
||||
.await?;
|
||||
|
||||
let add_event_id = |pdu: &Raw<Pdu>| {
|
||||
let add_event_id = |pdu: &Raw<Pdu>| -> Result<(EventId, serde_json::Value)> {
|
||||
let mut value = serde_json::from_str(pdu.json().get())
|
||||
.expect("converting raw jsons to values always works");
|
||||
let event_id = EventId::try_from(&*format!(
|
||||
"${}",
|
||||
ruma::signatures::reference_hash(&value)
|
||||
ruma::signatures::reference_hash(&value, &RoomVersionId::Version6)
|
||||
.expect("ruma can calculate reference hashes")
|
||||
))
|
||||
.expect("ruma's reference hashes are valid event ids");
|
||||
|
||||
value
|
||||
.as_object_mut()
|
||||
.ok_or_else(|| Error::BadServerResponse("PDU is not an object."))?
|
||||
.insert("event_id".to_owned(), event_id.to_string().into());
|
||||
value.insert(
|
||||
"event_id".to_owned(),
|
||||
serde_json::from_value(serde_json::json!(event_id))
|
||||
.expect("a valid EventId can be converted to CanonicalJsonValue"),
|
||||
);
|
||||
|
||||
Ok((event_id, value))
|
||||
Ok((event_id, serde_json::json!(value))) // TODO CanonicalJsonValue fixup?
|
||||
};
|
||||
|
||||
let room_state = send_join_response.room_state.state.iter().map(add_event_id);
|
||||
|
@ -580,7 +599,10 @@ async fn join_room_by_id_helper(
|
|||
|
||||
let mut event_map = room_state
|
||||
.chain(auth_chain)
|
||||
.chain(iter::once(Ok((event_id, join_event)))) // Add join event we just created
|
||||
.chain(iter::once(Ok((
|
||||
event_id,
|
||||
serde_json::to_value(join_event).unwrap(),
|
||||
)))) // Add join event we just created
|
||||
.map(|r| {
|
||||
let (event_id, value) = r?;
|
||||
serde_json::from_value::<StateEvent>(value.clone())
|
||||
|
@ -595,7 +617,7 @@ async fn join_room_by_id_helper(
|
|||
let control_events = event_map
|
||||
.values()
|
||||
.filter(|pdu| pdu.is_power_event())
|
||||
.map(|pdu| pdu.event_id().clone())
|
||||
.map(|pdu| pdu.event_id())
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
// These events are not guaranteed to be sorted but they are resolved according to spec
|
||||
|
|
|
@ -99,14 +99,14 @@ pub async fn send_state_event_for_empty_key_route(
|
|||
)]
|
||||
pub async fn get_state_events_route(
|
||||
db: State<'_, Database>,
|
||||
body: Ruma<get_state_events::Request>,
|
||||
body: Ruma<get_state_events::Request<'_>>,
|
||||
) -> ConduitResult<get_state_events::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
|
||||
// Users not in the room should not be able to access the state unless history_visibility is
|
||||
// WorldReadable
|
||||
if !db.rooms.is_joined(sender_user, &body.room_id)? {
|
||||
if !matches!(
|
||||
if !db.rooms.is_joined(sender_user, &body.room_id)?
|
||||
&& !matches!(
|
||||
db.rooms
|
||||
.room_state_get(&body.room_id, &EventType::RoomHistoryVisibility, "")?
|
||||
.map(|(_, event)| {
|
||||
|
@ -119,12 +119,12 @@ pub async fn get_state_events_route(
|
|||
.map(|e| e.history_visibility)
|
||||
}),
|
||||
Some(Ok(HistoryVisibility::WorldReadable))
|
||||
) {
|
||||
return Err(Error::BadRequest(
|
||||
ErrorKind::Forbidden,
|
||||
"You don't have permission to view the room state.",
|
||||
));
|
||||
}
|
||||
)
|
||||
{
|
||||
return Err(Error::BadRequest(
|
||||
ErrorKind::Forbidden,
|
||||
"You don't have permission to view the room state.",
|
||||
));
|
||||
}
|
||||
|
||||
Ok(get_state_events::Response {
|
||||
|
@ -144,14 +144,14 @@ pub async fn get_state_events_route(
|
|||
)]
|
||||
pub async fn get_state_events_for_key_route(
|
||||
db: State<'_, Database>,
|
||||
body: Ruma<get_state_events_for_key::Request>,
|
||||
body: Ruma<get_state_events_for_key::Request<'_>>,
|
||||
) -> ConduitResult<get_state_events_for_key::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
|
||||
// Users not in the room should not be able to access the state unless history_visibility is
|
||||
// WorldReadable
|
||||
if !db.rooms.is_joined(sender_user, &body.room_id)? {
|
||||
if !matches!(
|
||||
if !db.rooms.is_joined(sender_user, &body.room_id)?
|
||||
&& !matches!(
|
||||
db.rooms
|
||||
.room_state_get(&body.room_id, &EventType::RoomHistoryVisibility, "")?
|
||||
.map(|(_, event)| {
|
||||
|
@ -164,12 +164,12 @@ pub async fn get_state_events_for_key_route(
|
|||
.map(|e| e.history_visibility)
|
||||
}),
|
||||
Some(Ok(HistoryVisibility::WorldReadable))
|
||||
) {
|
||||
return Err(Error::BadRequest(
|
||||
ErrorKind::Forbidden,
|
||||
"You don't have permission to view the room state.",
|
||||
));
|
||||
}
|
||||
)
|
||||
{
|
||||
return Err(Error::BadRequest(
|
||||
ErrorKind::Forbidden,
|
||||
"You don't have permission to view the room state.",
|
||||
));
|
||||
}
|
||||
|
||||
let event = db
|
||||
|
@ -194,14 +194,14 @@ pub async fn get_state_events_for_key_route(
|
|||
)]
|
||||
pub async fn get_state_events_for_empty_key_route(
|
||||
db: State<'_, Database>,
|
||||
body: Ruma<get_state_events_for_empty_key::Request>,
|
||||
body: Ruma<get_state_events_for_empty_key::Request<'_>>,
|
||||
) -> ConduitResult<get_state_events_for_empty_key::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
|
||||
// Users not in the room should not be able to access the state unless history_visibility is
|
||||
// WorldReadable
|
||||
if !db.rooms.is_joined(sender_user, &body.room_id)? {
|
||||
if !matches!(
|
||||
if !db.rooms.is_joined(sender_user, &body.room_id)?
|
||||
&& !matches!(
|
||||
db.rooms
|
||||
.room_state_get(&body.room_id, &EventType::RoomHistoryVisibility, "")?
|
||||
.map(|(_, event)| {
|
||||
|
@ -214,12 +214,12 @@ pub async fn get_state_events_for_empty_key_route(
|
|||
.map(|e| e.history_visibility)
|
||||
}),
|
||||
Some(Ok(HistoryVisibility::WorldReadable))
|
||||
) {
|
||||
return Err(Error::BadRequest(
|
||||
ErrorKind::Forbidden,
|
||||
"You don't have permission to view the room state.",
|
||||
));
|
||||
}
|
||||
)
|
||||
{
|
||||
return Err(Error::BadRequest(
|
||||
ErrorKind::Forbidden,
|
||||
"You don't have permission to view the room state.",
|
||||
));
|
||||
}
|
||||
|
||||
let event = db
|
||||
|
|
|
@ -15,7 +15,7 @@ use ruma::{
|
|||
},
|
||||
EventType,
|
||||
},
|
||||
EventId, Raw, RoomAliasId, RoomId, ServerName, UserId,
|
||||
EventId, Raw, RoomAliasId, RoomId, RoomVersionId, ServerName, UserId,
|
||||
};
|
||||
use sled::IVec;
|
||||
use state_res::{event_auth, Error as StateError, Requester, StateEvent, StateMap, StateStore};
|
||||
|
@ -196,7 +196,7 @@ impl Rooms {
|
|||
Ok(self.pduid_statehash.get(pdu_id)?)
|
||||
}
|
||||
|
||||
/// Returns the last state hash key added to the db.
|
||||
/// Returns the last state hash key added to the db for the given room.
|
||||
pub fn current_state_hash(&self, room_id: &RoomId) -> Result<Option<StateHashId>> {
|
||||
Ok(self.roomid_statehash.get(room_id.as_bytes())?)
|
||||
}
|
||||
|
@ -249,7 +249,7 @@ impl Rooms {
|
|||
.is_some())
|
||||
}
|
||||
|
||||
/// Returns the full room state.
|
||||
/// Force the creation of a new StateHash and insert it into the db.
|
||||
pub fn force_state(
|
||||
&self,
|
||||
room_id: &RoomId,
|
||||
|
@ -436,6 +436,7 @@ impl Rooms {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
/// Creates a new persisted data unit and adds it to a room.
|
||||
pub fn append_pdu(
|
||||
&self,
|
||||
|
@ -687,7 +688,7 @@ impl Rooms {
|
|||
}
|
||||
EventType::RoomMember => {
|
||||
let prev_event = self
|
||||
.get_pdu(prev_events.iter().next().ok_or(Error::BadRequest(
|
||||
.get_pdu(prev_events.get(0).ok_or(Error::BadRequest(
|
||||
ErrorKind::Unknown,
|
||||
"Membership can't be the first event",
|
||||
))?)?
|
||||
|
@ -703,7 +704,7 @@ impl Rooms {
|
|||
sender: &sender,
|
||||
},
|
||||
prev_event,
|
||||
None,
|
||||
None, // TODO: third party invite
|
||||
&auth_events
|
||||
.iter()
|
||||
.map(|((ty, key), pdu)| {
|
||||
|
@ -761,7 +762,7 @@ impl Rooms {
|
|||
}
|
||||
|
||||
let mut pdu = PduEvent {
|
||||
event_id: EventId::try_from("$thiswillbefilledinlater").expect("we know this is valid"),
|
||||
event_id: ruma::event_id!("$thiswillbefilledinlater"),
|
||||
room_id: room_id.clone(),
|
||||
sender: sender.clone(),
|
||||
origin_server_ts: utils::millis_since_unix_epoch()
|
||||
|
@ -787,37 +788,42 @@ impl Rooms {
|
|||
};
|
||||
|
||||
// Hash and sign
|
||||
let mut pdu_json = serde_json::to_value(&pdu).expect("event is valid, we just created it");
|
||||
pdu_json
|
||||
.as_object_mut()
|
||||
.expect("json is object")
|
||||
.remove("event_id");
|
||||
let mut pdu_json: BTreeMap<String, ruma::serde::CanonicalJsonValue> =
|
||||
serde_json::from_value(serde_json::json!(&pdu))
|
||||
.expect("event is valid, we just created it");
|
||||
|
||||
pdu_json.remove("event_id");
|
||||
|
||||
// Add origin because synapse likes that (and it's required in the spec)
|
||||
pdu_json
|
||||
.as_object_mut()
|
||||
.expect("json is object")
|
||||
.insert("origin".to_owned(), globals.server_name().as_str().into());
|
||||
pdu_json.insert(
|
||||
"origin".to_owned(),
|
||||
serde_json::json!(globals.server_name())
|
||||
.try_into()
|
||||
.expect("server name is a valid CanonicalJsonValue"),
|
||||
);
|
||||
|
||||
ruma::signatures::hash_and_sign_event(
|
||||
globals.server_name().as_str(),
|
||||
globals.keypair(),
|
||||
&mut pdu_json,
|
||||
&RoomVersionId::Version6,
|
||||
)
|
||||
.expect("event is valid, we just created it");
|
||||
|
||||
// Generate event id
|
||||
pdu.event_id = EventId::try_from(&*format!(
|
||||
"${}",
|
||||
ruma::signatures::reference_hash(&pdu_json)
|
||||
ruma::signatures::reference_hash(&pdu_json, &RoomVersionId::Version6)
|
||||
.expect("ruma can calculate reference hashes")
|
||||
))
|
||||
.expect("ruma's reference hashes are valid event ids");
|
||||
|
||||
pdu_json
|
||||
.as_object_mut()
|
||||
.expect("json is object")
|
||||
.insert("event_id".to_owned(), pdu.event_id.to_string().into());
|
||||
pdu_json.insert(
|
||||
"event_id".to_owned(),
|
||||
serde_json::json!(pdu.event_id)
|
||||
.try_into()
|
||||
.expect("EventId is a valid CanonicalJsonValue"),
|
||||
);
|
||||
|
||||
// Increment the last index and use that
|
||||
// This is also the next_batch/since value
|
||||
|
@ -832,7 +838,7 @@ impl Rooms {
|
|||
|
||||
self.append_pdu(
|
||||
&pdu,
|
||||
&pdu_json,
|
||||
&serde_json::json!(pdu_json), // TODO fixup CanonicalJsonValue
|
||||
count,
|
||||
pdu_id.clone().into(),
|
||||
globals,
|
||||
|
|
|
@ -8,7 +8,7 @@ use ruma::{
|
|||
keys::{CrossSigningKey, OneTimeKey},
|
||||
},
|
||||
},
|
||||
encryption::IncomingDeviceKeys,
|
||||
encryption::DeviceKeys,
|
||||
events::{AnyToDeviceEvent, EventType},
|
||||
DeviceId, DeviceKeyAlgorithm, DeviceKeyId, Raw, UserId,
|
||||
};
|
||||
|
@ -401,7 +401,7 @@ impl Users {
|
|||
&self,
|
||||
user_id: &UserId,
|
||||
device_id: &DeviceId,
|
||||
device_keys: &IncomingDeviceKeys,
|
||||
device_keys: &DeviceKeys,
|
||||
rooms: &super::rooms::Rooms,
|
||||
globals: &super::globals::Globals,
|
||||
) -> Result<()> {
|
||||
|
@ -631,7 +631,7 @@ impl Users {
|
|||
&self,
|
||||
user_id: &UserId,
|
||||
device_id: &DeviceId,
|
||||
) -> Result<Option<IncomingDeviceKeys>> {
|
||||
) -> Result<Option<DeviceKeys>> {
|
||||
let mut key = user_id.to_string().as_bytes().to_vec();
|
||||
key.push(0xff);
|
||||
key.extend_from_slice(device_id.as_bytes());
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
use crate::Error;
|
||||
use ruma::{
|
||||
api::{Outgoing, OutgoingRequest},
|
||||
api::{AuthScheme, OutgoingRequest},
|
||||
identifiers::{DeviceId, UserId},
|
||||
Outgoing,
|
||||
};
|
||||
use std::{convert::TryFrom, convert::TryInto, ops::Deref};
|
||||
|
||||
|
@ -61,28 +62,30 @@ where
|
|||
.await
|
||||
.expect("database was loaded");
|
||||
|
||||
let (sender_user, sender_device) = if T::METADATA.requires_authentication {
|
||||
// Get token from header or query value
|
||||
let token = match request
|
||||
.headers()
|
||||
.get_one("Authorization")
|
||||
.map(|s| s[7..].to_owned()) // Split off "Bearer "
|
||||
.or_else(|| request.get_query_value("access_token").and_then(|r| r.ok()))
|
||||
{
|
||||
// TODO: M_MISSING_TOKEN
|
||||
None => return Failure((Status::Unauthorized, ())),
|
||||
Some(token) => token,
|
||||
};
|
||||
let (sender_user, sender_device) =
|
||||
// TODO: Do we need to matches! anything else here? ServerSignatures
|
||||
if matches!(T::METADATA.authentication, AuthScheme::AccessToken | AuthScheme::QueryOnlyAccessToken) {
|
||||
// Get token from header or query value
|
||||
let token = match request
|
||||
.headers()
|
||||
.get_one("Authorization")
|
||||
.map(|s| s[7..].to_owned()) // Split off "Bearer "
|
||||
.or_else(|| request.get_query_value("access_token").and_then(|r| r.ok()))
|
||||
{
|
||||
// TODO: M_MISSING_TOKEN
|
||||
None => return Failure((Status::Unauthorized, ())),
|
||||
Some(token) => token,
|
||||
};
|
||||
|
||||
// Check if token is valid
|
||||
match db.users.find_from_token(&token).unwrap() {
|
||||
// TODO: M_UNKNOWN_TOKEN
|
||||
None => return Failure((Status::Unauthorized, ())),
|
||||
Some((user_id, device_id)) => (Some(user_id), Some(device_id.into())),
|
||||
}
|
||||
} else {
|
||||
(None, None)
|
||||
};
|
||||
// Check if token is valid
|
||||
match db.users.find_from_token(&token).unwrap() {
|
||||
// TODO: M_UNKNOWN_TOKEN
|
||||
None => return Failure((Status::Unauthorized, ())),
|
||||
Some((user_id, device_id)) => (Some(user_id), Some(device_id.into())),
|
||||
}
|
||||
} else {
|
||||
(None, None)
|
||||
};
|
||||
|
||||
let mut http_request = http::Request::builder()
|
||||
.uri(request.uri().to_string())
|
||||
|
|
|
@ -17,11 +17,11 @@ use ruma::{
|
|||
OutgoingRequest,
|
||||
},
|
||||
directory::{IncomingFilter, IncomingRoomNetwork},
|
||||
EventId, ServerName,
|
||||
EventId, RoomVersionId, ServerName,
|
||||
};
|
||||
use std::{
|
||||
collections::BTreeMap,
|
||||
convert::TryFrom,
|
||||
convert::{TryFrom, TryInto},
|
||||
fmt::Debug,
|
||||
time::{Duration, SystemTime},
|
||||
};
|
||||
|
@ -95,7 +95,7 @@ where
|
|||
let mut http_request = request
|
||||
.try_into_http_request(&actual_destination, Some(""))
|
||||
.map_err(|e| {
|
||||
warn!("{}: {}", actual_destination, e);
|
||||
warn!("failed to find destination {}: {}", actual_destination, e);
|
||||
Error::BadServerResponse("Invalid destination")
|
||||
})?;
|
||||
|
||||
|
@ -122,7 +122,9 @@ where
|
|||
request_map.insert("origin".to_owned(), globals.server_name().as_str().into());
|
||||
request_map.insert("destination".to_owned(), destination.as_str().into());
|
||||
|
||||
let mut request_json = request_map.into();
|
||||
let mut request_json =
|
||||
serde_json::from_value(request_map.into()).expect("valid JSON is valid BTreeMap");
|
||||
|
||||
ruma::signatures::sign_json(
|
||||
globals.server_name().as_str(),
|
||||
globals.keypair(),
|
||||
|
@ -130,6 +132,9 @@ where
|
|||
)
|
||||
.expect("our request json is what ruma expects");
|
||||
|
||||
let request_json: serde_json::Map<String, serde_json::Value> =
|
||||
serde_json::from_slice(&serde_json::to_vec(&request_json).unwrap()).unwrap();
|
||||
|
||||
let signatures = request_json["signatures"]
|
||||
.as_object()
|
||||
.unwrap()
|
||||
|
@ -234,7 +239,9 @@ pub fn get_server_keys(db: State<'_, Database>) -> Json<String> {
|
|||
|
||||
let mut verify_keys = BTreeMap::new();
|
||||
verify_keys.insert(
|
||||
format!("ed25519:{}", db.globals.keypair().version()),
|
||||
format!("ed25519:{}", db.globals.keypair().version())
|
||||
.try_into()
|
||||
.expect("DB stores valid ServerKeyId's"),
|
||||
VerifyKey {
|
||||
key: base64::encode_config(db.globals.keypair().public_key(), base64::STANDARD_NO_PAD),
|
||||
},
|
||||
|
@ -259,7 +266,7 @@ pub fn get_server_keys(db: State<'_, Database>) -> Json<String> {
|
|||
&mut response,
|
||||
)
|
||||
.unwrap();
|
||||
Json(response.to_string())
|
||||
Json(ruma::serde::to_canonical_json_string(&response).expect("JSON is canonical"))
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "conduit_bin", get("/_matrix/key/v2/server/<_>"))]
|
||||
|
@ -365,7 +372,7 @@ pub async fn get_public_rooms_route(
|
|||
feature = "conduit_bin",
|
||||
put("/_matrix/federation/v1/send/<_>", data = "<body>")
|
||||
)]
|
||||
pub fn send_transaction_message_route<'a>(
|
||||
pub async fn send_transaction_message_route<'a>(
|
||||
db: State<'a, Database>,
|
||||
body: Ruma<send_transaction_message::v1::Request<'_>>,
|
||||
) -> ConduitResult<send_transaction_message::v1::Response> {
|
||||
|
@ -451,7 +458,7 @@ pub fn get_missing_events_route<'a>(
|
|||
)
|
||||
.map_err(|_| Error::bad_database("Invalid prev_events content in pdu in db."))?,
|
||||
);
|
||||
events.push(PduEvent::convert_to_outgoing_federation_event(pdu));
|
||||
events.push(serde_json::from_value(pdu).expect("Raw<..> is always valid"));
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue