use git2::{CertificateCheckStatus, CredentialType, RemoteCallbacks};
use log::debug;
use std::sync::{Arc, Mutex};
use crate::{
auth::{git_http::try_userpass_authentication, git_ssh::ssh_authenticate_git},
config::global::BGitGlobalConfig,
};
pub fn setup_auth_callbacks<'a>(global_config: &'a BGitGlobalConfig) -> RemoteCallbacks<'a> {
let mut callbacks = RemoteCallbacks::new();
let attempt_count: Arc<Mutex<usize>> = Arc::new(Mutex::new(0));
callbacks.credentials(move |url, username_from_url, allowed_types| {
let mut count = attempt_count.lock().unwrap();
*count += 1;
let current_attempt = *count;
drop(count);
if allowed_types.contains(CredentialType::USER_PASS_PLAINTEXT) {
try_userpass_authentication(username_from_url, global_config)
} else {
ssh_authenticate_git(
url,
username_from_url,
allowed_types,
current_attempt,
global_config,
)
}
});
callbacks.certificate_check(|_cert, _host| {
debug!("Skipping certificate verification (INSECURE)");
Ok(CertificateCheckStatus::CertificateOk)
});
callbacks
}