mirror of
https://forgejo.ellis.link/continuwuation/continuwuity.git
synced 2025-06-27 11:34:51 +02:00
69 lines
2.2 KiB
Rust
69 lines
2.2 KiB
Rust
use conduwuit::{implement, pdu::gen_event_id_canonical_json, Err, Result};
|
|
use ruma::{
|
|
signatures::Verified, CanonicalJsonObject, CanonicalJsonValue, OwnedEventId, RoomVersionId,
|
|
};
|
|
use serde_json::value::RawValue as RawJsonValue;
|
|
|
|
#[implement(super::Service)]
|
|
pub async fn validate_and_add_event_id(
|
|
&self,
|
|
pdu: &RawJsonValue,
|
|
room_version: &RoomVersionId,
|
|
) -> Result<(OwnedEventId, CanonicalJsonObject)> {
|
|
let (event_id, mut value) = gen_event_id_canonical_json(pdu, room_version)?;
|
|
if let Err(e) = self.verify_event(&value, Some(room_version)).await {
|
|
return Err!(BadServerResponse(debug_error!(
|
|
"Event {event_id} failed verification: {e:?}"
|
|
)));
|
|
}
|
|
|
|
value.insert("event_id".into(), CanonicalJsonValue::String(event_id.as_str().into()));
|
|
|
|
Ok((event_id, value))
|
|
}
|
|
|
|
#[implement(super::Service)]
|
|
pub async fn validate_and_add_event_id_no_fetch(
|
|
&self,
|
|
pdu: &RawJsonValue,
|
|
room_version: &RoomVersionId,
|
|
) -> Result<(OwnedEventId, CanonicalJsonObject)> {
|
|
let (event_id, mut value) = gen_event_id_canonical_json(pdu, room_version)?;
|
|
if !self.required_keys_exist(&value, room_version).await {
|
|
return Err!(BadServerResponse(debug_warn!(
|
|
"Event {event_id} cannot be verified: missing keys."
|
|
)));
|
|
}
|
|
|
|
if let Err(e) = self.verify_event(&value, Some(room_version)).await {
|
|
return Err!(BadServerResponse(debug_error!(
|
|
"Event {event_id} failed verification: {e:?}"
|
|
)));
|
|
}
|
|
|
|
value.insert("event_id".into(), CanonicalJsonValue::String(event_id.as_str().into()));
|
|
|
|
Ok((event_id, value))
|
|
}
|
|
|
|
#[implement(super::Service)]
|
|
pub async fn verify_event(
|
|
&self,
|
|
event: &CanonicalJsonObject,
|
|
room_version: Option<&RoomVersionId>,
|
|
) -> Result<Verified> {
|
|
let room_version = room_version.unwrap_or(&RoomVersionId::V11);
|
|
let keys = self.get_event_keys(event, room_version).await?;
|
|
ruma::signatures::verify_event(&keys, event, room_version).map_err(Into::into)
|
|
}
|
|
|
|
#[implement(super::Service)]
|
|
pub async fn verify_json(
|
|
&self,
|
|
event: &CanonicalJsonObject,
|
|
room_version: Option<&RoomVersionId>,
|
|
) -> Result {
|
|
let room_version = room_version.unwrap_or(&RoomVersionId::V11);
|
|
let keys = self.get_event_keys(event, room_version).await?;
|
|
ruma::signatures::verify_json(&keys, event.clone()).map_err(Into::into)
|
|
}
|