Rewrite receive_activity to return ImportError instead of HttpError
This commit is contained in:
parent
862415d49b
commit
1e575b2f6f
3 changed files with 20 additions and 7 deletions
|
@ -12,6 +12,7 @@ use crate::activitypub::handlers::{
|
||||||
use crate::activitypub::identifiers::parse_local_object_id;
|
use crate::activitypub::identifiers::parse_local_object_id;
|
||||||
use crate::config::{Config, Instance};
|
use crate::config::{Config, Instance};
|
||||||
use crate::errors::{DatabaseError, HttpError, ValidationError};
|
use crate::errors::{DatabaseError, HttpError, ValidationError};
|
||||||
|
use crate::http_signatures::verify::VerificationError;
|
||||||
use crate::models::posts::queries::{
|
use crate::models::posts::queries::{
|
||||||
get_post_by_id,
|
get_post_by_id,
|
||||||
get_post_by_remote_object_id,
|
get_post_by_remote_object_id,
|
||||||
|
@ -44,6 +45,9 @@ pub enum ImportError {
|
||||||
|
|
||||||
#[error(transparent)]
|
#[error(transparent)]
|
||||||
DatabaseError(#[from] DatabaseError),
|
DatabaseError(#[from] DatabaseError),
|
||||||
|
|
||||||
|
#[error(transparent)]
|
||||||
|
AuthError(#[from] VerificationError),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<ImportError> for HttpError {
|
impl From<ImportError> for HttpError {
|
||||||
|
@ -55,6 +59,9 @@ impl From<ImportError> for HttpError {
|
||||||
},
|
},
|
||||||
ImportError::ValidationError(error) => error.into(),
|
ImportError::ValidationError(error) => error.into(),
|
||||||
ImportError::DatabaseError(error) => error.into(),
|
ImportError::DatabaseError(error) => error.into(),
|
||||||
|
ImportError::AuthError(_) => {
|
||||||
|
HttpError::AuthError("invalid signature")
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,10 +4,13 @@ use serde_json::Value;
|
||||||
use tokio_postgres::GenericClient;
|
use tokio_postgres::GenericClient;
|
||||||
|
|
||||||
use crate::config::Config;
|
use crate::config::Config;
|
||||||
use crate::errors::{ConversionError, HttpError, ValidationError};
|
use crate::errors::{ConversionError, ValidationError};
|
||||||
use crate::http_signatures::verify::verify_signed_request;
|
use crate::http_signatures::verify::{
|
||||||
|
verify_signed_request,
|
||||||
|
VerificationError,
|
||||||
|
};
|
||||||
use super::activity::{Activity, Object};
|
use super::activity::{Activity, Object};
|
||||||
use super::fetcher::helpers::import_post;
|
use super::fetcher::helpers::{import_post, ImportError};
|
||||||
use super::handlers::{
|
use super::handlers::{
|
||||||
accept_follow::handle_accept_follow,
|
accept_follow::handle_accept_follow,
|
||||||
add::handle_add,
|
add::handle_add,
|
||||||
|
@ -86,7 +89,7 @@ pub fn find_object_id(object: &Value) -> Result<String, ValidationError> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn require_actor_signature(actor_id: &str, signer_id: &str)
|
fn require_actor_signature(actor_id: &str, signer_id: &str)
|
||||||
-> Result<(), HttpError>
|
-> Result<(), VerificationError>
|
||||||
{
|
{
|
||||||
if actor_id != signer_id {
|
if actor_id != signer_id {
|
||||||
// Forwarded activity
|
// Forwarded activity
|
||||||
|
@ -95,7 +98,7 @@ fn require_actor_signature(actor_id: &str, signer_id: &str)
|
||||||
signer_id,
|
signer_id,
|
||||||
actor_id,
|
actor_id,
|
||||||
);
|
);
|
||||||
return Err(HttpError::AuthError("actor and request signer do not match"));
|
return Err(VerificationError::InvalidSigner);
|
||||||
};
|
};
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -105,7 +108,7 @@ pub async fn receive_activity(
|
||||||
db_client: &mut impl GenericClient,
|
db_client: &mut impl GenericClient,
|
||||||
request: &HttpRequest,
|
request: &HttpRequest,
|
||||||
activity_raw: &Value,
|
activity_raw: &Value,
|
||||||
) -> Result<(), HttpError> {
|
) -> Result<(), ImportError> {
|
||||||
let activity: Activity = serde_json::from_value(activity_raw.clone())
|
let activity: Activity = serde_json::from_value(activity_raw.clone())
|
||||||
.map_err(|_| ValidationError("invalid activity"))?;
|
.map_err(|_| ValidationError("invalid activity"))?;
|
||||||
let activity_type = activity.activity_type.clone();
|
let activity_type = activity.activity_type.clone();
|
||||||
|
@ -127,7 +130,7 @@ pub async fn receive_activity(
|
||||||
return Ok(());
|
return Ok(());
|
||||||
};
|
};
|
||||||
log::warn!("invalid signature: {}", error);
|
log::warn!("invalid signature: {}", error);
|
||||||
return Err(HttpError::AuthError("invalid signature"));
|
return Err(error.into());
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
let signer_id = signer.actor_id(&config.instance_url());
|
let signer_id = signer.actor_id(&config.instance_url());
|
||||||
|
|
|
@ -44,6 +44,9 @@ pub enum VerificationError {
|
||||||
|
|
||||||
#[error("invalid signature")]
|
#[error("invalid signature")]
|
||||||
InvalidSignature,
|
InvalidSignature,
|
||||||
|
|
||||||
|
#[error("actor and request signer do not match")]
|
||||||
|
InvalidSigner,
|
||||||
}
|
}
|
||||||
|
|
||||||
struct HttpSignatureData {
|
struct HttpSignatureData {
|
||||||
|
|
Loading…
Reference in a new issue