简体   繁体   中英

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?

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.

This worked for me with gopkg.in/src-d/go-git.v4

func cloneGit() (err error) {
    username := "dude"
    password := "super-secret-Personal-access-token"
    repo := "github.build.company.com/org/repo-name.git"
    url := fmt.Sprintf("https://%s:%s@%s", username, password, repo)
    options := git.CloneOptions{
        URL:      url,
        Progress: os.Stdout,
    }
    r, err := git.PlainClone("./src", false, &options)

    if err != nil {
        return err
    }
    fmt.Println(r.Log)
    return err
}

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