[#1095]: Implement basic_auth for WASM

Signed-off-by: Jens Reimann <jreimann@redhat.com>
This commit is contained in:
Jens Reimann
2020-11-30 10:01:22 +01:00
committed by Sean McArthur
parent c4d5094522
commit 28840afd46
2 changed files with 22 additions and 1 deletions

View File

@@ -1,6 +1,8 @@
use std::convert::TryFrom;
use std::fmt;
use std::io::Write;
use base64::write::EncoderWriter as Base64Encoder;
use bytes::Bytes;
use http::{request::Parts, Method, Request as HttpRequest};
use serde::Serialize;
@@ -206,6 +208,25 @@ impl RequestBuilder {
self
}
/// Enable HTTP basic authentication.
pub fn basic_auth<U, P>(self, username: U, password: Option<P>) -> RequestBuilder
where
U: fmt::Display,
P: fmt::Display,
{
let mut header_value = b"Basic ".to_vec();
{
let mut encoder = Base64Encoder::new(&mut header_value, base64::STANDARD);
// The unwraps here are fine because Vec::write* is infallible.
write!(encoder, "{}:", username).unwrap();
if let Some(password) = password {
write!(encoder, "{}", password).unwrap();
}
}
self.header(crate::header::AUTHORIZATION, header_value)
}
/// Enable HTTP bearer authentication.
pub fn bearer_auth<T>(self, token: T) -> RequestBuilder
where