use actix::System; use actix_web::{web, App, HttpRequest, HttpServer, ResponseError}; use http_signature_normalization_actix::{prelude::*, verify::Algorithm}; use sha2::{Digest, Sha256}; use std::fmt; fn index((req, config): (HttpRequest, web::Data)) -> Result<&'static str, MyError> { let unverified = req.begin_verify(&config)?; if let Some(a) = unverified.algorithm() { match *a { Algorithm::Hs2019 => (), _ => return Err(MyError::Algorithm), } } if unverified.verify(|bytes, string| bytes == string.as_bytes()) { Ok("Eyyyyup") } else { Ok("Nope") } } fn main() -> Result<(), Box> { let sys = System::new("server-example"); HttpServer::new(move || { App::new() .data(Config::default()) .wrap(VerifyDigest::new(Sha256::new())) .route("/", web::post().to(index)) }) .bind("127.0.0.1:8010")? .start(); sys.run()?; Ok(()) } #[derive(Debug)] enum MyError { Verify(VerifyError), Algorithm, } impl fmt::Display for MyError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match *self { MyError::Verify(ref e) => write!(f, "Verify Error, {}", e), MyError::Algorithm => write!(f, "Unsupported algorithm"), } } } impl std::error::Error for MyError { fn description(&self) -> &str { match *self { MyError::Verify(ref e) => e.description(), MyError::Algorithm => "Unsupported algorithm", } } fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { match *self { MyError::Verify(ref e) => Some(e), MyError::Algorithm => None, } } } impl ResponseError for MyError { // default 500 } impl From for MyError { fn from(e: VerifyError) -> Self { MyError::Verify(e) } }