http-signature-normalization/http-signature-normalization-actix/examples/server.rs

58 lines
1.3 KiB
Rust

use actix::System;
use actix_web::{web, App, HttpRequest, HttpServer, ResponseError};
use failure::Fail;
use http_signature_normalization_actix::{prelude::*, verify::Algorithm};
use sha2::{Digest, Sha256};
fn index((req, config): (HttpRequest, web::Data<Config>)) -> 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<dyn std::error::Error>> {
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, Fail)]
enum MyError {
#[fail(display = "Failed to verify, {}", _0)]
Verify(#[cause] VerifyError),
#[fail(display = "Unsupported algorithm")]
Algorithm,
}
impl ResponseError for MyError {
// default 500
}
impl From<VerifyError> for MyError {
fn from(e: VerifyError) -> Self {
MyError::Verify(e)
}
}