2020-03-30 05:53:45 +00:00
|
|
|
use actix_web::{
|
|
|
|
http::StatusCode, middleware::Logger, web, App, HttpRequest, HttpResponse, HttpServer,
|
|
|
|
ResponseError,
|
|
|
|
};
|
2020-03-16 00:29:47 +00:00
|
|
|
use futures::future::{err, ok, Ready};
|
|
|
|
use http_signature_normalization_actix::prelude::*;
|
2020-03-17 19:47:00 +00:00
|
|
|
use log::info;
|
2019-09-13 01:12:35 +00:00
|
|
|
use sha2::{Digest, Sha256};
|
2019-09-11 23:06:36 +00:00
|
|
|
|
2019-09-13 22:55:51 +00:00
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
struct MyVerify;
|
2019-09-11 23:06:36 +00:00
|
|
|
|
2019-09-13 22:55:51 +00:00
|
|
|
impl SignatureVerify for MyVerify {
|
|
|
|
type Error = MyError;
|
2020-03-16 00:29:47 +00:00
|
|
|
type Future = Ready<Result<bool, Self::Error>>;
|
2019-09-13 22:55:51 +00:00
|
|
|
|
|
|
|
fn signature_verify(
|
|
|
|
&mut self,
|
|
|
|
algorithm: Option<Algorithm>,
|
2020-04-22 22:15:32 +00:00
|
|
|
key_id: String,
|
|
|
|
signature: String,
|
|
|
|
signing_string: String,
|
2019-09-13 22:55:51 +00:00
|
|
|
) -> Self::Future {
|
|
|
|
match algorithm {
|
|
|
|
Some(Algorithm::Hs2019) => (),
|
2020-03-16 00:29:47 +00:00
|
|
|
_ => return err(MyError::Algorithm),
|
2019-09-13 22:55:51 +00:00
|
|
|
};
|
|
|
|
|
2019-09-13 23:12:12 +00:00
|
|
|
if key_id != "my-key-id" {
|
2020-03-16 00:29:47 +00:00
|
|
|
return err(MyError::Key);
|
2019-09-13 23:12:12 +00:00
|
|
|
}
|
|
|
|
|
2020-04-22 22:15:32 +00:00
|
|
|
let decoded = match base64::decode(&signature) {
|
2020-03-16 00:29:47 +00:00
|
|
|
Ok(decoded) => decoded,
|
|
|
|
Err(_) => return err(MyError::Decode),
|
|
|
|
};
|
2019-09-11 23:06:36 +00:00
|
|
|
|
2020-03-17 23:54:00 +00:00
|
|
|
println!("Signing String\n{}", signing_string);
|
|
|
|
|
2020-03-16 00:29:47 +00:00
|
|
|
ok(decoded == signing_string.as_bytes())
|
2019-09-11 23:06:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-17 19:47:00 +00:00
|
|
|
async fn index(
|
|
|
|
(_, sig_verified): (DigestVerified, SignatureVerified),
|
|
|
|
req: HttpRequest,
|
2020-03-25 16:00:37 +00:00
|
|
|
_body: web::Bytes,
|
2020-03-17 19:47:00 +00:00
|
|
|
) -> &'static str {
|
|
|
|
info!("Verified request for {}", sig_verified.key_id());
|
|
|
|
info!("{:?}", req);
|
2019-09-13 22:55:51 +00:00
|
|
|
"Eyyyyup"
|
|
|
|
}
|
|
|
|
|
2020-03-16 00:29:47 +00:00
|
|
|
#[actix_rt::main]
|
|
|
|
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
2020-03-17 19:27:38 +00:00
|
|
|
std::env::set_var("RUST_LOG", "info");
|
|
|
|
pretty_env_logger::init();
|
|
|
|
|
2020-09-29 23:57:59 +00:00
|
|
|
let config = Config::default().require_header("accept").require_digest();
|
2019-09-13 22:55:51 +00:00
|
|
|
|
2019-09-11 23:06:36 +00:00
|
|
|
HttpServer::new(move || {
|
|
|
|
App::new()
|
2019-09-13 23:27:04 +00:00
|
|
|
.wrap(VerifyDigest::new(Sha256::new()).optional())
|
2020-03-17 20:12:10 +00:00
|
|
|
.wrap(VerifySignature::new(MyVerify, config.clone()).optional())
|
2020-03-30 05:53:45 +00:00
|
|
|
.wrap(Logger::default())
|
2019-09-13 01:12:35 +00:00
|
|
|
.route("/", web::post().to(index))
|
2019-09-11 23:06:36 +00:00
|
|
|
})
|
|
|
|
.bind("127.0.0.1:8010")?
|
2020-03-16 00:29:47 +00:00
|
|
|
.run()
|
|
|
|
.await?;
|
2019-09-11 23:06:36 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-03-16 00:29:47 +00:00
|
|
|
#[derive(Debug, thiserror::Error)]
|
2019-09-11 23:06:36 +00:00
|
|
|
enum MyError {
|
2020-04-23 17:54:56 +00:00
|
|
|
#[error("Failed to verify, {0}")]
|
2020-03-16 00:29:47 +00:00
|
|
|
Verify(#[from] PrepareVerifyError),
|
2019-09-11 23:06:36 +00:00
|
|
|
|
2020-03-16 00:29:47 +00:00
|
|
|
#[error("Unsupported algorithm")]
|
2019-09-13 01:29:24 +00:00
|
|
|
Algorithm,
|
2019-09-13 22:55:51 +00:00
|
|
|
|
2020-03-16 00:29:47 +00:00
|
|
|
#[error("Couldn't decode signature")]
|
2019-09-13 22:55:51 +00:00
|
|
|
Decode,
|
2019-09-13 23:12:12 +00:00
|
|
|
|
2020-03-16 00:29:47 +00:00
|
|
|
#[error("Invalid key")]
|
2019-09-13 23:12:12 +00:00
|
|
|
Key,
|
2019-09-11 23:06:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ResponseError for MyError {
|
2020-03-16 00:29:47 +00:00
|
|
|
fn status_code(&self) -> StatusCode {
|
|
|
|
StatusCode::BAD_REQUEST
|
2019-09-13 22:55:51 +00:00
|
|
|
}
|
|
|
|
|
2020-03-16 00:29:47 +00:00
|
|
|
fn error_response(&self) -> HttpResponse {
|
|
|
|
HttpResponse::BadRequest().finish()
|
2019-09-11 23:06:36 +00:00
|
|
|
}
|
|
|
|
}
|