Be more clear about check failures

This commit is contained in:
asonix 2022-12-19 13:53:24 -06:00
parent 7615a8ab5e
commit a65404b4a9
5 changed files with 48 additions and 36 deletions

View file

@ -1,7 +1,7 @@
[package] [package]
name = "activitystreams" name = "activitystreams"
description = "A set of core types and traits for activitystreams data" 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" license = "GPL-3.0"
authors = ["asonix <asonix@asonix.dog>"] authors = ["asonix <asonix@asonix.dog>"]
repository = "https://git.asonix.dog/asonix/activitystreams" repository = "https://git.asonix.dog/asonix/activitystreams"

View file

@ -438,7 +438,7 @@ pub trait AsActivityActorExt: AsActivityActor {
let actor = self.actor_unchecked(); let actor = self.actor_unchecked();
for any_base in actor { 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)?; self.check_authority(id)?;
} }
@ -571,7 +571,7 @@ pub trait AsActivityObjectExt: AsActivityObject {
let object = self.object_unchecked(); let object = self.object_unchecked();
for any_base in object { 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)?; self.check_authority(id)?;
} }

View file

@ -30,12 +30,23 @@ use crate::{
primitives::OneOrMany, primitives::OneOrMany,
unparsed::{Unparsed, UnparsedMut, UnparsedMutExt}, unparsed::{Unparsed, UnparsedMut, UnparsedMutExt},
}; };
use iri_string::types::IriString; use iri_string::{components::AuthorityComponents, types::IriString};
pub use activitystreams_kinds::actor as kind; pub use activitystreams_kinds::actor as kind;
use self::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 /// Implementation trait for deriving ActivityPub Actor methods for a type
/// ///
/// Any type implementing AsObject will automatically gain methods provided by ApActorExt /// 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() { if let Some(endpoints) = self.endpoints_unchecked() {
let authority_opt = self.id_unchecked().and_then(|id| id.authority_components()); let authority_opt = self.id_unchecked().and_then(|id| id.authority_components());
let mut any_failed = false; endpoints
any_failed |= endpoints
.proxy_url .proxy_url
.map(|u| u.authority_components() != authority_opt) .map(|i| check_opt(i, authority_opt.as_ref()))
.unwrap_or(false); .transpose()?;
any_failed |= endpoints endpoints
.oauth_authorization_endpoint .oauth_authorization_endpoint
.map(|u| u.authority_components() != authority_opt) .map(|i| check_opt(i, authority_opt.as_ref()))
.unwrap_or(false); .transpose()?;
any_failed |= endpoints endpoints
.oauth_token_endpoint .oauth_token_endpoint
.map(|u| u.authority_components() != authority_opt) .map(|i| check_opt(i, authority_opt.as_ref()))
.unwrap_or(false); .transpose()?;
any_failed |= endpoints endpoints
.provide_client_key .provide_client_key
.map(|u| u.authority_components() != authority_opt) .map(|i| check_opt(i, authority_opt.as_ref()))
.unwrap_or(false); .transpose()?;
any_failed |= endpoints endpoints
.sign_client_key .sign_client_key
.map(|u| u.authority_components() != authority_opt) .map(|i| check_opt(i, authority_opt.as_ref()))
.unwrap_or(false); .transpose()?;
any_failed |= endpoints endpoints
.shared_inbox .shared_inbox
.map(|u| u.authority_components() != authority_opt) .map(|i| check_opt(i, authority_opt.as_ref()))
.unwrap_or(false); .transpose()?;
if any_failed {
return Err(CheckError);
}
return Ok(Some(endpoints)); return Ok(Some(endpoints));
} }

View file

@ -263,7 +263,7 @@ pub trait BaseExt: AsBase {
let authority = self let authority = self
.id_unchecked() .id_unchecked()
.and_then(|id| id.authority_components()) .and_then(|id| id.authority_components())
.ok_or(CheckError)?; .ok_or(CheckError(Some(iri.as_ref().to_owned())))?;
check(iri, authority.host(), authority.port()) check(iri, authority.host(), authority.port())
} }
@ -290,7 +290,7 @@ pub trait BaseExt: AsBase {
if authority.host() == host && authority.port() == port { if authority.host() == host && authority.port() == port {
Some(Ok(id)) Some(Ok(id))
} else { } else {
Some(Err(CheckError)) Some(Err(CheckError(Some(id.clone()))))
} }
}) })
.transpose() .transpose()
@ -342,7 +342,7 @@ pub trait BaseExt: AsBase {
if authority.host() == host && authority.port() == port { if authority.host() == host && authority.port() == port {
Some(Ok(id)) Some(Ok(id))
} else { } else {
Some(Err(CheckError)) Some(Err(CheckError(Some(id.clone()))))
} }
}) })
.transpose() .transpose()

View file

@ -1,11 +1,15 @@
use iri_string::types::IriStr; use iri_string::types::{IriStr, IriString};
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct CheckError; pub struct CheckError(pub(crate) Option<IriString>);
impl std::fmt::Display for CheckError { impl std::fmt::Display for CheckError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { 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<T: AsRef<IriStr>>(
host: &str, host: &str,
port: Option<&str>, port: Option<&str>,
) -> Result<T, CheckError> { ) -> Result<T, CheckError> {
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 { if authority.host() != host || authority.port() != port {
return Err(CheckError); return Err(CheckError(Some(iri.as_ref().to_owned())));
} }
Ok(iri) Ok(iri)