mirror of
https://git.asonix.dog/asonix/http-signature-normalization.git
synced 2024-11-22 17:31:00 +00:00
Add actix example
This commit is contained in:
parent
854df4a8d1
commit
73ee0c51e6
3 changed files with 125 additions and 0 deletions
|
@ -12,3 +12,7 @@ edition = "2018"
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-web = "1.0"
|
actix-web = "1.0"
|
||||||
http-signature-normalization = { version = "0.1.0", path = ".." }
|
http-signature-normalization = { version = "0.1.0", path = ".." }
|
||||||
|
|
||||||
|
[dev-dependencies]
|
||||||
|
actix = "0.8"
|
||||||
|
futures = "0.1"
|
||||||
|
|
44
http-signature-normalization-actix/examples/client.rs
Normal file
44
http-signature-normalization-actix/examples/client.rs
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
use actix::System;
|
||||||
|
use actix_web::client::Client;
|
||||||
|
use futures::future::{lazy, Future};
|
||||||
|
use http_signature_normalization_actix::prelude::*;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
System::new("client-example")
|
||||||
|
.block_on(lazy(|| {
|
||||||
|
let config = Config::default();
|
||||||
|
|
||||||
|
Client::default()
|
||||||
|
.get("http://127.0.0.1:8010/")
|
||||||
|
.header("User-Agent", "Actix Web")
|
||||||
|
.authorization_signature::<_, MyError, _>(&config, "my-key-id", |s| {
|
||||||
|
Ok(s.as_bytes().to_vec())
|
||||||
|
})
|
||||||
|
.unwrap()
|
||||||
|
.send()
|
||||||
|
.map_err(|_| ())
|
||||||
|
.and_then(|mut res| res.body().map_err(|_| ()))
|
||||||
|
.map(|body| {
|
||||||
|
println!("{:?}", body);
|
||||||
|
})
|
||||||
|
}))
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum MyError {
|
||||||
|
Convert(ToStrError),
|
||||||
|
Header(InvalidHeaderValue),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<ToStrError> for MyError {
|
||||||
|
fn from(e: ToStrError) -> Self {
|
||||||
|
MyError::Convert(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<InvalidHeaderValue> for MyError {
|
||||||
|
fn from(e: InvalidHeaderValue) -> Self {
|
||||||
|
MyError::Header(e)
|
||||||
|
}
|
||||||
|
}
|
77
http-signature-normalization-actix/examples/server.rs
Normal file
77
http-signature-normalization-actix/examples/server.rs
Normal file
|
@ -0,0 +1,77 @@
|
||||||
|
use actix::System;
|
||||||
|
use actix_web::{web, App, HttpRequest, HttpServer, ResponseError};
|
||||||
|
use http_signature_normalization_actix::{prelude::*, verify::Algorithm};
|
||||||
|
use std::fmt;
|
||||||
|
|
||||||
|
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())
|
||||||
|
.route("/", web::get().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<VerifyError> for MyError {
|
||||||
|
fn from(e: VerifyError) -> Self {
|
||||||
|
MyError::Verify(e)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue