2022-11-28 21:19:56 +00:00
|
|
|
use axum::{
|
|
|
|
async_trait,
|
2023-02-19 12:26:01 +00:00
|
|
|
body::{Bytes, HttpBody},
|
2022-11-28 21:19:56 +00:00
|
|
|
extract::FromRequest,
|
|
|
|
http::{Request, StatusCode},
|
|
|
|
response::{IntoResponse, Response},
|
|
|
|
};
|
2023-02-19 12:26:01 +00:00
|
|
|
use http::{HeaderMap, Method, Uri};
|
2022-11-28 21:19:56 +00:00
|
|
|
|
2023-03-02 14:18:06 +00:00
|
|
|
/// Handles incoming activities, verifying HTTP signatures and other checks
|
2022-11-28 21:19:56 +00:00
|
|
|
pub mod inbox;
|
2023-03-02 14:18:06 +00:00
|
|
|
/// Wrapper struct to respond with `application/activity+json` in axum handlers
|
2022-11-28 21:19:56 +00:00
|
|
|
pub mod json;
|
2023-03-02 14:18:06 +00:00
|
|
|
#[doc(hidden)]
|
2023-02-11 12:32:35 +00:00
|
|
|
pub mod middleware;
|
2022-11-28 21:19:56 +00:00
|
|
|
|
2023-02-19 12:26:01 +00:00
|
|
|
/// Contains everything that is necessary to verify HTTP signatures and receive an
|
|
|
|
/// activity, including the request body.
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct ActivityData {
|
|
|
|
headers: HeaderMap,
|
|
|
|
method: Method,
|
|
|
|
uri: Uri,
|
|
|
|
body: Vec<u8>,
|
2022-11-28 21:19:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait]
|
2023-02-19 12:26:01 +00:00
|
|
|
impl<S, B> FromRequest<S, B> for ActivityData
|
2022-11-28 21:19:56 +00:00
|
|
|
where
|
2023-02-19 12:26:01 +00:00
|
|
|
Bytes: FromRequest<S, B>,
|
|
|
|
B: HttpBody + Send + 'static,
|
2022-11-28 21:19:56 +00:00
|
|
|
S: Send + Sync,
|
2023-02-19 12:26:01 +00:00
|
|
|
<B as HttpBody>::Error: std::fmt::Display,
|
|
|
|
<B as HttpBody>::Data: Send,
|
2022-11-28 21:19:56 +00:00
|
|
|
{
|
|
|
|
type Rejection = Response;
|
|
|
|
|
2023-02-19 12:26:01 +00:00
|
|
|
async fn from_request(req: Request<B>, _state: &S) -> Result<Self, Self::Rejection> {
|
|
|
|
let (parts, body) = req.into_parts();
|
2022-11-28 21:19:56 +00:00
|
|
|
|
2023-02-19 12:26:01 +00:00
|
|
|
// this wont work if the body is an long running stream
|
|
|
|
let bytes = hyper::body::to_bytes(body)
|
|
|
|
.await
|
|
|
|
.map_err(|err| (StatusCode::INTERNAL_SERVER_ERROR, err.to_string()).into_response())?;
|
|
|
|
|
|
|
|
Ok(Self {
|
|
|
|
headers: parts.headers,
|
|
|
|
method: parts.method,
|
|
|
|
uri: parts.uri,
|
|
|
|
body: bytes.to_vec(),
|
|
|
|
})
|
2022-11-28 21:19:56 +00:00
|
|
|
}
|
|
|
|
}
|