mirror of
https://github.com/LemmyNet/lemmy.git
synced 2024-11-30 16:21:24 +00:00
65 lines
1.5 KiB
Rust
65 lines
1.5 KiB
Rust
|
use crate::settings::Settings;
|
||
|
use openssl::{pkey::PKey, rsa::Rsa};
|
||
|
use std::io::{Error, ErrorKind};
|
||
|
use url::Url;
|
||
|
|
||
|
pub struct Keypair {
|
||
|
pub private_key: String,
|
||
|
pub public_key: String,
|
||
|
}
|
||
|
|
||
|
/// Generate the asymmetric keypair for ActivityPub HTTP signatures.
|
||
|
pub fn generate_actor_keypair() -> Result<Keypair, Error> {
|
||
|
let rsa = Rsa::generate(2048)?;
|
||
|
let pkey = PKey::from_rsa(rsa)?;
|
||
|
let public_key = pkey.public_key_to_pem()?;
|
||
|
let private_key = pkey.private_key_to_pem_pkcs8()?;
|
||
|
let key_to_string = |key| match String::from_utf8(key) {
|
||
|
Ok(s) => Ok(s),
|
||
|
Err(e) => Err(Error::new(
|
||
|
ErrorKind::Other,
|
||
|
format!("Failed converting key to string: {}", e),
|
||
|
)),
|
||
|
};
|
||
|
Ok(Keypair {
|
||
|
private_key: key_to_string(private_key)?,
|
||
|
public_key: key_to_string(public_key)?,
|
||
|
})
|
||
|
}
|
||
|
|
||
|
pub enum EndpointType {
|
||
|
Community,
|
||
|
User,
|
||
|
Post,
|
||
|
Comment,
|
||
|
PrivateMessage,
|
||
|
}
|
||
|
|
||
|
pub fn get_apub_protocol_string() -> &'static str {
|
||
|
if Settings::get().federation.tls_enabled {
|
||
|
"https"
|
||
|
} else {
|
||
|
"http"
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// Generates the ActivityPub ID for a given object type and ID.
|
||
|
pub fn make_apub_endpoint(endpoint_type: EndpointType, name: &str) -> Url {
|
||
|
let point = match endpoint_type {
|
||
|
EndpointType::Community => "c",
|
||
|
EndpointType::User => "u",
|
||
|
EndpointType::Post => "post",
|
||
|
EndpointType::Comment => "comment",
|
||
|
EndpointType::PrivateMessage => "private_message",
|
||
|
};
|
||
|
|
||
|
Url::parse(&format!(
|
||
|
"{}://{}/{}/{}",
|
||
|
get_apub_protocol_string(),
|
||
|
Settings::get().hostname,
|
||
|
point,
|
||
|
name
|
||
|
))
|
||
|
.unwrap()
|
||
|
}
|