]> Untitled Git - lemmy.git/blob - server/src/apub/mod.rs
Federation DB Changes.
[lemmy.git] / server / src / apub / mod.rs
1 pub mod community;
2 pub mod post;
3 pub mod puller;
4 pub mod user;
5 use crate::Settings;
6 use openssl::{pkey::PKey, rsa::Rsa};
7
8 use actix_web::body::Body;
9 use actix_web::HttpResponse;
10 use url::Url;
11
12 fn create_apub_response<T>(json: &T) -> HttpResponse<Body>
13 where
14   T: serde::ser::Serialize,
15 {
16   HttpResponse::Ok()
17     .content_type("application/activity+json")
18     .json(json)
19 }
20
21 pub enum EndpointType {
22   Community,
23   User,
24   Post,
25 }
26
27 pub fn make_apub_endpoint(endpoint_type: EndpointType, name: &str) -> Url {
28   let point = match endpoint_type {
29     EndpointType::Community => "c",
30     EndpointType::User => "u",
31     EndpointType::Post => "p",
32   };
33
34   Url::parse(&format!(
35     "{}://{}/federation/{}/{}",
36     get_apub_protocol_string(),
37     Settings::get().hostname,
38     point,
39     name
40   ))
41   .unwrap()
42 }
43
44 pub fn get_apub_protocol_string() -> &'static str {
45   if Settings::get().federation.tls_enabled {
46     "https"
47   } else {
48     "http"
49   }
50 }
51
52 pub fn gen_keypair() -> (Vec<u8>, Vec<u8>) {
53   let rsa = Rsa::generate(2048).expect("sign::gen_keypair: key generation error");
54   let pkey = PKey::from_rsa(rsa).expect("sign::gen_keypair: parsing error");
55   (
56     pkey
57       .public_key_to_pem()
58       .expect("sign::gen_keypair: public key encoding error"),
59     pkey
60       .private_key_to_pem_pkcs8()
61       .expect("sign::gen_keypair: private key encoding error"),
62   )
63 }
64
65 pub fn gen_keypair_str() -> (String, String) {
66   let (public_key, private_key) = gen_keypair();
67   (vec_bytes_to_str(public_key), vec_bytes_to_str(private_key))
68 }
69
70 fn vec_bytes_to_str(bytes: Vec<u8>) -> String {
71   String::from_utf8_lossy(&bytes).into_owned()
72 }