From 17d3d2492cad63b3c63355a44752c3e9d21bc150 Mon Sep 17 00:00:00 2001 From: Felix Date: Fri, 10 Apr 2020 15:50:40 +0200 Subject: [PATCH] Federate actor public keys --- server/src/apub/community.rs | 32 ++++++++++++++++++++------------ server/src/apub/mod.rs | 6 ++++-- server/src/apub/signatures.rs | 30 ++++++++++++++++++++++++++++++ server/src/apub/user.rs | 33 +++++++++++++++++++++------------ 4 files changed, 75 insertions(+), 26 deletions(-) create mode 100644 server/src/apub/signatures.rs diff --git a/server/src/apub/community.rs b/server/src/apub/community.rs index 076347425..76a94ed23 100644 --- a/server/src/apub/community.rs +++ b/server/src/apub/community.rs @@ -1,4 +1,5 @@ use crate::apub::fetcher::{fetch_remote_object, fetch_remote_user}; +use crate::apub::signatures::PublicKey; use crate::apub::*; use crate::db::community::{Community, CommunityForm}; use crate::db::community_view::CommunityFollowerView; @@ -54,15 +55,13 @@ pub async fn get_apub_community_list( impl Community { fn as_group(&self, conn: &PgConnection) -> Result { - let base_url = make_apub_endpoint(EndpointType::Community, &self.name); - let mut group = Group::default(); let oprops: &mut ObjectProperties = group.as_mut(); let creator = User_::read(conn, self.creator_id)?; oprops .set_context_xsd_any_uri(context())? - .set_id(base_url.to_owned())? + .set_id(self.actor_id.to_owned())? .set_name_xsd_string(self.name.to_owned())? .set_published(convert_datetime(self.published))? .set_attributed_to_xsd_any_uri(make_apub_endpoint(EndpointType::User, &creator.name))?; @@ -80,24 +79,33 @@ impl Community { actor_props .set_preferred_username(self.title.to_owned())? - .set_inbox(format!("{}/inbox", &base_url))? - .set_outbox(format!("{}/outbox", &base_url))? - .set_followers(format!("{}/followers", &base_url))?; + .set_inbox(format!("{}/inbox", &self.actor_id))? + .set_outbox(format!("{}/outbox", &self.actor_id))? + .set_followers(format!("{}/followers", &self.actor_id))?; - Ok(group.extend(actor_props)) + let public_key = PublicKey { + id: format!("{}#main-key", self.actor_id), + owner: self.actor_id.to_owned(), + public_key_pem: self.public_key.to_owned().unwrap(), + }; + + Ok(group.extend(actor_props).extend(public_key.to_ext())) } } impl CommunityForm { pub fn from_group(group: &GroupExt, conn: &PgConnection) -> Result { - let followers_uri = Url::parse(&group.extension.get_followers().unwrap().to_string())?; - let outbox_uri = Url::parse(&group.extension.get_outbox().to_string())?; + let oprops = &group.base.base.object_props; + let aprops = &group.base.extension; + let public_key: &PublicKey = &group.extension.public_key; + + let followers_uri = Url::parse(&aprops.get_followers().unwrap().to_string())?; + let outbox_uri = Url::parse(&aprops.get_outbox().to_string())?; let _outbox = fetch_remote_object::(&outbox_uri)?; let _followers = fetch_remote_object::(&followers_uri)?; - let oprops = &group.base.object_props; - let aprops = &group.extension; let apub_id = Url::parse(&oprops.get_attributed_to_xsd_any_uri().unwrap().to_string())?; let creator = fetch_remote_user(&apub_id, conn)?; + Ok(CommunityForm { name: oprops.get_name_xsd_string().unwrap().to_string(), title: aprops.get_preferred_username().unwrap().to_string(), @@ -118,7 +126,7 @@ impl CommunityForm { actor_id: oprops.get_id().unwrap().to_string(), local: false, private_key: None, - public_key: None, + public_key: Some(public_key.to_owned().public_key_pem), last_refreshed_at: Some(naive_now()), }) } diff --git a/server/src/apub/mod.rs b/server/src/apub/mod.rs index e716a3831..68839c39f 100644 --- a/server/src/apub/mod.rs +++ b/server/src/apub/mod.rs @@ -3,7 +3,9 @@ pub mod community; pub mod fetcher; pub mod inbox; pub mod post; +pub mod signatures; pub mod user; +use crate::apub::signatures::PublicKeyExtension; use crate::Settings; use activitystreams::actor::{properties::ApActorProperties, Group, Person}; use activitystreams::ext::Ext; @@ -12,8 +14,8 @@ use actix_web::HttpResponse; use openssl::{pkey::PKey, rsa::Rsa}; use url::Url; -type GroupExt = Ext; -type PersonExt = Ext; +type GroupExt = Ext, PublicKeyExtension>; +type PersonExt = Ext, PublicKeyExtension>; static APUB_JSON_CONTENT_TYPE: &str = "application/activity+json"; diff --git a/server/src/apub/signatures.rs b/server/src/apub/signatures.rs new file mode 100644 index 000000000..5bb3c5344 --- /dev/null +++ b/server/src/apub/signatures.rs @@ -0,0 +1,30 @@ +// For this example, we'll use the Extensible trait, the Extension trait, the Actor trait, and +// the Person type +use activitystreams::{actor::Actor, ext::Extension}; + +// The following is taken from here: +// https://docs.rs/activitystreams/0.5.0-alpha.17/activitystreams/ext/index.html + +#[derive(Clone, Debug, Default, serde::Deserialize, serde::Serialize)] +#[serde(rename_all = "camelCase")] +pub struct PublicKey { + pub id: String, + pub owner: String, + pub public_key_pem: String, +} + +#[derive(Clone, Debug, Default, serde::Deserialize, serde::Serialize)] +#[serde(rename_all = "camelCase")] +pub struct PublicKeyExtension { + pub public_key: PublicKey, +} + +impl PublicKey { + pub fn to_ext(&self) -> PublicKeyExtension { + PublicKeyExtension { + public_key: self.to_owned(), + } + } +} + +impl Extension for PublicKeyExtension where T: Actor {} diff --git a/server/src/apub/user.rs b/server/src/apub/user.rs index be02922f5..404a9c102 100644 --- a/server/src/apub/user.rs +++ b/server/src/apub/user.rs @@ -1,4 +1,5 @@ -use crate::apub::{create_apub_response, make_apub_endpoint, EndpointType, PersonExt}; +use crate::apub::signatures::PublicKey; +use crate::apub::{create_apub_response, PersonExt}; use crate::db::user::{UserForm, User_}; use crate::{convert_datetime, naive_now}; use activitystreams::{ @@ -25,15 +26,13 @@ pub async fn get_apub_user( info: Path, db: web::Data>>, ) -> Result, Error> { - dbg!(&info.user_name); let user = User_::find_by_email_or_username(&&db.get()?, &info.user_name)?; - let base_url = make_apub_endpoint(EndpointType::User, &user.name); let mut person = Person::default(); let oprops: &mut ObjectProperties = person.as_mut(); oprops .set_context_xsd_any_uri(context())? - .set_id(base_url.to_string())? + .set_id(user.actor_id.to_string())? .set_name_xsd_string(user.name.to_owned())? .set_published(convert_datetime(user.published))?; @@ -48,18 +47,28 @@ pub async fn get_apub_user( let mut actor_props = ApActorProperties::default(); actor_props - .set_inbox(format!("{}/inbox", &base_url))? - .set_outbox(format!("{}/outbox", &base_url))? - .set_following(format!("{}/following", &base_url))? - .set_liked(format!("{}/liked", &base_url))?; + .set_inbox(format!("{}/inbox", &user.actor_id))? + .set_outbox(format!("{}/outbox", &user.actor_id))? + .set_following(format!("{}/following", &user.actor_id))? + .set_liked(format!("{}/liked", &user.actor_id))?; - Ok(create_apub_response(&person.extend(actor_props))) + let public_key = PublicKey { + id: format!("{}#main-key", user.actor_id), + owner: user.actor_id.to_owned(), + public_key_pem: user.public_key.unwrap(), + }; + + Ok(create_apub_response( + &person.extend(actor_props).extend(public_key.to_ext()), + )) } impl UserForm { pub fn from_person(person: &PersonExt) -> Result { - let oprops = &person.base.object_props; - let aprops = &person.extension; + let oprops = &person.base.base.object_props; + let aprops = &person.base.extension; + let public_key: &PublicKey = &person.extension.public_key; + Ok(UserForm { name: oprops.get_name_xsd_string().unwrap().to_string(), preferred_username: aprops.get_preferred_username().map(|u| u.to_string()), @@ -83,7 +92,7 @@ impl UserForm { bio: oprops.get_summary_xsd_string().map(|s| s.to_string()), local: false, private_key: None, - public_key: None, + public_key: Some(public_key.to_owned().public_key_pem), last_refreshed_at: Some(naive_now()), }) }