2023-03-05 17:17:34 -08:00
|
|
|
//! Utilities for using this library with actix-web framework
|
|
|
|
|
|
2024-09-11 05:47:13 -07:00
|
|
|
mod http_compat;
|
2023-03-05 17:17:34 -08:00
|
|
|
pub mod inbox;
|
|
|
|
|
#[doc(hidden)]
|
|
|
|
|
pub mod middleware;
|
2023-06-12 04:32:54 -07:00
|
|
|
|
|
|
|
|
use crate::{
|
|
|
|
|
config::Data,
|
|
|
|
|
error::Error,
|
|
|
|
|
http_signatures::{self, verify_body_hash},
|
|
|
|
|
traits::{Actor, Object},
|
|
|
|
|
};
|
|
|
|
|
use actix_web::{web::Bytes, HttpRequest};
|
|
|
|
|
use serde::Deserialize;
|
|
|
|
|
|
|
|
|
|
/// Checks whether the request is signed by an actor of type A, and returns
|
|
|
|
|
/// the actor in question if a valid signature is found.
|
|
|
|
|
pub async fn signing_actor<A>(
|
|
|
|
|
request: &HttpRequest,
|
|
|
|
|
body: Option<Bytes>,
|
|
|
|
|
data: &Data<<A as Object>::DataType>,
|
|
|
|
|
) -> Result<A, <A as Object>::Error>
|
|
|
|
|
where
|
|
|
|
|
A: Object + Actor,
|
2023-11-20 02:42:47 -08:00
|
|
|
<A as Object>::Error: From<Error>,
|
2023-06-12 04:32:54 -07:00
|
|
|
for<'de2> <A as Object>::Kind: Deserialize<'de2>,
|
|
|
|
|
{
|
2024-09-11 05:47:13 -07:00
|
|
|
let digest_header = request
|
|
|
|
|
.headers()
|
|
|
|
|
.get("Digest")
|
|
|
|
|
.map(http_compat::header_value);
|
|
|
|
|
verify_body_hash(digest_header.as_ref(), &body.unwrap_or_default())?;
|
2023-06-12 04:32:54 -07:00
|
|
|
|
2024-09-11 05:47:13 -07:00
|
|
|
let headers = http_compat::header_map(request.headers());
|
|
|
|
|
let method = http_compat::method(request.method());
|
|
|
|
|
let uri = http_compat::uri(request.uri());
|
|
|
|
|
http_signatures::signing_actor(&headers, &method, &uri, data).await
|
2023-06-12 04:32:54 -07:00
|
|
|
}
|