diff --git a/Cargo.toml b/Cargo.toml index 13b81bc..307a3ca 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "activitystreams" description = "A set of core types and traits for activitystreams data" -version = "0.7.0-alpha.22" +version = "0.7.0-alpha.23" license = "GPL-3.0" authors = ["asonix "] repository = "https://git.asonix.dog/asonix/activitystreams" diff --git a/src/activity.rs b/src/activity.rs index fc2c498..f46e686 100644 --- a/src/activity.rs +++ b/src/activity.rs @@ -438,7 +438,7 @@ pub trait AsActivityActorExt: AsActivityActor { let actor = self.actor_unchecked(); for any_base in actor { - let id = any_base.id().ok_or(CheckError)?; + let id = any_base.id().ok_or(CheckError(None))?; self.check_authority(id)?; } @@ -571,7 +571,7 @@ pub trait AsActivityObjectExt: AsActivityObject { let object = self.object_unchecked(); for any_base in object { - let id = any_base.id().ok_or(CheckError)?; + let id = any_base.id().ok_or(CheckError(None))?; self.check_authority(id)?; } diff --git a/src/actor.rs b/src/actor.rs index 2d11c36..8e1734c 100644 --- a/src/actor.rs +++ b/src/actor.rs @@ -30,12 +30,23 @@ use crate::{ primitives::OneOrMany, unparsed::{Unparsed, UnparsedMut, UnparsedMutExt}, }; -use iri_string::types::IriString; +use iri_string::{components::AuthorityComponents, types::IriString}; pub use activitystreams_kinds::actor as kind; use self::kind::*; +fn check_opt( + iri: &IriString, + authority_components: Option<&AuthorityComponents>, +) -> Result<(), CheckError> { + if iri.authority_components().as_ref() == authority_components { + Ok(()) + } else { + Err(CheckError(Some(iri.clone()))) + } +} + /// Implementation trait for deriving ActivityPub Actor methods for a type /// /// Any type implementing AsObject will automatically gain methods provided by ApActorExt @@ -820,36 +831,30 @@ pub trait ApActorExt: AsApActor { if let Some(endpoints) = self.endpoints_unchecked() { let authority_opt = self.id_unchecked().and_then(|id| id.authority_components()); - let mut any_failed = false; - - any_failed |= endpoints + endpoints .proxy_url - .map(|u| u.authority_components() != authority_opt) - .unwrap_or(false); - any_failed |= endpoints + .map(|i| check_opt(i, authority_opt.as_ref())) + .transpose()?; + endpoints .oauth_authorization_endpoint - .map(|u| u.authority_components() != authority_opt) - .unwrap_or(false); - any_failed |= endpoints + .map(|i| check_opt(i, authority_opt.as_ref())) + .transpose()?; + endpoints .oauth_token_endpoint - .map(|u| u.authority_components() != authority_opt) - .unwrap_or(false); - any_failed |= endpoints + .map(|i| check_opt(i, authority_opt.as_ref())) + .transpose()?; + endpoints .provide_client_key - .map(|u| u.authority_components() != authority_opt) - .unwrap_or(false); - any_failed |= endpoints + .map(|i| check_opt(i, authority_opt.as_ref())) + .transpose()?; + endpoints .sign_client_key - .map(|u| u.authority_components() != authority_opt) - .unwrap_or(false); - any_failed |= endpoints + .map(|i| check_opt(i, authority_opt.as_ref())) + .transpose()?; + endpoints .shared_inbox - .map(|u| u.authority_components() != authority_opt) - .unwrap_or(false); - - if any_failed { - return Err(CheckError); - } + .map(|i| check_opt(i, authority_opt.as_ref())) + .transpose()?; return Ok(Some(endpoints)); } diff --git a/src/base.rs b/src/base.rs index 1e1008f..5d97514 100644 --- a/src/base.rs +++ b/src/base.rs @@ -263,7 +263,7 @@ pub trait BaseExt: AsBase { let authority = self .id_unchecked() .and_then(|id| id.authority_components()) - .ok_or(CheckError)?; + .ok_or(CheckError(Some(iri.as_ref().to_owned())))?; check(iri, authority.host(), authority.port()) } @@ -290,7 +290,7 @@ pub trait BaseExt: AsBase { if authority.host() == host && authority.port() == port { Some(Ok(id)) } else { - Some(Err(CheckError)) + Some(Err(CheckError(Some(id.clone())))) } }) .transpose() @@ -342,7 +342,7 @@ pub trait BaseExt: AsBase { if authority.host() == host && authority.port() == port { Some(Ok(id)) } else { - Some(Err(CheckError)) + Some(Err(CheckError(Some(id.clone())))) } }) .transpose() diff --git a/src/checked.rs b/src/checked.rs index 8ec21ee..4cf4a99 100644 --- a/src/checked.rs +++ b/src/checked.rs @@ -1,11 +1,15 @@ -use iri_string::types::IriStr; +use iri_string::types::{IriStr, IriString}; #[derive(Clone, Debug)] -pub struct CheckError; +pub struct CheckError(pub(crate) Option); impl std::fmt::Display for CheckError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "IRI failed host and port check") + if let Some(iri) = &self.0 { + write!(f, "IRI failed host and port check: {}", iri) + } else { + write!(f, "IRI missing") + } } } @@ -16,10 +20,13 @@ pub(crate) fn check>( host: &str, port: Option<&str>, ) -> Result { - let authority = iri.as_ref().authority_components().ok_or(CheckError)?; + let authority = iri + .as_ref() + .authority_components() + .ok_or(CheckError(Some(iri.as_ref().to_owned())))?; if authority.host() != host || authority.port() != port { - return Err(CheckError); + return Err(CheckError(Some(iri.as_ref().to_owned()))); } Ok(iri)