Added HTTP Authentication for Client

This commit is contained in:
Stephen Ellis 2018-10-09 12:24:04 -07:00
parent 4d17a9afcc
commit 55d95b73c4
1 changed files with 24 additions and 0 deletions

View File

@ -12,6 +12,7 @@ use serde::Serialize;
use serde_json; use serde_json;
use serde_urlencoded; use serde_urlencoded;
use url::Url; use url::Url;
use base64::encode;
use super::connector::{ClientConnector, Connection}; use super::connector::{ClientConnector, Connection};
use super::pipeline::SendRequest; use super::pipeline::SendRequest;
@ -484,6 +485,29 @@ impl ClientRequestBuilder {
self self
} }
/// Set HTTP basic authorization
pub fn basic_auth<U, P>(&mut self, username: U, password: Option<P>) -> &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<T>( &mut self, token: T) -> &mut Self
where
T: fmt::Display,
{
let header_value = format!("Bearer {}", token);
self.header(header::AUTHORIZATION, &*header_value)
}
/// Set content length /// Set content length
#[inline] #[inline]
pub fn content_length(&mut self, len: u64) -> &mut Self { pub fn content_length(&mut self, len: u64) -> &mut Self {