Reimplemented RequestBuilder::basic_auth to use Base64Encoder (#713)
This commit is contained in:
committed by
Sean McArthur
parent
960e33cd48
commit
3a24cc1d4b
@@ -1,7 +1,10 @@
|
|||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::future::Future;
|
use std::future::Future;
|
||||||
|
use std::io::Write;
|
||||||
|
|
||||||
use base64::encode;
|
use base64;
|
||||||
|
use base64::write::EncoderWriter as Base64Encoder;
|
||||||
|
use bytes::Bytes;
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
#[cfg(feature = "json")]
|
#[cfg(feature = "json")]
|
||||||
use serde_json;
|
use serde_json;
|
||||||
@@ -153,12 +156,17 @@ impl RequestBuilder {
|
|||||||
U: fmt::Display,
|
U: fmt::Display,
|
||||||
P: fmt::Display,
|
P: fmt::Display,
|
||||||
{
|
{
|
||||||
let auth = match password {
|
let mut header_value = b"Basic ".to_vec();
|
||||||
Some(password) => format!("{}:{}", username, password),
|
{
|
||||||
None => format!("{}:", username),
|
let mut encoder = Base64Encoder::new(&mut header_value, base64::STANDARD);
|
||||||
};
|
// The unwraps here are fine because Vec::write* is infallible.
|
||||||
let header_value = format!("Basic {}", encode(&auth));
|
write!(encoder, "{}:", username).unwrap();
|
||||||
self.header(crate::header::AUTHORIZATION, &*header_value)
|
if let Some(password) = password {
|
||||||
|
write!(encoder, "{}", password).unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
self.header(crate::header::AUTHORIZATION, Bytes::from(header_value))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Enable HTTP bearer authentication.
|
/// Enable HTTP bearer authentication.
|
||||||
@@ -167,7 +175,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(crate::header::AUTHORIZATION, header_value)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set the request body.
|
/// Set the request body.
|
||||||
|
|||||||
Reference in New Issue
Block a user