use base64::{Engine, engine::general_purpose};
use tracing::{debug, warn};
use crate::http::digest_auth::{DigestAuthChallenge, DigestAuthResponse};
use crate::http::header_processor::HttpResponseHead;
pub(crate) fn proxy_basic_auth(username: &str, password: &str) -> String {
let credentials = format!("{}:{}", username, password);
let encoded = general_purpose::STANDARD.encode(credentials.as_bytes());
format!("Basic {}", encoded)
}
pub(crate) fn proxy_digest_auth(
username: &str,
password: &str,
method: &str,
uri: &str,
challenge: &DigestAuthChallenge,
nc: u32,
) -> String {
let response = DigestAuthResponse::compute(username, password, method, uri, challenge, nc);
response.to_header_value()
}
pub(crate) fn build_proxy_auth_header(
head: &HttpResponseHead,
username: &str,
password: &str,
method: &str,
uri: &str,
nc: u32,
) -> Option<String> {
for (_, value) in head.iter_headers() {
if (value.starts_with("Digest ") || value.starts_with("digest "))
&& let Ok(challenge) = DigestAuthChallenge::parse(value)
{
let auth = proxy_digest_auth(username, password, method, uri, &challenge, nc);
debug!(
"Using Digest proxy authentication for realm='{}'",
challenge.realm
);
return Some(auth);
}
}
for (_, value) in head.iter_headers() {
if value.starts_with("Basic ") || value.starts_with("basic ") {
let auth = proxy_basic_auth(username, password);
debug!("Using Basic proxy authentication");
return Some(auth);
}
}
if head.header("proxy-authenticate").is_some() {
warn!("Unknown Proxy-Authenticate scheme, falling back to Basic");
return Some(proxy_basic_auth(username, password));
}
None
}