Skip to content

Commit

Permalink
Clone less
Browse files Browse the repository at this point in the history
  • Loading branch information
asonix committed Dec 23, 2017
1 parent f309b42 commit d5dbc9a
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 72 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl GetKey for MyKeyGetter {
type Key = Cursor<Vec<u8>>;
type Error = ..;

fn get_key(self, _key_id: String) -> Result<Self::Key, ..> {
fn get_key(self, _key_id: &str) -> Result<Self::Key, ..> {
Ok(Cursor::new(self.key.clone()))
}
}
Expand Down Expand Up @@ -170,7 +170,7 @@ impl GetKey for MyKeyGetter {
type Key = Cursor<Vec<u8>>;
type Error = ..;

fn get_key(self, _key_id: String) -> Result<Self::Key, Self::Error> {
fn get_key(self, _key_id: &str) -> Result<Self::Key, Self::Error> {
Ok(Cursor::new(self.key.clone()))
}
}
Expand Down
2 changes: 1 addition & 1 deletion examples/hyper_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl GetKey for MyKeyGetter {
type Key = Cursor<Vec<u8>>;
type Error = Error;

fn get_key(self, _key_id: String) -> Result<Self::Key, Self::Error> {
fn get_key(self, _key_id: &str) -> Result<Self::Key, Self::Error> {
Ok(Cursor::new(self.key.clone()))
}
}
Expand Down
2 changes: 1 addition & 1 deletion examples/rocket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl GetKey for MyKeyGetter {
type Key = Cursor<Vec<u8>>;
type Error = Error;

fn get_key(self, _key_id: String) -> Result<Self::Key, Self::Error> {
fn get_key(self, _key_id: &str) -> Result<Self::Key, Self::Error> {
Ok(Cursor::new(self.key.clone()))
}
}
Expand Down
26 changes: 15 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
// along with HTTP Signatures If not, see <http://www.gnu.org/licenses/>.

#![feature(try_from)]
#![feature(underscore_lifetimes)]

#[cfg(feature = "use_hyper")]
extern crate hyper;
Expand Down Expand Up @@ -44,6 +45,8 @@ pub use create::{AsHttpSignature, WithHttpSignature, HttpSignature, SigningStrin
pub use verify::{AuthorizationHeader, VerifyAuthorizationHeader, GetKey};
pub use error::{Error, DecodeError, VerificationError};

const REQUEST_TARGET: &'static str = "(request-target)";

#[derive(Debug)]
pub enum ShaSize {
TwoFiftySix,
Expand Down Expand Up @@ -103,13 +106,14 @@ mod tests {
use std::io::Cursor;
use std::fs::File;

use super::HttpSignature;
use super::SignatureAlgorithm;
use super::SigningString;
use super::GetKey;
use super::AuthorizationHeader;
use super::ShaSize;
use super::VerificationError;
use super::REQUEST_TARGET;
use create::HttpSignature;
use create::SigningString;
use verify::GetKey;
use verify::AuthorizationHeader;
use error::VerificationError;
use create::Signature;

struct HmacKeyGetter {
Expand All @@ -120,7 +124,7 @@ mod tests {
type Key = Cursor<Vec<u8>>;
type Error = VerificationError;

fn get_key(self, _: String) -> Result<Self::Key, Self::Error> {
fn get_key(self, _: &str) -> Result<Self::Key, Self::Error> {
Ok(Cursor::new(self.key))
}
}
Expand All @@ -133,7 +137,7 @@ mod tests {
type Key = File;
type Error = VerificationError;

fn get_key(self, _: String) -> Result<Self::Key, Self::Error> {
fn get_key(self, _: &str) -> Result<Self::Key, Self::Error> {
Ok(self.key)
}
}
Expand Down Expand Up @@ -187,7 +191,7 @@ mod tests {
let mut headers_one: HashMap<String, Vec<String>> = HashMap::new();
headers_one.insert("Accept".into(), vec!["application/json".into()]);
headers_one.insert(
"(request-target)".into(),
REQUEST_TARGET.into(),
vec![format!("{} {}?{}", method.to_lowercase(), path, query)],
);

Expand All @@ -204,7 +208,7 @@ mod tests {
let signature: Signature = signing_string.try_into().unwrap();

let auth_header = signature.authorization();
let auth_header = AuthorizationHeader::new(auth_header).unwrap();
let auth_header = AuthorizationHeader::new(&auth_header).unwrap();

auth_header
.verify(&headers_two, method, path, Some(query), key_getter)
Expand All @@ -224,7 +228,7 @@ mod tests {
let mut headers_one: HashMap<String, Vec<String>> = HashMap::new();
headers_one.insert("Accept".into(), vec!["application/json".into()]);
headers_one.insert(
"(request-target)".into(),
REQUEST_TARGET.into(),
vec![format!("{} {}?{}", method.to_lowercase(), path, query)],
);

Expand All @@ -240,7 +244,7 @@ mod tests {
let signature: Signature = signing_string.try_into().unwrap();

let auth_header = signature.authorization();
let auth_header = AuthorizationHeader::new(auth_header).unwrap();
let auth_header = AuthorizationHeader::new(&auth_header).unwrap();

auth_header
.verify(&headers_two, method, path, Some(query), key_getter)
Expand Down
4 changes: 2 additions & 2 deletions src/use_hyper_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use std::io::Read;
use std::collections::HashMap;

use error::Error;
use super::SignatureAlgorithm;
use super::{SignatureAlgorithm, REQUEST_TARGET};
use create::{AsHttpSignature, WithHttpSignature, HttpSignature};

use hyper::Request as HyperRequest;
Expand All @@ -34,7 +34,7 @@ where
) -> Result<HttpSignature<T>, Error> {
let mut headers = HashMap::new();
headers.insert(
"(request-target)".into(),
REQUEST_TARGET.into(),
vec![
if let Some(ref query) = self.uri().query() {
format!(
Expand Down
13 changes: 9 additions & 4 deletions src/use_hyper_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,22 @@ impl VerifyAuthorizationHeader for Request {
VerificationError::HeaderNotPresent,
)?;

let auth_header = AuthorizationHeader::new(auth_header.clone())?;
let auth_header = AuthorizationHeader::new(auth_header)?;

let headers: Vec<(String, String)> = self.headers()
let headers: Vec<(&str, String)> = self.headers()
.iter()
.map(|header_view| {
(header_view.name().into(), header_view.value_string())
(header_view.name(), header_view.value_string())
})
.collect();

let headers_borrowed: Vec<(&str, &str)> = headers
.iter()
.map(|&(key, ref val)| (key, val.as_ref()))
.collect();

auth_header.verify(
headers.as_ref(),
headers_borrowed.as_ref(),
&self.method().as_ref().to_lowercase(),
self.path(),
self.query(),
Expand Down
4 changes: 2 additions & 2 deletions src/use_reqwest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use std::io::Read;
use std::collections::HashMap;

use error::Error;
use super::SignatureAlgorithm;
use super::{SignatureAlgorithm, REQUEST_TARGET};
use create::{AsHttpSignature, WithHttpSignature, HttpSignature};

use reqwest::Request as ReqwestRequest;
Expand All @@ -34,7 +34,7 @@ where
) -> Result<HttpSignature<T>, Error> {
let mut headers = HashMap::new();
headers.insert(
"(request-target)".into(),
REQUEST_TARGET.into(),
vec![
if let Some(ref query) = self.url().query() {
format!(
Expand Down
9 changes: 7 additions & 2 deletions src/use_rocket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,20 @@ impl<'r> VerifyAuthorizationHeader for Request<'r> {
VerificationError::HeaderNotPresent,
)?;

let auth_header = AuthorizationHeader::new(String::from(auth_header))?;
let auth_header = AuthorizationHeader::new(auth_header)?;

let headers: Vec<(String, String)> = self.headers()
.iter()
.map(|header| (header.name().into(), header.value().into()))
.collect();

let headers_borrowed: Vec<(&str, &str)> = headers
.iter()
.map(|&(ref key, ref val)| (key.as_ref(), val.as_ref()))
.collect();

auth_header.verify(
headers.as_ref(),
headers_borrowed.as_ref(),
self.method().as_str(),
self.uri().path(),
self.uri().query(),
Expand Down
Loading

0 comments on commit d5dbc9a

Please sign in to comment.