mirror of
https://gitea.com/gitea/tea.git
synced 2024-12-04 14:46:40 -05:00
f445ac7521
fix lint fix lint Move print TrackedTimesList to print package Move AbsPathWithExpansion to utils/path.go rename module intern to config Move Subcomands into it's own Packages Split times subcomands into own sourcefiles Split repos subcomands into own sourcefiles Split releases subcomands into own sourcefiles Split pulls subcomands into own sourcefiles Split milestones subcomands into own sourcefiles Split login subcomands into own sourcefiles Split labels subcomands into own sourcefiles split issues subcomands into own sourcefiles mv Move Interactive Login Creation to interact package Move Add Login function to intern/login.go apply from review lint: add description to exported func smal nits Move DetailViews stdout print func to print package Refactor: * Move Config & Login routines into intern package * rename global var in cmd * Move help func to utils Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://gitea.com/gitea/tea/pulls/206 Reviewed-by: Norwin <noerw@noreply.gitea.io> Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com>
107 lines
2.6 KiB
Go
107 lines
2.6 KiB
Go
// 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 (
|
|
"bufio"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"net/url"
|
|
"os"
|
|
"strings"
|
|
|
|
"code.gitea.io/tea/modules/utils"
|
|
|
|
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"
|
|
"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.
|
|
func GetAuthForURL(remoteURL *url.URL, httpUser, keyFile string) (auth git_transport.AuthMethod, err error) {
|
|
user := remoteURL.User.Username()
|
|
|
|
switch remoteURL.Scheme {
|
|
case "https":
|
|
if httpUser != "" {
|
|
user = httpUser
|
|
}
|
|
if user == "" {
|
|
user, err = promptUser(remoteURL.Host)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
pass, isSet := remoteURL.User.Password()
|
|
if !isSet {
|
|
pass, err = promptPass(remoteURL.Host)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
auth = &gogit_http.BasicAuth{Password: pass, Username: user}
|
|
|
|
case "ssh":
|
|
// try to select right key via ssh-agent. if it fails, try to read a key manually
|
|
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 != "" {
|
|
keyFile, err = utils.AbsPathWithExpansion(keyFile)
|
|
} else {
|
|
keyFile, err = utils.AbsPathWithExpansion("~/.ssh/id_rsa")
|
|
}
|
|
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)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
sig, err = ssh.ParsePrivateKeyWithPassphrase(sshKey, []byte(pass))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
return sig, err
|
|
}
|
|
|
|
func promptUser(domain string) (string, error) {
|
|
reader := bufio.NewReader(os.Stdin)
|
|
fmt.Printf("%s username: ", domain)
|
|
username, err := reader.ReadString('\n')
|
|
return strings.TrimSpace(username), err
|
|
}
|
|
|
|
func promptPass(domain string) (string, error) {
|
|
fmt.Printf("%s password: ", domain)
|
|
pass, err := terminal.ReadPassword(0)
|
|
return string(pass), err
|
|
}
|