diff --git a/CHANGES.md b/CHANGES.md index 1a3260286..8cce71597 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -8,6 +8,8 @@ * Add `insert` and `remove` methods to `HttpResponseBuilder` +* Add client HTTP Authentication methods `.basic_auth()` and `.bearer_auth()`. #540 + ### Fixed * Ignored the `If-Modified-Since` if `If-None-Match` is specified. #680 diff --git a/src/client/request.rs b/src/client/request.rs index ad08ad135..89789933c 100644 --- a/src/client/request.rs +++ b/src/client/request.rs @@ -12,6 +12,7 @@ use serde::Serialize; use serde_json; use serde_urlencoded; use url::Url; +use base64::encode; use super::connector::{ClientConnector, Connection}; use super::pipeline::SendRequest; @@ -485,6 +486,29 @@ impl ClientRequestBuilder { self } + /// Set HTTP basic authorization + pub fn basic_auth(&mut self, username: U, password: Option
) -> &mut Self
+ where
+ U: fmt::Display,
+ P: fmt::Display,
+ {
+ let auth = match password {
+ Some(password) => format!("{}:{}", username, password),
+ None => format!("{}", username)
+ };
+ let header_value = format!("Basic {}", encode(&auth));
+ self.header(header::AUTHORIZATION, &*header_value)
+ }
+
+ /// Set HTTP bearer authentication
+ pub fn bearer_auth