1
0
mirror of https://github.com/fafhrd91/actix-web synced 2025-06-25 06:39:22 +02:00

Added HTTP Authentication for Client #540

This commit is contained in:
Nikolay Kim
2019-03-26 20:57:06 -07:00
parent 1cca25c276
commit ab597dd98a
3 changed files with 82 additions and 32 deletions

View File

@ -242,6 +242,30 @@ impl ClientRequest {
self.header(header::CONTENT_LENGTH, wrt.get_mut().take().freeze())
}
/// Set HTTP basic authorization
pub fn basic_auth<U, P>(self, username: U, password: Option<P>) -> Self
where
U: fmt::Display,
P: fmt::Display,
{
let auth = match password {
Some(password) => format!("{}:{}", username, password),
None => format!("{}", username),
};
self.header(
header::AUTHORIZATION,
format!("Basic {}", base64::encode(&auth)),
)
}
/// Set HTTP bearer authentication
pub fn bearer_auth<T>(self, token: T) -> Self
where
T: fmt::Display,
{
self.header(header::AUTHORIZATION, format!("Bearer {}", token))
}
#[cfg(feature = "cookies")]
/// Set a cookie
///