http-signature-normalization/http/src/lib.rs

180 lines
5.3 KiB
Rust
Raw Normal View History

2019-09-21 16:26:11 +00:00
#![deny(missing_docs)]
//! Integration of Http Signature Normalization with the HTTP crate
//!
//! This provides a thin wrapper around transforming HTTP's `HeaderMap`, `PathAndQuery`, and
//! `Method` types into BTreeMaps and Strings for signing and verifying requests
2019-09-11 22:05:58 +00:00
use http::{
header::{HeaderMap, ToStrError},
method::Method,
uri::PathAndQuery,
};
use std::{collections::BTreeMap, error::Error, fmt};
use self::{create::Unsigned, verify::Unverified};
2019-09-21 16:26:11 +00:00
/// Export useful types signing and verifying requests
2019-09-11 22:05:58 +00:00
pub mod prelude {
pub use http::{
header::{HeaderMap, InvalidHeaderValue, ToStrError},
method::Method,
uri::PathAndQuery,
};
pub use crate::create::{Signed, Unsigned};
pub use crate::verify::{
Algorithm, DeprecatedAlgorithm, ParseSignatureError, ParsedHeader, Unvalidated, Unverified,
ValidateError,
};
2019-09-21 16:26:11 +00:00
pub use crate::{Config, PrepareVerifyError};
2019-09-11 22:05:58 +00:00
}
pub mod create;
2019-09-21 16:26:11 +00:00
/// Export types used for signature verification
2019-09-11 22:05:58 +00:00
pub mod verify {
pub use http_signature_normalization::verify::{
Algorithm, DeprecatedAlgorithm, ParseSignatureError, ParsedHeader, Unvalidated, Unverified,
ValidateError,
};
}
#[derive(Clone, Default)]
2019-09-21 16:26:11 +00:00
/// Thinly wrap Http Signature Normalization's config type
2019-09-11 22:05:58 +00:00
pub struct Config {
2019-09-21 16:26:11 +00:00
/// Expose the inner Config
2019-09-11 22:05:58 +00:00
pub config: http_signature_normalization::Config,
}
#[derive(Debug)]
2019-09-21 16:26:11 +00:00
/// Errors produced when preparing to verify an Http Signature
pub enum PrepareVerifyError {
/// There was an error in the underlying library
Sig(http_signature_normalization::PrepareVerifyError),
/// There was an error producing a String from the HeaderValue
2019-09-11 22:05:58 +00:00
Header(ToStrError),
}
2022-11-29 00:34:13 +00:00
#[derive(Debug)]
/// Errors produced when preparing to sign an Http Signature
pub enum PrepareSignError {
/// There was an error in the underlying library
Required(http_signature_normalization::RequiredError),
/// There was an error producing a String from the HeaderValue
Header(ToStrError),
}
2019-09-11 22:05:58 +00:00
impl Config {
2019-09-21 16:26:11 +00:00
/// Begin the process of signing a request
///
/// The types required from this function can be produced from http's Request and URI types.
2019-09-11 22:05:58 +00:00
pub fn begin_sign(
&self,
method: &Method,
path_and_query: Option<&PathAndQuery>,
headers: HeaderMap,
2022-11-29 00:34:13 +00:00
) -> Result<Unsigned, PrepareSignError> {
2019-09-11 22:05:58 +00:00
let headers = headers
.iter()
.map(|(k, v)| v.to_str().map(|v| (k.to_string(), v.to_string())))
.collect::<Result<BTreeMap<_, _>, ToStrError>>()?;
let path_and_query = path_and_query
.map(|p| p.to_string())
2022-11-29 00:34:13 +00:00
.unwrap_or_else(|| String::from("/"));
2019-09-11 22:05:58 +00:00
let unsigned = self
.config
2022-11-29 00:34:13 +00:00
.begin_sign(method.as_ref(), &path_and_query, headers)?;
2019-09-11 22:05:58 +00:00
Ok(Unsigned { unsigned })
}
2019-09-21 16:26:11 +00:00
/// Begin the process of verifying a request
///
/// The types required from this function can be produced from http's Request and URI types.
2019-09-11 22:05:58 +00:00
pub fn begin_verify(
&self,
method: &Method,
path_and_query: Option<&PathAndQuery>,
headers: HeaderMap,
2019-09-21 16:26:11 +00:00
) -> Result<Unverified, PrepareVerifyError> {
2019-09-11 22:05:58 +00:00
let headers = headers
.iter()
.map(|(k, v)| v.to_str().map(|v| (k.to_string(), v.to_string())))
.collect::<Result<BTreeMap<_, _>, ToStrError>>()?;
let path_and_query = path_and_query
.map(|p| p.to_string())
2022-11-29 00:34:13 +00:00
.unwrap_or_else(|| String::from("/"));
2019-09-11 22:05:58 +00:00
let unverified = self
.config
2022-11-29 00:34:13 +00:00
.begin_verify(method.as_ref(), &path_and_query, headers)?;
2019-09-11 22:05:58 +00:00
Ok(unverified)
}
}
2019-09-21 16:26:11 +00:00
impl fmt::Display for PrepareVerifyError {
2019-09-11 22:05:58 +00:00
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
2019-09-21 16:26:11 +00:00
PrepareVerifyError::Sig(ref e) => write!(f, "Sig error, {}", e),
PrepareVerifyError::Header(ref e) => write!(f, "Header error, {}", e),
2019-09-11 22:05:58 +00:00
}
}
}
2022-11-29 00:34:13 +00:00
impl fmt::Display for PrepareSignError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2019-09-11 22:05:58 +00:00
match *self {
2022-11-29 00:34:13 +00:00
PrepareSignError::Required(ref e) => write!(f, "Required error, {}", e),
PrepareSignError::Header(ref e) => write!(f, "Header error, {}", e),
2019-09-11 22:05:58 +00:00
}
}
2022-11-29 00:34:13 +00:00
}
2019-09-11 22:05:58 +00:00
2022-11-29 00:34:13 +00:00
impl Error for PrepareVerifyError {
2019-09-11 22:05:58 +00:00
fn source(&self) -> Option<&(dyn Error + 'static)> {
match *self {
2019-09-21 16:26:11 +00:00
PrepareVerifyError::Sig(ref e) => Some(e),
PrepareVerifyError::Header(ref e) => Some(e),
2019-09-11 22:05:58 +00:00
}
}
}
2022-11-29 00:34:13 +00:00
impl Error for PrepareSignError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match *self {
PrepareSignError::Required(ref e) => Some(e),
PrepareSignError::Header(ref e) => Some(e),
}
}
}
2019-09-21 16:26:11 +00:00
impl From<http_signature_normalization::PrepareVerifyError> for PrepareVerifyError {
fn from(e: http_signature_normalization::PrepareVerifyError) -> Self {
PrepareVerifyError::Sig(e)
2019-09-11 22:05:58 +00:00
}
}
2019-09-21 16:26:11 +00:00
impl From<ToStrError> for PrepareVerifyError {
2019-09-11 22:05:58 +00:00
fn from(e: ToStrError) -> Self {
2019-09-21 16:26:11 +00:00
PrepareVerifyError::Header(e)
2019-09-11 22:05:58 +00:00
}
}
2022-11-29 00:34:13 +00:00
impl From<http_signature_normalization::RequiredError> for PrepareSignError {
fn from(e: http_signature_normalization::RequiredError) -> Self {
PrepareSignError::Required(e)
}
}
impl From<ToStrError> for PrepareSignError {
fn from(e: ToStrError) -> Self {
PrepareSignError::Header(e)
}
}