2019-09-11 23:06:36 +00:00
|
|
|
use actix_web::client::Client;
|
|
|
|
use http_signature_normalization_actix::prelude::*;
|
2019-09-13 01:12:35 +00:00
|
|
|
use sha2::{Digest, Sha256};
|
2020-03-17 23:54:00 +00:00
|
|
|
use std::time::SystemTime;
|
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-16 00:29:47 +00:00
|
|
|
let mut digest = Sha256::new();
|
|
|
|
|
|
|
|
let mut response = Client::default()
|
|
|
|
.post("http://127.0.0.1:8010/")
|
|
|
|
.header("User-Agent", "Actix Web")
|
2020-03-17 23:54:00 +00:00
|
|
|
.set(actix_web::http::header::Date(SystemTime::now().into()))
|
2020-03-17 20:12:10 +00:00
|
|
|
.signature_with_digest(&config, "my-key-id", &mut digest, "Hewwo-owo", |s| {
|
2020-03-17 23:54:00 +00:00
|
|
|
println!("Signing String\n{}", s);
|
2020-03-16 00:29:47 +00:00
|
|
|
Ok(base64::encode(s)) as Result<_, MyError>
|
|
|
|
})?
|
|
|
|
.send()
|
|
|
|
.await
|
|
|
|
.map_err(|e| {
|
|
|
|
eprintln!("Error, {}", e);
|
|
|
|
MyError::SendRequest
|
|
|
|
})?;
|
|
|
|
|
|
|
|
let body = response.body().await.map_err(|e| {
|
|
|
|
eprintln!("Error, {}", e);
|
|
|
|
MyError::Body
|
|
|
|
})?;
|
|
|
|
|
|
|
|
println!("{:?}", body);
|
|
|
|
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>> {
|
|
|
|
std::env::set_var("RUST_LOG", "info");
|
|
|
|
pretty_env_logger::init();
|
|
|
|
|
|
|
|
let config = Config::default();
|
|
|
|
|
|
|
|
request(config.clone()).await?;
|
|
|
|
request(config.dont_use_created_field()).await?;
|
|
|
|
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-03-16 00:29:47 +00:00
|
|
|
#[error("Failed to read header, {0}")]
|
|
|
|
Convert(#[from] ToStrError),
|
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,
|
2019-09-11 23:06:36 +00:00
|
|
|
}
|