Backport Sentivite headers to blocking implementation (#931)

This commit is contained in:
Damien Cuenot
2020-05-28 23:21:59 +02:00
committed by GitHub
parent d879d6f6c2
commit 248a9765b3

View File

@@ -164,7 +164,18 @@ impl RequestBuilder {
/// # Ok(()) /// # Ok(())
/// # } /// # }
/// ``` /// ```
pub fn header<K, V>(mut self, key: K, value: V) -> RequestBuilder pub fn header<K, V>(self, key: K, value: V) -> RequestBuilder
where
HeaderName: TryFrom<K>,
HeaderValue: TryFrom<V>,
<HeaderName as TryFrom<K>>::Error: Into<http::Error>,
<HeaderValue as TryFrom<V>>::Error: Into<http::Error>,
{
self.header_sensitive(key, value, false)
}
/// Add a `Header` to this Request with ability to define if header_value is sensitive.
fn header_sensitive<K, V>(mut self, key: K, value: V, sensitive: bool) -> RequestBuilder
where where
HeaderName: TryFrom<K>, HeaderName: TryFrom<K>,
HeaderValue: TryFrom<V>, HeaderValue: TryFrom<V>,
@@ -175,7 +186,8 @@ impl RequestBuilder {
if let Ok(ref mut req) = self.request { if let Ok(ref mut req) = self.request {
match <HeaderName as TryFrom<K>>::try_from(key) { match <HeaderName as TryFrom<K>>::try_from(key) {
Ok(key) => match <HeaderValue as TryFrom<V>>::try_from(value) { Ok(key) => match <HeaderValue as TryFrom<V>>::try_from(value) {
Ok(value) => { Ok(mut value) => {
value.set_sensitive(sensitive);
req.headers_mut().append(key, value); req.headers_mut().append(key, value);
} }
Err(e) => error = Some(crate::error::builder(e.into())), Err(e) => error = Some(crate::error::builder(e.into())),
@@ -242,7 +254,7 @@ impl RequestBuilder {
None => format!("{}:", username), None => format!("{}:", username),
}; };
let header_value = format!("Basic {}", encode(&auth)); let header_value = format!("Basic {}", encode(&auth));
self.header(crate::header::AUTHORIZATION, &*header_value) self.header_sensitive(crate::header::AUTHORIZATION, &*header_value, true)
} }
/// Enable HTTP bearer authentication. /// Enable HTTP bearer authentication.
@@ -261,7 +273,7 @@ impl RequestBuilder {
T: fmt::Display, T: fmt::Display,
{ {
let header_value = format!("Bearer {}", token); let header_value = format!("Bearer {}", token);
self.header(crate::header::AUTHORIZATION, &*header_value) self.header_sensitive(crate::header::AUTHORIZATION, &*header_value, true)
} }
/// Set the request body. /// Set the request body.
@@ -965,4 +977,36 @@ mod tests {
assert_eq!(req.method(), Method::GET); assert_eq!(req.method(), Method::GET);
assert_eq!(req.url().as_str(), "http://localhost/"); assert_eq!(req.url().as_str(), "http://localhost/");
} }
#[test]
fn test_basic_auth_sensitive_header() {
let client = Client::new();
let some_url = "https://localhost/";
let req = client
.get(some_url)
.basic_auth("Aladdin", Some("open sesame"))
.build()
.expect("request build");
assert_eq!(req.url().as_str(), "https://localhost/");
assert_eq!(req.headers()["authorization"], "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==");
assert_eq!(req.headers()["authorization"].is_sensitive(), true);
}
#[test]
fn test_bearer_auth_sensitive_header() {
let client = Client::new();
let some_url = "https://localhost/";
let req = client
.get(some_url)
.bearer_auth("Hold my bear")
.build()
.expect("request build");
assert_eq!(req.url().as_str(), "https://localhost/");
assert_eq!(req.headers()["authorization"], "Bearer Hold my bear");
assert_eq!(req.headers()["authorization"].is_sensitive(), true);
}
} }