2022-02-26 17:49:35 +00:00
|
|
|
use actix_rt::task::JoinError;
|
2021-03-10 02:04:56 +00:00
|
|
|
use awc::Client;
|
2019-09-11 23:06:36 +00:00
|
|
|
use http_signature_normalization_actix::prelude::*;
|
2019-09-13 01:12:35 +00:00
|
|
|
use sha2::{Digest, Sha256};
|
2021-09-18 01:04:04 +00:00
|
|
|
use tracing::{error, info};
|
|
|
|
use tracing_error::ErrorLayer;
|
|
|
|
use tracing_subscriber::{layer::SubscriberExt, EnvFilter};
|
2019-09-11 23:06:36 +00:00
|
|
|
|
2020-03-17 23:54:00 +00:00
|
|
|
async fn request(config: Config) -> Result<(), Box<dyn std::error::Error>> {
|
2020-03-30 05:53:45 +00:00
|
|
|
let digest = Sha256::new();
|
2020-03-16 00:29:47 +00:00
|
|
|
|
|
|
|
let mut response = Client::default()
|
|
|
|
.post("http://127.0.0.1:8010/")
|
2021-02-10 21:45:51 +00:00
|
|
|
.append_header(("User-Agent", "Actix Web"))
|
|
|
|
.append_header(("Accept", "text/plain"))
|
2020-03-30 05:53:45 +00:00
|
|
|
.signature_with_digest(config, "my-key-id", digest, "Hewwo-owo", |s| {
|
2021-09-18 01:04:04 +00:00
|
|
|
info!("Signing String\n{}", s);
|
2020-03-16 00:29:47 +00:00
|
|
|
Ok(base64::encode(s)) as Result<_, MyError>
|
2020-03-30 05:53:45 +00:00
|
|
|
})
|
|
|
|
.await?
|
2020-03-16 00:29:47 +00:00
|
|
|
.send()
|
|
|
|
.await
|
|
|
|
.map_err(|e| {
|
2021-09-18 01:04:04 +00:00
|
|
|
error!("Error, {}", e);
|
2020-03-16 00:29:47 +00:00
|
|
|
MyError::SendRequest
|
|
|
|
})?;
|
|
|
|
|
|
|
|
let body = response.body().await.map_err(|e| {
|
2021-09-18 01:04:04 +00:00
|
|
|
error!("Error, {}", e);
|
2020-03-16 00:29:47 +00:00
|
|
|
MyError::Body
|
|
|
|
})?;
|
|
|
|
|
2021-09-18 01:04:04 +00:00
|
|
|
info!("{:?}", body);
|
2020-03-16 00:29:47 +00:00
|
|
|
Ok(())
|
2019-09-11 23:06:36 +00:00
|
|
|
}
|
|
|
|
|
2020-03-17 23:54:00 +00:00
|
|
|
#[actix_rt::main]
|
|
|
|
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
2021-09-18 01:04:04 +00:00
|
|
|
let env_filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info"));
|
|
|
|
|
|
|
|
let subscriber = tracing_subscriber::Registry::default()
|
|
|
|
.with(env_filter)
|
|
|
|
.with(ErrorLayer::default())
|
|
|
|
.with(tracing_subscriber::fmt::layer());
|
|
|
|
|
|
|
|
tracing::subscriber::set_global_default(subscriber)?;
|
2020-03-17 23:54:00 +00:00
|
|
|
|
2020-09-29 23:57:59 +00:00
|
|
|
let config = Config::default().require_header("accept").require_digest();
|
2020-03-17 23:54:00 +00:00
|
|
|
|
|
|
|
request(config.clone()).await?;
|
2020-09-29 23:57:59 +00:00
|
|
|
request(config.mastodon_compat()).await?;
|
2020-03-17 23:54:00 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-03-16 00:29:47 +00:00
|
|
|
#[derive(Debug, thiserror::Error)]
|
2019-09-11 23:06:36 +00:00
|
|
|
pub enum MyError {
|
2020-04-23 17:54:56 +00:00
|
|
|
#[error("Failed to create signing string, {0}")]
|
|
|
|
Convert(#[from] PrepareSignError),
|
2019-09-13 01:29:24 +00:00
|
|
|
|
2020-03-16 00:29:47 +00:00
|
|
|
#[error("Failed to create header, {0}")]
|
|
|
|
Header(#[from] InvalidHeaderValue),
|
2019-09-11 23:06:36 +00:00
|
|
|
|
2020-03-16 00:29:47 +00:00
|
|
|
#[error("Failed to send request")]
|
|
|
|
SendRequest,
|
2019-09-11 23:06:36 +00:00
|
|
|
|
2020-03-16 00:29:47 +00:00
|
|
|
#[error("Failed to retrieve request body")]
|
|
|
|
Body,
|
2020-03-30 05:53:45 +00:00
|
|
|
|
|
|
|
#[error("Blocking operation was canceled")]
|
|
|
|
Canceled,
|
|
|
|
}
|
|
|
|
|
2022-02-26 17:49:35 +00:00
|
|
|
impl From<JoinError> for MyError {
|
|
|
|
fn from(_: JoinError) -> Self {
|
2021-02-10 21:45:51 +00:00
|
|
|
MyError::Canceled
|
2020-03-30 05:53:45 +00:00
|
|
|
}
|
2019-09-11 23:06:36 +00:00
|
|
|
}
|