From 9a3b54b9a3658b371ebea6c4ec8588703e08c4e2 Mon Sep 17 00:00:00 2001 From: Norwin Date: Mon, 7 Dec 2020 22:14:56 +0800 Subject: [PATCH] use token auth for https remotes (#275) use token for https auth instead of user name also handle http urls Co-authored-by: Norwin Roosen Reviewed-on: https://gitea.com/gitea/tea/pulls/275 Reviewed-by: Lunny Xiao Reviewed-by: 6543 <6543@obermui.de> Co-Authored-By: Norwin Co-Committed-By: Norwin --- modules/git/auth.go | 36 +++++------------------------------ modules/task/pull_checkout.go | 2 +- modules/task/pull_clean.go | 2 +- 3 files changed, 7 insertions(+), 33 deletions(-) diff --git a/modules/git/auth.go b/modules/git/auth.go index 0221ba0..1dbc6a5 100644 --- a/modules/git/auth.go +++ b/modules/git/auth.go @@ -5,12 +5,9 @@ package git import ( - "bufio" "fmt" "io/ioutil" "net/url" - "os" - "strings" "code.gitea.io/tea/modules/utils" @@ -24,31 +21,15 @@ import ( // 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() - +func GetAuthForURL(remoteURL *url.URL, authToken, keyFile string) (auth git_transport.AuthMethod, err error) { 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 "http", "https": + // gitea supports push/pull via app token as username. + auth = &gogit_http.BasicAuth{Password: "", Username: authToken} case "ssh": // try to select right key via ssh-agent. if it fails, try to read a key manually + user := remoteURL.User.Username() auth, err = gogit_ssh.DefaultAuthBuilder(user) if err != nil { signer, err := readSSHPrivKey(keyFile) @@ -92,13 +73,6 @@ func readSSHPrivKey(keyFile string) (sig ssh.Signer, err error) { 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) diff --git a/modules/task/pull_checkout.go b/modules/task/pull_checkout.go index eb16f3d..eba917b 100644 --- a/modules/task/pull_checkout.go +++ b/modules/task/pull_checkout.go @@ -60,7 +60,7 @@ func PullCheckout(login *config.Login, repoOwner, repoName string, index int64) if err != nil { return err } - auth, err := local_git.GetAuthForURL(url, login.User, login.SSHKey) + auth, err := local_git.GetAuthForURL(url, login.Token, login.SSHKey) if err != nil { return err } diff --git a/modules/task/pull_clean.go b/modules/task/pull_clean.go index 28094df..8fa654c 100644 --- a/modules/task/pull_clean.go +++ b/modules/task/pull_clean.go @@ -78,7 +78,7 @@ call me again with the --ignore-sha flag`, pr.Head.Ref) if err != nil { return err } - auth, err := local_git.GetAuthForURL(url, login.User, login.SSHKey) + auth, err := local_git.GetAuthForURL(url, login.Token, login.SSHKey) if err != nil { return err }