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

58 lines
1.3 KiB
Rust
Raw Normal View History

2019-09-11 23:06:36 +00:00
use actix::System;
use actix_web::{web, App, HttpRequest, HttpServer, ResponseError};
2019-09-13 01:29:24 +00:00
use failure::Fail;
2019-09-11 23:06:36 +00:00
use http_signature_normalization_actix::{prelude::*, verify::Algorithm};
2019-09-13 01:12:35 +00:00
use sha2::{Digest, Sha256};
2019-09-11 23:06:36 +00:00
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())
2019-09-13 01:12:35 +00:00
.wrap(VerifyDigest::new(Sha256::new()))
.route("/", web::post().to(index))
2019-09-11 23:06:36 +00:00
})
.bind("127.0.0.1:8010")?
.start();
sys.run()?;
Ok(())
}
2019-09-13 01:29:24 +00:00
#[derive(Debug, Fail)]
2019-09-11 23:06:36 +00:00
enum MyError {
2019-09-13 01:29:24 +00:00
#[fail(display = "Failed to verify, {}", _0)]
Verify(#[cause] VerifyError),
2019-09-11 23:06:36 +00:00
2019-09-13 01:29:24 +00:00
#[fail(display = "Unsupported algorithm")]
Algorithm,
2019-09-11 23:06:36 +00:00
}
impl ResponseError for MyError {
// default 500
}
impl From<VerifyError> for MyError {
fn from(e: VerifyError) -> Self {
MyError::Verify(e)
}
}