From eeae9e3ad7c79997991b200b3ceb7a832774c145 Mon Sep 17 00:00:00 2001 From: silverpill Date: Tue, 7 Feb 2023 18:28:19 +0000 Subject: [PATCH] Remove dependency on activitypub module from config::main --- src/activitypub/actors/types.rs | 14 +++++++++----- src/activitypub/constants.rs | 2 -- src/activitypub/deliverer.rs | 15 ++++++--------- src/activitypub/fetcher/fetchers.rs | 5 ++++- src/activitypub/identifiers.rs | 6 ++++++ src/config/main.rs | 10 ---------- 6 files changed, 25 insertions(+), 27 deletions(-) diff --git a/src/activitypub/actors/types.rs b/src/activitypub/actors/types.rs index 7fa73f2..c4dc2ad 100644 --- a/src/activitypub/actors/types.rs +++ b/src/activitypub/actors/types.rs @@ -8,11 +8,15 @@ use serde_json::{json, Value}; use crate::activitypub::{ constants::{ - ACTOR_KEY_SUFFIX, AP_CONTEXT, W3ID_SECURITY_CONTEXT, }, - identifiers::{local_actor_id, LocalActorCollection}, + identifiers::{ + local_actor_id, + local_actor_key_id, + local_instance_actor_id, + LocalActorCollection, + }, receiver::parse_property_value, vocabulary::{IDENTITY_PROOF, IMAGE, LINK, PERSON, PROPERTY_VALUE, SERVICE}, }; @@ -259,7 +263,7 @@ pub fn get_local_actor( let private_key = deserialize_private_key(&user.private_key)?; let public_key_pem = get_public_key_pem(&private_key)?; let public_key = PublicKey { - id: format!("{}{}", actor_id, ACTOR_KEY_SUFFIX), + id: local_actor_key_id(&actor_id), owner: actor_id.clone(), public_key_pem: public_key_pem, }; @@ -332,12 +336,12 @@ pub fn get_local_actor( pub fn get_instance_actor( instance: &Instance, ) -> Result { - let actor_id = instance.actor_id(); + let actor_id = local_instance_actor_id(&instance.url()); let actor_inbox = LocalActorCollection::Inbox.of(&actor_id); let actor_outbox = LocalActorCollection::Outbox.of(&actor_id); let public_key_pem = get_public_key_pem(&instance.actor_key)?; let public_key = PublicKey { - id: instance.actor_key_id(), + id: local_actor_key_id(&actor_id), owner: actor_id.clone(), public_key_pem: public_key_pem, }; diff --git a/src/activitypub/constants.rs b/src/activitypub/constants.rs index 4f09ead..01163b4 100644 --- a/src/activitypub/constants.rs +++ b/src/activitypub/constants.rs @@ -7,5 +7,3 @@ pub const AP_CONTEXT: &str = "https://www.w3.org/ns/activitystreams"; pub const AP_PUBLIC: &str = "https://www.w3.org/ns/activitystreams#Public"; pub const W3ID_SECURITY_CONTEXT: &str = "https://w3id.org/security/v1"; pub const W3ID_DATA_INTEGRITY_CONTEXT: &str = "https://w3id.org/security/data-integrity/v1"; - -pub const ACTOR_KEY_SUFFIX: &str = "#main-key"; diff --git a/src/activitypub/deliverer.rs b/src/activitypub/deliverer.rs index ac809b6..83ea495 100644 --- a/src/activitypub/deliverer.rs +++ b/src/activitypub/deliverer.rs @@ -30,8 +30,8 @@ use crate::models::{ }; use crate::utils::crypto_rsa::deserialize_private_key; use super::actors::types::Actor; -use super::constants::{AP_MEDIA_TYPE, ACTOR_KEY_SUFFIX}; -use super::identifiers::local_actor_id; +use super::constants::AP_MEDIA_TYPE; +use super::identifiers::{local_actor_id, local_actor_key_id}; use super::queues::OutgoingActivityJobData; #[derive(thiserror::Error, Debug)] @@ -136,14 +136,11 @@ async fn deliver_activity_worker( recipients: Vec, ) -> Result<(), DelivererError> { let actor_key = deserialize_private_key(&sender.private_key)?; - let actor_key_id = format!( - "{}{}", - local_actor_id( - &instance.url(), - &sender.profile.username, - ), - ACTOR_KEY_SUFFIX, + let actor_id = local_actor_id( + &instance.url(), + &sender.profile.username, ); + let actor_key_id = local_actor_key_id(&actor_id); let activity_signed = if is_object_signed(&activity) { log::warn!("activity is already signed"); activity diff --git a/src/activitypub/fetcher/fetchers.rs b/src/activitypub/fetcher/fetchers.rs index e828a59..1275f86 100644 --- a/src/activitypub/fetcher/fetchers.rs +++ b/src/activitypub/fetcher/fetchers.rs @@ -7,6 +7,7 @@ use serde_json::Value; use crate::activitypub::{ actors::types::Actor, constants::AP_MEDIA_TYPE, + identifiers::{local_actor_key_id, local_instance_actor_id}, types::Object, }; use crate::config::Instance; @@ -90,12 +91,14 @@ async fn send_request( }; if !instance.is_private { // Only public instance can send signed request + let instance_actor_id = local_instance_actor_id(&instance.url()); + let instance_actor_key_id = local_actor_key_id(&instance_actor_id); let headers = create_http_signature( Method::GET, url, "", &instance.actor_key, - &instance.actor_key_id(), + &instance_actor_key_id, )?; request_builder = request_builder .header("Host", headers.host) diff --git a/src/activitypub/identifiers.rs b/src/activitypub/identifiers.rs index 452575b..ec23667 100644 --- a/src/activitypub/identifiers.rs +++ b/src/activitypub/identifiers.rs @@ -3,6 +3,8 @@ use uuid::Uuid; use crate::errors::ValidationError; +const ACTOR_KEY_SUFFIX: &str = "#main-key"; + pub enum LocalActorCollection { Inbox, Outbox, @@ -58,6 +60,10 @@ pub fn local_instance_actor_id(instance_url: &str) -> String { format!("{}/actor", instance_url) } +pub fn local_actor_key_id(actor_id: &str) -> String { + format!("{}{}", actor_id, ACTOR_KEY_SUFFIX) +} + pub fn local_object_id(instance_url: &str, internal_object_id: &Uuid) -> String { format!("{}/objects/{}", instance_url, internal_object_id) } diff --git a/src/config/main.rs b/src/config/main.rs index 2ce0761..09f2fdc 100644 --- a/src/config/main.rs +++ b/src/config/main.rs @@ -9,8 +9,6 @@ use serde::{ }; use url::Url; -use crate::activitypub::constants::ACTOR_KEY_SUFFIX; -use crate::activitypub::identifiers::local_instance_actor_id; use crate::utils::urls::guess_protocol; use super::blockchain::BlockchainConfig; @@ -183,14 +181,6 @@ impl Instance { self._url.host_str().unwrap().to_string() } - pub fn actor_id(&self) -> String { - local_instance_actor_id(&self.url()) - } - - pub fn actor_key_id(&self) -> String { - format!("{}{}", self.actor_id(), ACTOR_KEY_SUFFIX) - } - pub fn agent(&self) -> String { format!( "Mitra {version}; {instance_url}",