mirror of
https://git.asonix.dog/asonix/http-signature-normalization.git
synced 2024-11-22 01:11:00 +00:00
Remove dependency on http
This commit is contained in:
parent
5daf0a78d1
commit
aefb08e627
4 changed files with 30 additions and 59 deletions
|
@ -12,4 +12,3 @@ edition = "2018"
|
|||
[dependencies]
|
||||
base64 = "0.10"
|
||||
chrono = "0.4"
|
||||
http = "0.1.18"
|
||||
|
|
|
@ -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<String>,
|
||||
|
@ -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 {
|
||||
|
|
39
src/lib.rs
39
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<Unsigned, ToStrError> {
|
||||
let (sig_headers, mut btm) = build_headers_list(headers)?;
|
||||
method: &str,
|
||||
path_and_query: &str,
|
||||
headers: &mut BTreeMap<String, String>,
|
||||
) -> 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<F, T>(&self, unvalidated: Unvalidated, f: F) -> Result<T, ValidateError>
|
||||
|
@ -81,17 +76,7 @@ impl Config {
|
|||
}
|
||||
}
|
||||
|
||||
fn build_headers_list(
|
||||
headers: &HeaderMap,
|
||||
) -> Result<(Vec<String>, BTreeMap<String, String>), ToStrError> {
|
||||
let btm: BTreeMap<String, String> = headers
|
||||
.iter()
|
||||
.map(|(k, v)| {
|
||||
v.to_str()
|
||||
.map(|v| (k.as_str().to_lowercase().to_owned(), v.to_owned()))
|
||||
})
|
||||
.collect::<Result<BTreeMap<String, String>, _>>()?;
|
||||
|
||||
fn build_headers_list(btm: &BTreeMap<String, String>) -> Vec<String> {
|
||||
let http_header_keys: Vec<String> = 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<DateTime<Utc>>,
|
||||
expires: Option<DateTime<Utc>>,
|
||||
sig_headers: &[String],
|
||||
|
|
|
@ -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<Unvalidated, ToStrError> {
|
||||
let (_, mut btm) = build_headers_list(headers)?;
|
||||
|
||||
method: &str,
|
||||
path_and_query: &str,
|
||||
headers: &mut BTreeMap<String, String>,
|
||||
) -> 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,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue