Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unify basic-auth header generation #1726

Merged
merged 1 commit into from
Jan 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 1 addition & 12 deletions src/async_impl/request.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::convert::TryFrom;
use std::fmt;
use std::future::Future;
use std::io::Write;
use std::time::Duration;

use serde::Serialize;
Expand All @@ -16,7 +15,6 @@ use super::response::Response;
#[cfg(feature = "multipart")]
use crate::header::CONTENT_LENGTH;
use crate::header::{HeaderMap, HeaderName, HeaderValue, CONTENT_TYPE};
use crate::util::base64;
use crate::{Method, Url};
use http::{request::Parts, Request as HttpRequest, Version};

Expand Down Expand Up @@ -251,16 +249,7 @@ impl RequestBuilder {
U: fmt::Display,
P: fmt::Display,
{
let mut header_value = b"Basic ".to_vec();
{
let mut encoder = base64::encoder(&mut header_value);
// The unwraps here are fine because Vec::write* is infallible.
write!(encoder, "{}:", username).unwrap();
if let Some(password) = password {
write!(encoder, "{}", password).unwrap();
}
}

let header_value = crate::util::basic_auth(username, password);
self.header_sensitive(crate::header::AUTHORIZATION, header_value, true)
}

Expand Down
9 changes: 2 additions & 7 deletions src/blocking/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use super::body::{self, Body};
use super::multipart;
use super::Client;
use crate::header::{HeaderMap, HeaderName, HeaderValue, CONTENT_TYPE};
use crate::util::base64;
use crate::{async_impl, Method, Url};

/// A request which can be executed with `Client::execute()`.
Expand Down Expand Up @@ -266,12 +265,8 @@ impl RequestBuilder {
U: fmt::Display,
P: fmt::Display,
{
let auth = match password {
Some(password) => format!("{}:{}", username, password),
None => format!("{}:", username),
};
let header_value = format!("Basic {}", base64::encode(&auth));
self.header_sensitive(crate::header::AUTHORIZATION, &*header_value, true)
let header_value = crate::util::basic_auth(username, password);
self.header_sensitive(crate::header::AUTHORIZATION, header_value, true)
}

/// Enable HTTP bearer authentication.
Expand Down
8 changes: 1 addition & 7 deletions src/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use std::net::SocketAddr;
use std::sync::Arc;

use crate::into_url::{IntoUrl, IntoUrlSealed};
use crate::util::base64;
use crate::Url;
use http::{header::HeaderValue, Uri};
use ipnet::IpNet;
Expand Down Expand Up @@ -763,12 +762,7 @@ impl fmt::Debug for Custom {
}

pub(crate) fn encode_basic_auth(username: &str, password: &str) -> HeaderValue {
let val = format!("{}:{}", username, password);
let mut header = format!("Basic {}", base64::encode(&val))
.parse::<HeaderValue>()
.expect("base64 is always valid HeaderValue");
header.set_sensitive(true);
header
crate::util::basic_auth(username, Some(password))
}

/// A helper trait to allow testing `Proxy::intercept` without having to
Expand Down
29 changes: 16 additions & 13 deletions src/util.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
use crate::header::{Entry, HeaderMap, OccupiedEntry};
use crate::header::{Entry, HeaderMap, HeaderValue, OccupiedEntry};

pub(crate) mod base64 {
use std::io;

use base64::engine::GeneralPurpose;
pub fn basic_auth<U, P>(username: U, password: Option<P>) -> HeaderValue
where
U: std::fmt::Display,
P: std::fmt::Display,
{
use base64::prelude::BASE64_STANDARD;
use base64::write::EncoderWriter;
use std::io::Write;

if_hyper! {
pub fn encode<T: AsRef<[u8]>>(input: T) -> String {
use base64::Engine;
BASE64_STANDARD.encode(input)
let mut buf = b"Basic ".to_vec();
{
let mut encoder = EncoderWriter::new(&mut buf, &BASE64_STANDARD);
let _ = write!(encoder, "{}:", username);
if let Some(password) = password {
let _ = write!(encoder, "{}", password);
}
}

pub fn encoder<'a, W: io::Write>(delegate: W) -> EncoderWriter<'a, GeneralPurpose, W> {
EncoderWriter::new(delegate, &BASE64_STANDARD)
}
let mut header = HeaderValue::from_bytes(&buf).expect("base64 is always valid HeaderValue");
header.set_sensitive(true);
header
}

// xor-shift
Expand Down
13 changes: 1 addition & 12 deletions src/wasm/request.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::convert::TryFrom;
use std::fmt;
use std::io::Write;

use bytes::Bytes;
use http::{request::Parts, Method, Request as HttpRequest};
Expand All @@ -12,7 +11,6 @@ use web_sys::RequestCredentials;

use super::{Body, Client, Response};
use crate::header::{HeaderMap, HeaderName, HeaderValue, CONTENT_TYPE};
use crate::util::base64;

/// A request which can be executed with `Client::execute()`.
pub struct Request {
Expand Down Expand Up @@ -214,16 +212,7 @@ impl RequestBuilder {
U: fmt::Display,
P: fmt::Display,
{
let mut header_value = b"Basic ".to_vec();
{
let mut encoder = base64::encoder(&mut header_value);
// The unwraps here are fine because Vec::write* is infallible.
write!(encoder, "{}:", username).unwrap();
if let Some(password) = password {
write!(encoder, "{}", password).unwrap();
}
}

let header_value = crate::util::basic_auth(username, password);
self.header(crate::header::AUTHORIZATION, header_value)
}

Expand Down