2020-04-18 23:09:03 -04:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package git
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/url"
|
|
|
|
|
2020-09-30 01:11:33 -04:00
|
|
|
"code.gitea.io/tea/modules/utils"
|
|
|
|
|
2020-04-29 22:02:15 -04:00
|
|
|
git_transport "github.com/go-git/go-git/v5/plumbing/transport"
|
|
|
|
gogit_http "github.com/go-git/go-git/v5/plumbing/transport/http"
|
|
|
|
gogit_ssh "github.com/go-git/go-git/v5/plumbing/transport/ssh"
|
2020-04-18 23:09:03 -04:00
|
|
|
"golang.org/x/crypto/ssh"
|
|
|
|
"golang.org/x/crypto/ssh/terminal"
|
|
|
|
)
|
|
|
|
|
|
|
|
// GetAuthForURL returns the appropriate AuthMethod to be used in Push() / Pull()
|
|
|
|
// operations depending on the protocol, and prompts the user for credentials if
|
|
|
|
// necessary.
|
2020-12-07 09:14:56 -05:00
|
|
|
func GetAuthForURL(remoteURL *url.URL, authToken, keyFile string) (auth git_transport.AuthMethod, err error) {
|
2020-04-18 23:09:03 -04:00
|
|
|
switch remoteURL.Scheme {
|
2020-12-07 09:14:56 -05:00
|
|
|
case "http", "https":
|
|
|
|
// gitea supports push/pull via app token as username.
|
|
|
|
auth = &gogit_http.BasicAuth{Password: "", Username: authToken}
|
2020-04-18 23:09:03 -04:00
|
|
|
|
|
|
|
case "ssh":
|
|
|
|
// try to select right key via ssh-agent. if it fails, try to read a key manually
|
2020-12-07 09:14:56 -05:00
|
|
|
user := remoteURL.User.Username()
|
2020-04-18 23:09:03 -04:00
|
|
|
auth, err = gogit_ssh.DefaultAuthBuilder(user)
|
|
|
|
if err != nil {
|
|
|
|
signer, err := readSSHPrivKey(keyFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
auth = &gogit_ssh.PublicKeys{User: user, Signer: signer}
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("don't know how to handle url scheme %v", remoteURL.Scheme)
|
|
|
|
}
|
|
|
|
|
|
|
|
return auth, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func readSSHPrivKey(keyFile string) (sig ssh.Signer, err error) {
|
|
|
|
if keyFile != "" {
|
2020-09-30 01:11:33 -04:00
|
|
|
keyFile, err = utils.AbsPathWithExpansion(keyFile)
|
2020-04-18 23:09:03 -04:00
|
|
|
} else {
|
2020-09-30 01:11:33 -04:00
|
|
|
keyFile, err = utils.AbsPathWithExpansion("~/.ssh/id_rsa")
|
2020-04-18 23:09:03 -04:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
sshKey, err := ioutil.ReadFile(keyFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
sig, err = ssh.ParsePrivateKey(sshKey)
|
|
|
|
if err != nil {
|
|
|
|
pass, err := promptPass(keyFile)
|
2020-04-24 22:35:22 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-04-18 23:09:03 -04:00
|
|
|
sig, err = ssh.ParsePrivateKeyWithPassphrase(sshKey, []byte(pass))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return sig, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func promptPass(domain string) (string, error) {
|
|
|
|
fmt.Printf("%s password: ", domain)
|
|
|
|
pass, err := terminal.ReadPassword(0)
|
|
|
|
return string(pass), err
|
|
|
|
}
|