From 6701b526d1c1ad9cda748a3f05ad80d8210de6e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Wed, 9 Feb 2022 00:10:10 +0100 Subject: [PATCH] Fix authenticating git operations after `auth login --with-token` After completing the interactive `gh auth login` flow, the `hosts.yml` config file will have been populated with both `oauth_token` and `user` properties for the GitHub host. However, after `auth login --with-token` only the `oauth_token` is persisted but no username. This fixes `auth git-credential` behavior so it allows authentication even if the `user` property is missing. It's entirely optional to send a proper username for git authentication, since GitHub seems to ignore the actual value sent and just focuses on the token itself. --- pkg/cmd/auth/gitcredential/helper.go | 3 +++ pkg/cmd/auth/gitcredential/helper_test.go | 29 +++++++++++++++++++---- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/pkg/cmd/auth/gitcredential/helper.go b/pkg/cmd/auth/gitcredential/helper.go index 117584b34..bda962e50 100644 --- a/pkg/cmd/auth/gitcredential/helper.go +++ b/pkg/cmd/auth/gitcredential/helper.go @@ -112,6 +112,9 @@ func helperRun(opts *CredentialOptions) error { gotUser = tokenUser } else { gotUser, _, _ = cfg.GetWithSource(lookupHost, "user") + if gotUser == "" { + gotUser = tokenUser + } } if gotUser == "" || gotToken == "" { diff --git a/pkg/cmd/auth/gitcredential/helper_test.go b/pkg/cmd/auth/gitcredential/helper_test.go index 92cdcb692..45488682d 100644 --- a/pkg/cmd/auth/gitcredential/helper_test.go +++ b/pkg/cmd/auth/gitcredential/helper_test.go @@ -15,11 +15,6 @@ func (c tinyConfig) GetWithSource(host, key string) (string, string, error) { return c[fmt.Sprintf("%s:%s", host, key)], c["_source"], nil } -func (c tinyConfig) Get(host, key string) (val string, err error) { - val, _, err = c.GetWithSource(host, key) - return -} - func Test_helperRun(t *testing.T) { tests := []struct { name string @@ -170,6 +165,30 @@ func Test_helperRun(t *testing.T) { wantStdout: "", wantStderr: "", }, + { + name: "no username configured", + opts: CredentialOptions{ + Operation: "get", + Config: func() (config, error) { + return tinyConfig{ + "_source": "/Users/monalisa/.config/gh/hosts.yml", + "example.com:oauth_token": "OTOKEN", + }, nil + }, + }, + input: heredoc.Doc(` + protocol=https + host=example.com + `), + wantErr: false, + wantStdout: heredoc.Doc(` + protocol=https + host=example.com + username=x-access-token + password=OTOKEN + `), + wantStderr: "", + }, { name: "token from env", opts: CredentialOptions{