129 lines
3.3 KiB
Rust
129 lines
3.3 KiB
Rust
use http::{
|
|
header::{HeaderMap, ToStrError},
|
|
method::Method,
|
|
uri::PathAndQuery,
|
|
};
|
|
use std::{collections::BTreeMap, error::Error, fmt};
|
|
|
|
use self::{create::Unsigned, verify::Unverified};
|
|
|
|
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,
|
|
};
|
|
|
|
pub use crate::{Config, VerifyError};
|
|
}
|
|
|
|
pub mod create;
|
|
|
|
pub mod verify {
|
|
pub use http_signature_normalization::verify::{
|
|
Algorithm, DeprecatedAlgorithm, ParseSignatureError, ParsedHeader, Unvalidated, Unverified,
|
|
ValidateError,
|
|
};
|
|
}
|
|
|
|
#[derive(Clone, Default)]
|
|
pub struct Config {
|
|
pub config: http_signature_normalization::Config,
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub enum VerifyError {
|
|
Sig(http_signature_normalization::VerifyError),
|
|
Header(ToStrError),
|
|
}
|
|
|
|
impl Config {
|
|
pub fn begin_sign(
|
|
&self,
|
|
method: &Method,
|
|
path_and_query: Option<&PathAndQuery>,
|
|
headers: HeaderMap,
|
|
) -> Result<Unsigned, ToStrError> {
|
|
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())
|
|
.unwrap_or(String::from("/"));
|
|
|
|
let unsigned = self
|
|
.config
|
|
.begin_sign(&method.to_string(), &path_and_query, headers);
|
|
|
|
Ok(Unsigned { unsigned })
|
|
}
|
|
|
|
pub fn begin_verify(
|
|
&self,
|
|
method: &Method,
|
|
path_and_query: Option<&PathAndQuery>,
|
|
headers: HeaderMap,
|
|
) -> Result<Unverified, VerifyError> {
|
|
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())
|
|
.unwrap_or(String::from("/"));
|
|
|
|
let unverified = self
|
|
.config
|
|
.begin_verify(&method.to_string(), &path_and_query, headers)?;
|
|
|
|
Ok(unverified)
|
|
}
|
|
}
|
|
|
|
impl fmt::Display for VerifyError {
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
match *self {
|
|
VerifyError::Sig(ref e) => write!(f, "Sig error, {}", e),
|
|
VerifyError::Header(ref e) => write!(f, "Header error, {}", e),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Error for VerifyError {
|
|
fn description(&self) -> &str {
|
|
match *self {
|
|
VerifyError::Sig(ref e) => e.description(),
|
|
VerifyError::Header(ref e) => e.description(),
|
|
}
|
|
}
|
|
|
|
fn source(&self) -> Option<&(dyn Error + 'static)> {
|
|
match *self {
|
|
VerifyError::Sig(ref e) => Some(e),
|
|
VerifyError::Header(ref e) => Some(e),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl From<http_signature_normalization::VerifyError> for VerifyError {
|
|
fn from(e: http_signature_normalization::VerifyError) -> Self {
|
|
VerifyError::Sig(e)
|
|
}
|
|
}
|
|
|
|
impl From<ToStrError> for VerifyError {
|
|
fn from(e: ToStrError) -> Self {
|
|
VerifyError::Header(e)
|
|
}
|
|
}
|