use tracing::warn;
use crate::http::auth::{AuthConfigFactory, AuthResolveOptions};
use crate::http::request_response::HttpMethod;
use crate::http::skip_response::{AuthScheme, HttpAuthChallenge};
use super::basic_auth::handle_basic_challenge;
use super::digest_auth::handle_digest_challenge;
use super::types::AuthChallengeResult;
#[allow(clippy::too_many_arguments)]
pub fn handle_auth_challenge(
challenge: &HttpAuthChallenge,
auth_factory: &mut AuthConfigFactory,
url: &url::Url,
auth_opts: &AuthResolveOptions,
method: HttpMethod,
authentication_used: bool,
nc: u32,
) -> AuthChallengeResult {
let status_code = if challenge.is_proxy { 407 } else { 401 };
if authentication_used {
return AuthChallengeResult::NoCredentials {
status_code,
message: "Authentication failed: credentials already tried".to_string(),
};
}
let host = url.host_str().unwrap_or("");
let port = url
.port_or_known_default()
.unwrap_or(if url.scheme() == "https" { 443 } else { 80 });
let path = url.path();
match challenge.scheme {
AuthScheme::Basic => handle_basic_challenge(
auth_factory,
host,
port,
path,
auth_opts,
challenge.is_proxy,
),
AuthScheme::Digest => handle_digest_challenge(
auth_factory,
host,
port,
path,
auth_opts,
url,
method,
challenge,
nc,
),
AuthScheme::Ntlm => {
warn!("NTLM authentication is not supported");
AuthChallengeResult::UnsupportedScheme {
scheme: "NTLM".to_string(),
status_code,
}
}
AuthScheme::Negotiate => {
warn!("SPNEGO/Kerberos Negotiate authentication is not supported");
AuthChallengeResult::UnsupportedScheme {
scheme: "Negotiate".to_string(),
status_code,
}
}
}
}