Skip to content

Commit

Permalink
[#1095]: Implement basic_auth for WASM
Browse files Browse the repository at this point in the history
Signed-off-by: Jens Reimann <jreimann@redhat.com>
  • Loading branch information
ctron authored and seanmonstar committed May 30, 2022
1 parent c4d5094 commit 28840af
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -91,7 +92,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 }
Expand Down
21 changes: 21 additions & 0 deletions src/wasm/request.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 28840af

Please sign in to comment.