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]
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 <asonix@asonix.dog>"]
repository = "https://git.asonix.dog/asonix/activitystreams"

View file

@ -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)?;
}

View file

@ -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));
}

View file

@ -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()

View file

@ -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<IriString>);
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<T: AsRef<IriStr>>(
host: &str,
port: Option<&str>,
) -> 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 {
return Err(CheckError);
return Err(CheckError(Some(iri.as_ref().to_owned())));
}
Ok(iri)