mirror of
https://github.com/LemmyNet/activitypub-federation-rust.git
synced 2024-09-27 14:00:19 +00:00
426871f5af
* Use anyhow::Error for UrlVerifier return type (fixes #61) * fmt * uncomment
39 lines
1.2 KiB
Rust
39 lines
1.2 KiB
Rust
//! Verify that received data is valid
|
|
|
|
use crate::error::Error;
|
|
use anyhow::anyhow;
|
|
use url::Url;
|
|
|
|
/// Check that both urls have the same domain. If not, return UrlVerificationError.
|
|
///
|
|
/// ```
|
|
/// # use url::Url;
|
|
/// # use activitypub_federation::protocol::verification::verify_domains_match;
|
|
/// let a = Url::parse("https://example.com/abc")?;
|
|
/// let b = Url::parse("https://sample.net/abc")?;
|
|
/// assert!(verify_domains_match(&a, &b).is_err());
|
|
/// # Ok::<(), url::ParseError>(())
|
|
/// ```
|
|
pub fn verify_domains_match(a: &Url, b: &Url) -> Result<(), Error> {
|
|
if a.domain() != b.domain() {
|
|
return Err(Error::UrlVerificationError(anyhow!("Domains do not match")));
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
/// Check that both urls are identical. If not, return UrlVerificationError.
|
|
///
|
|
/// ```
|
|
/// # use url::Url;
|
|
/// # use activitypub_federation::protocol::verification::verify_urls_match;
|
|
/// let a = Url::parse("https://example.com/abc")?;
|
|
/// let b = Url::parse("https://example.com/123")?;
|
|
/// assert!(verify_urls_match(&a, &b).is_err());
|
|
/// # Ok::<(), url::ParseError>(())
|
|
/// ```
|
|
pub fn verify_urls_match(a: &Url, b: &Url) -> Result<(), Error> {
|
|
if a != b {
|
|
return Err(Error::UrlVerificationError(anyhow!("Urls do not match")));
|
|
}
|
|
Ok(())
|
|
}
|