use tracing::{info, warn};
use crate::http::auth::{AuthConfigFactory, AuthResolveOptions};
use crate::http::request::basic_auth;
use super::types::AuthChallengeResult;
pub fn handle_basic_challenge(
auth_factory: &mut AuthConfigFactory,
host: &str,
port: u16,
path: &str,
auth_opts: &AuthResolveOptions,
is_proxy: bool,
) -> AuthChallengeResult {
let activated = is_proxy || auth_factory.activate_basic_cred(host, port, path, auth_opts);
if !activated {
warn!(
"Cannot activate BasicCred for {}:{}{} — no credentials found",
host, port, path
);
return AuthChallengeResult::NoCredentials {
status_code: if is_proxy { 407 } else { 401 },
message: "No credentials available for Basic authentication".to_string(),
};
}
let url_for_resolve = url::Url::parse(&format!("http://{}:{}{}", host, port, path))
.unwrap_or_else(|_| {
url::Url::parse("http://localhost/").expect("fallback URL must be valid")
});
let mut opts_with_challenge = auth_opts.clone();
opts_with_challenge.http_auth_challenge = true;
let auth_config = if is_proxy {
auth_factory.resolve_proxy(auth_opts)
} else {
auth_factory.resolve(&url_for_resolve, false, &opts_with_challenge)
};
match auth_config {
Some(ac) => {
let auth_header = basic_auth(ac.user(), ac.password());
info!(
"Resolved Basic auth for {}:{}{} (user={})",
host,
port,
path,
ac.user()
);
if is_proxy {
AuthChallengeResult::RetryWithAuth {
authorization_header: auth_header,
is_proxy: true,
}
} else {
AuthChallengeResult::RetryWithAuth {
authorization_header: auth_header,
is_proxy: false,
}
}
}
None => {
warn!(
"BasicCred activated but resolve returned no credentials for {}:{}{}",
host, port, path
);
AuthChallengeResult::NoCredentials {
status_code: if is_proxy { 407 } else { 401 },
message: "No credentials available after BasicCred activation".to_string(),
}
}
}
}