简体   繁体   中英

LibGit2 SSH Authenication Failing

I'm trying to clone a repository using SSH with LibGit2 using this code:

#include <string>
#include <iostream>
#include "main.h"
#include "git2.h"

using namespace std;

int creds(git_cred **out, const char *url, const char *username_from_url,
             unsigned int allowed_types, void *payload) {
    cout << "Calling creds" << endl;
    return git_credential_ssh_key_new(out, username_from_url,
            "path_to_home/.ssh/id_rsa.pub", "path_to_home/.ssh/id_rsa", "password");
}

int main() {
    git_libgit2_init();
    string url = "git@github.com:Black-Photon/Git-Testing.git";

    git_repository *repo = nullptr;
    git_clone_options ops = GIT_CLONE_OPTIONS_INIT;
    ops.fetch_opts.callbacks.credentials = creds;

    int err = git_clone(&repo, url.c_str(), "path_to_home/Documents/temp/Git-Testing", &ops);
    cout << giterr_last()->message << endl;

    git_libgit2_shutdown();
}

However the output I get is:

Calling creds
Calling creds
Calling creds
...

Suggesting it isn't accepting the SSH key and password. However when I use the same key and password in ordinary Git:

git clone git@github.com:Black-Photon/Git-Testing.git
Cloning into 'Git-Testing'...
Enter passphrase for key 'path_to_home/.ssh/id_rsa': password
remote: Enumerating objects: 23, done.
remote: Counting objects: 100% (23/23), done.
remote: Compressing objects: 100% (17/17), done.
remote: Total 29 (delta 0), reused 13 (delta 0), pack-reused 6
Receiving objects: 100% (29/29), 4.58 KiB | 426.00 KiB/s, done.

It works normally. Why is it failing using LibGit2?

I'm using LibGit2 version 0.99.0. I have tried with both a kept and removed empty last line in id_rsa and previously on LibGit2 version 0.26.0 gave a different message: Failed to authenticate SSH session: Callback returned error . I have also tried using git_credential_ssh_key_memory_new with the same results.

The SSH library being used, 1.8.0, is outdated and must be updated to 1.9.0.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM