Merge branch 'admin-hash-sign' into 'next'

feat(admin): add hash-and-sign-event command

See merge request famedly/conduit!670
This commit is contained in:
Matthias Ahouansou 2024-05-09 16:19:40 +00:00
commit 11990e7524

View file

@ -165,19 +165,15 @@ enum AdminCommand {
/// Enables incoming federation handling for a room again. /// Enables incoming federation handling for a room again.
EnableRoom { room_id: Box<RoomId> }, EnableRoom { room_id: Box<RoomId> },
/// Verify json signatures /// Sign a json object using Conduit's signing keys, putting the json in a codeblock
/// [commandbody]()
/// # ```
/// # json here
/// # ```
SignJson, SignJson,
/// Verify json signatures /// Verify json signatures, putting the json in a codeblock
/// [commandbody]()
/// # ```
/// # json here
/// # ```
VerifyJson, VerifyJson,
/// Parses a JSON object as an event then creates a hash and signs it, putting a room
/// version as an argument, and the json in a codeblock
HashAndSignEvent { room_version_id: RoomVersionId },
} }
#[derive(Debug)] #[derive(Debug)]
@ -860,6 +856,36 @@ impl Service {
) )
} }
} }
AdminCommand::HashAndSignEvent { room_version_id } => {
if body.len() > 2
// Language may be specified as part of the codeblock (e.g. "```json")
&& body[0].trim().starts_with("```")
&& body.last().unwrap().trim() == "```"
{
let string = body[1..body.len() - 1].join("\n");
match serde_json::from_str(&string) {
Ok(mut value) => {
if let Err(e) = ruma::signatures::hash_and_sign_event(
services().globals.server_name().as_str(),
services().globals.keypair(),
&mut value,
&room_version_id,
) {
RoomMessageEventContent::text_plain(format!("Invalid event: {e}"))
} else {
let json_text = serde_json::to_string_pretty(&value)
.expect("canonical json is valid json");
RoomMessageEventContent::text_plain(json_text)
}
}
Err(e) => RoomMessageEventContent::text_plain(format!("Invalid json: {e}")),
}
} else {
RoomMessageEventContent::text_plain(
"Expected code block in command body. Add --help for details.",
)
}
}
}; };
Ok(reply_message_content) Ok(reply_message_content)