diff --git a/Cargo.toml b/Cargo.toml index 3e186940d..f5b3f4b6f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -76,6 +76,7 @@ __rustls = ["hyper-rustls", "tokio-rustls", "rustls", "__tls", "rustls-pemfile"] __internal_proxy_sys_no_cache = [] [dependencies] +base64 = "0.13" http = "0.2" url = "2.2" bytes = "1.0" @@ -90,7 +91,6 @@ serde_json = { version = "1.0", optional = true } mime_guess = { version = "2.0", default-features = false, optional = true } [target.'cfg(not(target_arch = "wasm32"))'.dependencies] -base64 = "0.13" encoding_rs = "0.8" futures-core = { version = "0.3.0", default-features = false } futures-util = { version = "0.3.0", default-features = false } diff --git a/src/wasm/request.rs b/src/wasm/request.rs index a939cc7fd..51adc11e5 100644 --- a/src/wasm/request.rs +++ b/src/wasm/request.rs @@ -1,7 +1,9 @@ use std::convert::TryFrom; use std::fmt; +use std::io::Write; use bytes::Bytes; +use base64::write::EncoderWriter as Base64Encoder; use http::{request::Parts, Method, Request as HttpRequest}; use serde::Serialize; #[cfg(feature = "json")] @@ -197,6 +199,25 @@ impl RequestBuilder { self } + /// Enable HTTP basic authentication. + pub fn basic_auth(self, username: U, password: Option

) -> 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(self, token: T) -> RequestBuilder where