2019-09-21 16:26:11 +00:00
# HTTP Signature Normaliztion Actix
_An HTTP Signatures library that leaves the signing to you_
- [crates.io ](https://crates.io/crates/http-signature-normalization-actix )
- [docs.rs ](https://docs.rs/http-signature-normalization-actix )
2020-03-16 00:29:47 +00:00
- [Hit me up on Mastodon ](https://asonix.dog/@asonix )
2019-09-21 16:26:11 +00:00
Http Signature Normalization is a minimal-dependency crate for producing HTTP Signatures with user-provided signing and verification. The API is simple; there's a series of steps for creation and verification with types that ensure reasonable usage.
## Usage
This crate provides extensions the ClientRequest type from Actix Web, and provides middlewares for verifying HTTP Signatures, and optionally, Digest headers
#### First, add this crate to your dependencies
```toml
2021-02-10 21:45:51 +00:00
actix-rt = "2.0.2"
actix-web = "4.0.0-beta.3"
2020-03-16 00:29:47 +00:00
thiserror = "0.1"
2021-02-10 21:45:51 +00:00
http-signature-normalization-actix = { version = "0.5.0-beta.2", default-features = false, features = ["sha-2"] }
2020-09-30 00:08:34 +00:00
sha2 = "0.9"
2019-09-21 16:26:11 +00:00
```
#### Then, use it in your client
```rust
use actix_web::client::Client;
use http_signature_normalization_actix::prelude::*;
use sha2::{Digest, Sha256};
2020-03-16 00:29:47 +00:00
#[actix_rt::main]
async fn main() -> Result< (), Box< dyn std::error::Error > > {
let config = Config::default();
let mut digest = Sha256::new();
let mut response = Client::default()
.post("http://127.0.0.1:8010/")
.header("User-Agent", "Actix Web")
2020-09-30 00:08:34 +00:00
.authorization_signature_with_digest(& config, "my-key-id", & mut digest, "My request body", |s| {
2020-03-16 00:29:47 +00:00
Ok(base64::encode(s)) as Result< _ , MyError >
})?
2020-04-21 18:04:25 +00:00
.await?
2020-03-16 00:29:47 +00:00
.send()
.await
.map_err(|e| {
eprintln!("Error, {}", e);
MyError::SendRequest
})?;
let body = response.body().await.map_err(|e| {
eprintln!("Error, {}", e);
MyError::Body
})?;
println!("{:?}", body);
Ok(())
2019-09-21 16:26:11 +00:00
}
2020-03-16 00:29:47 +00:00
#[derive(Debug, thiserror::Error)]
2019-09-21 16:26:11 +00:00
pub enum MyError {
2020-03-16 00:29:47 +00:00
#[error("Failed to read header, {0}")]
Convert(#[from] ToStrError),
2019-09-21 16:26:11 +00:00
2020-03-16 00:29:47 +00:00
#[error("Failed to create header, {0}")]
Header(#[from] InvalidHeaderValue),
2019-09-21 16:26:11 +00:00
2020-03-16 00:29:47 +00:00
#[error("Failed to send request")]
SendRequest,
2019-09-21 16:26:11 +00:00
2020-03-16 00:29:47 +00:00
#[error("Failed to retrieve request body")]
Body,
2019-09-21 16:26:11 +00:00
}
```
#### Or, use it in your server
```rust
2020-03-16 00:29:47 +00:00
use actix_web::{http::StatusCode, web, App, HttpResponse, HttpServer, ResponseError};
use futures::future::{err, ok, Ready};
use http_signature_normalization_actix::prelude::*;
2019-09-21 16:26:11 +00:00
use sha2::{Digest, Sha256};
#[derive(Clone, Debug)]
struct MyVerify;
impl SignatureVerify for MyVerify {
type Error = MyError;
2020-03-16 00:29:47 +00:00
type Future = Ready< Result < bool , Self::Error > >;
2019-09-21 16:26:11 +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-21 16:26:11 +00:00
) -> Self::Future {
match algorithm {
Some(Algorithm::Hs2019) => (),
2020-03-16 00:29:47 +00:00
_ => return err(MyError::Algorithm),
2019-09-21 16:26:11 +00:00
};
if key_id != "my-key-id" {
2020-03-16 00:29:47 +00:00
return err(MyError::Key);
2019-09-21 16:26:11 +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-21 16:26:11 +00:00
2020-03-16 00:29:47 +00:00
ok(decoded == signing_string.as_bytes())
2019-09-21 16:26:11 +00:00
}
}
2020-03-17 19:47:00 +00:00
async fn index((_, sig_verified): (DigestVerified, SignatureVerified)) -> & 'static str {
println!("Signature verified for {}", sig_verified.key_id());
2019-09-21 16:26:11 +00:00
"Eyyyyup"
}
2020-03-16 00:29:47 +00:00
#[actix_rt::main]
async fn main() -> Result< (), Box< dyn std::error::Error > > {
2019-09-21 16:26:11 +00:00
let config = Config::default();
HttpServer::new(move || {
App::new()
.wrap(VerifyDigest::new(Sha256::new()).optional())
.wrap(
VerifySignature::new(MyVerify, config.clone())
.authorization()
.optional(),
)
.route("/", web::post().to(index))
})
.bind("127.0.0.1:8010")?
2020-03-16 00:29:47 +00:00
.run()
.await?;
2019-09-21 16:26:11 +00:00
Ok(())
}
2020-03-16 00:29:47 +00:00
#[derive(Debug, thiserror::Error)]
2019-09-21 16:26:11 +00:00
enum MyError {
2020-03-16 00:29:47 +00:00
#[error("Failed to verify, {}", _0)]
Verify(#[from] PrepareVerifyError),
2019-09-21 16:26:11 +00:00
2020-03-16 00:29:47 +00:00
#[error("Unsupported algorithm")]
2019-09-21 16:26:11 +00:00
Algorithm,
2020-03-16 00:29:47 +00:00
#[error("Couldn't decode signature")]
2019-09-21 16:26:11 +00:00
Decode,
2020-03-16 00:29:47 +00:00
#[error("Invalid key")]
2019-09-21 16:26:11 +00:00
Key,
}
impl ResponseError for MyError {
2020-03-16 00:29:47 +00:00
fn status_code(& self) -> StatusCode {
StatusCode::BAD_REQUEST
2019-09-21 16:26:11 +00:00
}
2020-03-16 00:29:47 +00:00
fn error_response(& self) -> HttpResponse {
HttpResponse::BadRequest().finish()
2019-09-21 16:26:11 +00:00
}
}
```
### Contributing
Unless otherwise stated, all contributions to this project will be licensed under the CSL with
the exceptions listed in the License section of this file.
### License
This work is licensed under the Cooperative Software License. This is not a Free Software
License, but may be considered a "source-available License." For most hobbyists, self-employed
developers, worker-owned companies, and cooperatives, this software can be used in most
projects so long as this software is distributed under the terms of the CSL. For more
information, see the provided LICENSE file. If none exists, the license can be found online
[here ](https://lynnesbian.space/csl/ ). If you are a free software project and wish to use this
software under the terms of the GNU Affero General Public License, please contact me at
[asonix@asonix.dog ](mailto:asonix@asonix.dog ) and we can sort that out. If you wish to use this
project under any other license, especially in proprietary software, the answer is likely no.
2020-04-18 19:36:53 +00:00
Http Signature Normalization Actix is currently licensed under the AGPL to the Lemmy project, found
at [github.com/LemmyNet/lemmy ](https://github.com/LemmyNet/lemmy )