【问题标题】:Connect to private github repository with libgit2使用 libgit2 连接到私有 github 存储库
【发布时间】:2014-02-03 00:57:31
【问题描述】:

我正在尝试使用 libgit2 获取私有 github 存储库。回购属于一个组织,我有读/写权限。该程序是用Objective-C++编写的。我有这个:

- (int)fetchBranches
{
    git_remote *remote = NULL;
    int result = 0;
    if (!(result = git_remote_load(&remote, repo, "origin"))) {
        git_remote_callbacks callbacks = GIT_REMOTE_CALLBACKS_INIT;
        callbacks.credentials = cred_acquire_cb;
        git_remote_set_callbacks(remote, &callbacks);
        result = git_remote_fetch(remote);
    }
    return result;
}

int cred_acquire_cb(git_cred **out,
                    const char * url,
                    const char * username_from_url,
                    unsigned int allowed_types,
                    void * payload)
{
    return git_cred_ssh_key_new(out, "GITHUBUSERNAME", "/Users/admin/.ssh/id_rsa.pub", "/Users/admin/.ssh/id_rsa", "PASSPHRASE");
}

这会创建凭据,但是当我尝试使用 libssh2 验证会话时,它会失败。 (libgit2/src/transsports/ssh.c:302):

rc = libssh2_userauth_publickey_fromfile(
      session, c->username, c->publickey,
      c->privatekey, c->passphrase);

错误为 -18,LIBSSH2_ERROR_AUTHENTICATION_FAILED。我已经确认公钥已添加到 Github,并尝试将其更改为新的。密码也是正确的,用户名是我的 github 用户名。我也可以通过控制台进行获取。远程配置是:

[remote "origin"]
    url = git@github.com:ORGNAME/REPONAME.git
    fetch = +refs/heads/*:refs/remotes/origin/*

我也尝试从 OSX 中的 git-agent 获取密钥,结果相同:

return git_cred_ssh_key_from_agent(out, "GITHUBUSERNAME");

最后我尝试使用明文认证:

return git_cred_userpass_plaintext_new(out, username, password);

最后一种方法在将 repo url 更改为 HTTPS 地址后有效。问题是它要求用户每次输入密码或将包含密码的 git_cred 结构存储为空白(不喜欢那样)。

有人知道为什么 ssh 身份验证失败了吗?有没有办法避免每次操作都必须输入用户名/密码(使用明文身份验证)?

【问题讨论】:

  • 你一直提到“GITHUBUERNAME”,但是对于 SSH 身份验证,ssh 用户是“git”。关键是用来确定 GitHub 用户名是什么。
  • 好吧,我太傻了。是的,用户是 git。有了这个,它就可以工作了。如果你想给出一个答案,我会认为它是有效的。谢谢
  • @Tonidero 那么这里的用户名是什么?

标签: macos authentication github ssh libgit2


【解决方案1】:

通过 ssh 交谈时的用户是“git”(因此所有 url 都是 git@github.com:user/repo),因此您应该使用它而不是 GitHub 上的“真实”用户名。您发送的密钥是服务器用来确定它正在处理的用户的密钥。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-17
    • 2017-08-29
    • 2013-08-11
    • 2013-11-27
    • 2013-01-25
    • 1970-01-01
    • 2017-12-08
    • 2019-08-09
    相关资源
    最近更新 更多