mirror of
https://github.com/LemmyNet/activitypub-federation-rust.git
synced 2024-06-10 09:19:34 +00:00
9332c81458
* feat: add actix feature flag * (WIP)feat: add axum feature * WIP: axum veridy digest + example Note: this does not compile yet * WIP * chore: clippy lints * Use actix rt for axum example * ci: run example in CI for both actix and axum * feat: add json wrapper type for axum * docs: update readme with actix and axum feature flags * fix: fix ci * chore: more clippy lints * refactor: update according to PR comment and factorize 'verify_digest'
52 lines
1.7 KiB
Rust
52 lines
1.7 KiB
Rust
use crate::{
|
|
core::object_id::ObjectId,
|
|
data::Data,
|
|
traits::{ActivityHandler, Actor, ApubObject},
|
|
Error,
|
|
LocalInstance,
|
|
};
|
|
|
|
use crate::core::signatures::verify_signature;
|
|
use actix_web::{HttpRequest, HttpResponse};
|
|
use serde::de::DeserializeOwned;
|
|
use tracing::debug;
|
|
|
|
/// Receive an activity and perform some basic checks, including HTTP signature verification.
|
|
pub async fn receive_activity<Activity, ActorT, Datatype>(
|
|
request: HttpRequest,
|
|
activity: Activity,
|
|
local_instance: &LocalInstance,
|
|
data: &Data<Datatype>,
|
|
) -> Result<HttpResponse, <Activity as ActivityHandler>::Error>
|
|
where
|
|
Activity: ActivityHandler<DataType = Datatype> + DeserializeOwned + Send + 'static,
|
|
ActorT: ApubObject<DataType = Datatype> + Actor + Send + 'static,
|
|
for<'de2> <ActorT as ApubObject>::ApubType: serde::Deserialize<'de2>,
|
|
<Activity as ActivityHandler>::Error: From<anyhow::Error>
|
|
+ From<Error>
|
|
+ From<<ActorT as ApubObject>::Error>
|
|
+ From<serde_json::Error>,
|
|
<ActorT as ApubObject>::Error: From<Error> + From<anyhow::Error>,
|
|
{
|
|
local_instance.verify_url_and_domain(&activity).await?;
|
|
|
|
let request_counter = &mut 0;
|
|
let actor = ObjectId::<ActorT>::new(activity.actor().clone())
|
|
.dereference(data, local_instance, request_counter)
|
|
.await?;
|
|
|
|
verify_signature(
|
|
request.headers(),
|
|
request.method(),
|
|
request.uri(),
|
|
actor.public_key(),
|
|
)?;
|
|
|
|
debug!("Verifying activity {}", activity.id().to_string());
|
|
activity.verify(data, request_counter).await?;
|
|
|
|
debug!("Receiving activity {}", activity.id().to_string());
|
|
activity.receive(data, request_counter).await?;
|
|
Ok(HttpResponse::Ok().finish())
|
|
}
|