gitauthenticationgogiteago-git

go-git: How to authenticate at remote server?


I'm using the go project go-git as a git-client and want to fetch from a private git-repository hosted via gitea.

The appropiate function to do that is func (r *Remote) Fetch(o *FetchOptions) error, which expects an transport.AuthMethod object for authentication.

I tried the following:

repo, _ := git.PlainOpen("/path/to/project/folder")
err := repo.Fetch(&git.FetchOptions{
    Auth: http.NewBasicAuth("someUser", "andThePassword"),
})

...which simply returns:

invalid auth method

The same happens if I use

authenticator, _ := ssh.NewSSHAgentAuth("git")

from the package "gopkg.in/src-d/go-git.v4/plumbing/transport/ssh".

And also if I use a certificate:

authenticator, _:= ssh.NewPublicKeysFromFile("gitea name", "/home/name/.ssh/id_rsa", "passphrase")

How do I find out which authentication method is supported, and are there existing implementations of transport.AuthMethod that I could use?


Solution

  • The problem here was, that I was using the wrong http-package to acquire the authenticator.

    I used the BasicAuth() from the package net/http - however, the correct http package to use is gopkg.in/src-d/go-git.v4/plumbing/transport/http.

    When changing the import and using the (compatible) BasicAuth-authenticator from there, it works flawlessly.