rust-lang / git2-rs

libgit2 bindings for Rust
https://docs.rs/git2
Apache License 2.0
1.67k stars 384 forks source link

how can I fallback authentication from ssh to plain username and password if no ssh key valid found #888

Open Masber opened 1 year ago

Masber commented 1 year ago

I would like to overwrite the default authentication based on remote url. For instance, I have a url like git@... which means git2-rs will expect to authenticate through ssh with user git. I want to be able to login using plain username and password instead like the example below.

I tried the following:

        cb.credentials(|_url, _username_from_url, _allowed_types| {
            log::debug!("url is: {}", _url);
            log::debug!("username from url is: {}", _username_from_url.unwrap_or("Not defined"));
            log::debug!("allowed types are: {:#?}", _allowed_types);

            let mut cred = git2::Cred::userpass_plaintext("username", "password"); an https address 'url = https://git.cscs.ch/msopena/manta.git' makes library to switch to CredentialType::USER_PASS_PLAINTEXT

            let cred_aux: git2::Cred = cred.unwrap();

            log::debug!("credentials type: {}", &cred_aux.credtype());
            log::debug!("credentials username: {}", &cred_aux.has_username());

            Ok(cred_aux)
        });

and this is what I get

[2022-10-05T16:22:29Z DEBUG manta::git2_rs_utils::local] url is: git@...
[2022-10-05T16:22:29Z DEBUG manta::git2_rs_utils::local] username from url is: git
[2022-10-05T16:22:29Z DEBUG manta::git2_rs_utils::local] allowed types are: USER_PASS_PLAINTEXT | SSH_KEY | SSH_MEMORY | SSH_CUSTOM
[2022-10-05T16:22:29Z DEBUG manta::git2_rs_utils::local] credentials type: 1
[2022-10-05T16:22:29Z DEBUG manta::git2_rs_utils::local] credentials username: true
Error: Error { code: -1, klass: 23, message: "username does not match previous request" }

Any idea how can I force the git2-rs to use a different authentication type ignoring the username in the url?