From aefb08e627b676092675cf39d728a08dc93b7318 Mon Sep 17 00:00:00 2001 From: asonix Date: Wed, 11 Sep 2019 00:24:04 -0500 Subject: [PATCH] Remove dependency on http --- Cargo.toml | 1 - src/create.rs | 19 ++++--------------- src/lib.rs | 39 ++++++++++++--------------------------- src/verify.rs | 30 ++++++++++++++---------------- 4 files changed, 30 insertions(+), 59 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 0f6b980..309ceea 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,4 +12,3 @@ edition = "2018" [dependencies] base64 = "0.10" chrono = "0.4" -http = "0.1.18" diff --git a/src/create.rs b/src/create.rs index 3ecf6d7..a212046 100644 --- a/src/create.rs +++ b/src/create.rs @@ -1,13 +1,10 @@ use chrono::{DateTime, Utc}; -use http::header::{HeaderMap, HeaderName, HeaderValue, InvalidHeaderValue, AUTHORIZATION}; use crate::{ ALGORITHM_FIELD, ALGORITHM_VALUE, CREATED_FIELD, EXPIRES_FIELD, HEADERS_FIELD, KEY_ID_FIELD, SIGNATURE_FIELD, }; -const SIGNATURE_HEADER: &'static str = "Signature"; - pub struct Signed { signature: String, sig_headers: Vec, @@ -24,20 +21,12 @@ pub struct Unsigned { } impl Signed { - pub fn signature_header(self, hm: &mut HeaderMap) -> Result<(), InvalidHeaderValue> { - hm.insert( - AUTHORIZATION, - HeaderValue::from_str(&format!("Signature {}", self.into_header()))?, - ); - Ok(()) + pub fn signature_header(self) -> String { + format!("Signature {}", self.into_header()) } - pub fn authorization_header(self, hm: &mut HeaderMap) -> Result<(), InvalidHeaderValue> { - hm.insert( - HeaderName::from_static(SIGNATURE_HEADER), - HeaderValue::from_str(&self.into_header())?, - ); - Ok(()) + pub fn authorization_header(self) -> String { + self.into_header() } fn into_header(self) -> String { diff --git a/src/lib.rs b/src/lib.rs index 7996a54..825fbf8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,9 +1,4 @@ use chrono::{DateTime, Duration, Utc}; -use http::{ - header::{HeaderMap, ToStrError}, - method::Method, - uri::PathAndQuery, -}; use std::collections::BTreeMap; pub mod create; @@ -34,11 +29,11 @@ pub struct Config { impl Config { pub fn normalize( &self, - method: Method, - path_and_query: &PathAndQuery, - headers: &HeaderMap, - ) -> Result { - let (sig_headers, mut btm) = build_headers_list(headers)?; + method: &str, + path_and_query: &str, + headers: &mut BTreeMap, + ) -> Unsigned { + let sig_headers = build_headers_list(headers); let created = Utc::now(); let expires = created + self.expires; @@ -49,15 +44,15 @@ impl Config { Some(created), Some(expires), &sig_headers, - &mut btm, + headers, ); - Ok(Unsigned { + Unsigned { signing_string, sig_headers, created, expires, - }) + } } pub fn validate(&self, unvalidated: Unvalidated, f: F) -> Result @@ -81,17 +76,7 @@ impl Config { } } -fn build_headers_list( - headers: &HeaderMap, -) -> Result<(Vec, BTreeMap), ToStrError> { - let btm: BTreeMap = headers - .iter() - .map(|(k, v)| { - v.to_str() - .map(|v| (k.as_str().to_lowercase().to_owned(), v.to_owned())) - }) - .collect::, _>>()?; - +fn build_headers_list(btm: &BTreeMap) -> Vec { let http_header_keys: Vec = btm.keys().cloned().collect(); let mut sig_headers = vec![ @@ -102,12 +87,12 @@ fn build_headers_list( sig_headers.extend(http_header_keys); - Ok((sig_headers, btm)) + sig_headers } fn build_signing_string( - method: Method, - path_and_query: &PathAndQuery, + method: &str, + path_and_query: &str, created: Option>, expires: Option>, sig_headers: &[String], diff --git a/src/verify.rs b/src/verify.rs index d5bd900..195d3a0 100644 --- a/src/verify.rs +++ b/src/verify.rs @@ -1,14 +1,14 @@ use chrono::{DateTime, TimeZone, Utc}; -use http::{ - header::{HeaderMap, ToStrError}, - method::Method, - uri::PathAndQuery, +use std::{ + collections::{BTreeMap, HashMap}, + error::Error, + fmt, + str::FromStr, }; -use std::{collections::HashMap, error::Error, fmt, str::FromStr}; use crate::{ - build_headers_list, build_signing_string, ALGORITHM_FIELD, CREATED, CREATED_FIELD, - EXPIRES_FIELD, HEADERS_FIELD, KEY_ID_FIELD, SIGNATURE_FIELD, + build_signing_string, ALGORITHM_FIELD, CREATED, CREATED_FIELD, EXPIRES_FIELD, HEADERS_FIELD, + KEY_ID_FIELD, SIGNATURE_FIELD, }; pub struct Unvalidated { @@ -76,22 +76,20 @@ impl Unvalidated { impl ParsedHeader { pub fn to_unvalidated( self, - method: Method, - path_and_query: &PathAndQuery, - headers: &HeaderMap, - ) -> Result { - let (_, mut btm) = build_headers_list(headers)?; - + method: &str, + path_and_query: &str, + headers: &mut BTreeMap, + ) -> Unvalidated { let signing_string = build_signing_string( method, path_and_query, self.created, self.expires, &self.headers, - &mut btm, + headers, ); - Ok(Unvalidated { + Unvalidated { key_id: self.key_id, signature: self.signature, parsed_at: self.parsed_at, @@ -99,7 +97,7 @@ impl ParsedHeader { created: self.created, expires: self.expires, signing_string, - }) + } } }