Sign GET request to other instances

This commit is contained in:
Kitaiti Makoto
2021-11-24 22:50:16 +09:00
parent c525410062
commit f4d7dfb261
9 changed files with 66 additions and 15 deletions
+22 -7
View File
@@ -1,6 +1,11 @@
use reqwest::header::{HeaderValue, ACCEPT};
use reqwest::{
header::{HeaderValue, HOST},
Url,
};
use std::fmt::Debug;
use super::{request, sign::Signer};
/// Represents an ActivityPub inbox.
///
/// It routes an incoming Activity through the registered handlers.
@@ -311,6 +316,14 @@ pub trait FromId<C>: Sized {
id: &str,
proxy: Option<reqwest::Proxy>,
) -> Result<Self::Object, (Option<serde_json::Value>, Self::Error)> {
let mut headers = request::headers();
let url = Url::parse(id).map_err(|_| (None, InboxError::DerefError.into()))?;
if !url.has_host() {
return Err((None, InboxError::DerefError.into()));
}
let host_header_value = HeaderValue::from_str(&url.host_str().expect("Unreachable"))
.map_err(|_| (None, InboxError::DerefError.into()))?;
headers.insert(HOST, host_header_value);
if let Some(proxy) = proxy {
reqwest::ClientBuilder::new().proxy(proxy)
} else {
@@ -320,13 +333,13 @@ pub trait FromId<C>: Sized {
.build()
.map_err(|_| (None, InboxError::DerefError.into()))?
.get(id)
.headers(headers.clone())
.header(
ACCEPT,
HeaderValue::from_str(
&super::ap_accept_header()
.into_iter()
.collect::<Vec<_>>()
.join(", "),
"Signature",
request::signature(
Self::get_sender(),
&headers,
("get", url.path(), url.query()),
)
.map_err(|_| (None, InboxError::DerefError.into()))?,
)
@@ -347,6 +360,8 @@ pub trait FromId<C>: Sized {
/// Tries to find a `Self` with a given ID (`id`), using `ctx` (a database)
fn from_db(ctx: &C, id: &str) -> Result<Self, Self::Error>;
fn get_sender() -> &'static dyn Signer;
}
/// Should be implemented by anything representing an ActivityPub actor.
+2 -2
View File
@@ -118,8 +118,8 @@ type Path<'a> = &'a str;
type Query<'a> = &'a str;
type RequestTarget<'a> = (Method<'a>, Path<'a>, Option<Query<'a>>);
pub fn signature<S: Signer>(
signer: &S,
pub fn signature(
signer: &dyn Signer,
headers: &HeaderMap,
request_target: RequestTarget,
) -> Result<HeaderValue, Error> {