From b9a89ffc8ef814ed473a49f2ad48a4edb6b4f763 Mon Sep 17 00:00:00 2001 From: Nutomic Date: Tue, 19 Nov 2024 14:22:05 +0100 Subject: [PATCH] Add method to sign arbitrary http request (#131) --- src/config.rs | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/config.rs b/src/config.rs index 2015750..63af704 100644 --- a/src/config.rs +++ b/src/config.rs @@ -17,14 +17,17 @@ use crate::{ activity_queue::{create_activity_queue, ActivityQueue}, error::Error, + http_signatures::sign_request, protocol::verification::verify_domains_match, traits::{ActivityHandler, Actor}, }; use async_trait::async_trait; +use bytes::Bytes; use derive_builder::Builder; use dyn_clone::{clone_trait_object, DynClone}; use moka::future::Cache; -use reqwest_middleware::ClientWithMiddleware; +use reqwest::Request; +use reqwest_middleware::{ClientWithMiddleware, RequestBuilder}; use rsa::{pkcs8::DecodePrivateKey, RsaPrivateKey}; use serde::de::DeserializeOwned; use std::{ @@ -327,6 +330,25 @@ impl Data { pub fn request_count(&self) -> u32 { self.request_counter.load(Ordering::Relaxed) } + + /// Add HTTP signature to arbitrary request + pub async fn sign_request(&self, req: RequestBuilder, body: Bytes) -> Result { + let (actor_id, private_key_pem) = + self.config + .signed_fetch_actor + .as_deref() + .ok_or(Error::Other( + "config value signed_fetch_actor is none".to_string(), + ))?; + sign_request( + req, + actor_id, + body, + private_key_pem.clone(), + self.config.http_signature_compat, + ) + .await + } } impl Deref for Data {