move signature outside the spawning

this allows us to actually move stuff into the async block
and we can drop the 'static life-time.
This commit is contained in:
Jeb Rosen 2020-02-17 22:34:18 +01:00 committed by Igor Galić
parent a010025074
commit 8aa99cea35
No known key found for this signature in database
GPG Key ID: ACFEFF7F6A123A86
3 changed files with 11 additions and 14 deletions

View File

@ -120,10 +120,9 @@ impl<'a, 'r> FromRequestAsync<'a, 'r> for ApRequest {
}) })
} }
} }
pub fn broadcast<S, A, T, C>(sender: &'static S, act: A, to: Vec<T>) pub fn broadcast<S, A, T, C>(sender: &S, act: A, to: Vec<T>)
where where
S: sign::Signer, S: sign::Signer,
S: std::marker::Sync,
A: Activity, A: Activity,
T: inbox::AsActor<C>, T: inbox::AsActor<C>,
{ {
@ -151,6 +150,8 @@ where
let body = signed.to_string(); let body = signed.to_string();
let mut headers = request::headers(); let mut headers = request::headers();
headers.insert("Digest", request::Digest::digest(&body)); headers.insert("Digest", request::Digest::digest(&body));
let sig = request::signature(sender, &headers)
.expect("activity_pub::broadcast: request signature error");
rt.spawn(async move { rt.spawn(async move {
let client = ClientBuilder::new() let client = ClientBuilder::new()
.connect_timeout(std::time::Duration::from_secs(5)) .connect_timeout(std::time::Duration::from_secs(5))
@ -159,11 +160,7 @@ where
client client
.post(&inbox) .post(&inbox)
.headers(headers.clone()) .headers(headers.clone())
.header( .header("Signature", sig)
"Signature",
request::signature(sender, &headers)
.expect("activity_pub::broadcast: request signature error"),
)
.body(body) .body(body)
.send() .send()
.await .await

View File

@ -81,10 +81,10 @@ impl Follow {
/// from -> The one sending the follow request /// from -> The one sending the follow request
/// target -> The target of the request, responding with Accept /// target -> The target of the request, responding with Accept
pub fn accept_follow<A: Signer + IntoId + Clone + Sync, B: Clone + AsActor<T> + IntoId, T>( pub fn accept_follow<A: Signer + IntoId + Clone, B: Clone + AsActor<T> + IntoId, T>(
conn: &Connection, conn: &Connection,
from: &B, from: &B,
target: &'static A, target: &A,
follow: FollowAct, follow: FollowAct,
from_id: i32, from_id: i32,
target_id: i32, target_id: i32,

View File

@ -10,6 +10,7 @@ use rocket::{data::*, http::Status, response::status, Outcome::*, Request};
use rocket_contrib::json::*; use rocket_contrib::json::*;
use serde::Deserialize; use serde::Deserialize;
use std::io::Read; use std::io::Read;
use tokio::io::AsyncReadExt;
pub fn handle_incoming( pub fn handle_incoming(
rockets: PlumeRocket, rockets: PlumeRocket,
@ -73,10 +74,7 @@ impl<'a, T: Deserialize<'a>> FromData<'a> for SignedJson<T> {
type Owned = String; type Owned = String;
type Borrowed = str; type Borrowed = str;
fn transform<'r>( fn transform<'r>(r: &'r Request, d: Data) -> TransformFuture<'r, Self::Owned, Self::Error> {
r: &'r Request,
d: Data
) -> TransformFuture<'r, Self::Owned, Self::Error> {
Box::pin(async move { Box::pin(async move {
let size_limit = r.limits().get("json").unwrap_or(JSON_LIMIT); let size_limit = r.limits().get("json").unwrap_or(JSON_LIMIT);
let mut s = String::with_capacity(512); let mut s = String::with_capacity(512);
@ -96,7 +94,9 @@ impl<'a, T: Deserialize<'a>> FromData<'a> for SignedJson<T> {
let string = try_outcome!(o.borrowed()); let string = try_outcome!(o.borrowed());
match serde_json::from_str(&string) { match serde_json::from_str(&string) {
Ok(v) => Success(SignedJson(Digest::from_body(&string), Json(v))), Ok(v) => Success(SignedJson(Digest::from_body(&string), Json(v))),
Err(e) if e.is_data() => return Failure((Status::UnprocessableEntity, JsonError::Parse(string, e))), Err(e) if e.is_data() => {
return Failure((Status::UnprocessableEntity, JsonError::Parse(string, e)))
}
Err(e) => Failure((Status::BadRequest, JsonError::Parse(string, e))), Err(e) => Failure((Status::BadRequest, JsonError::Parse(string, e))),
} }
}) })