Skip to content

Commit

Permalink
Fix authenticating git operations after auth login --with-token
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mislav committed Feb 8, 2022
1 parent c3d451b commit 6701b52
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
3 changes: 3 additions & 0 deletions pkg/cmd/auth/gitcredential/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ func helperRun(opts *CredentialOptions) error {
gotUser = tokenUser
} else {
gotUser, _, _ = cfg.GetWithSource(lookupHost, "user")
if gotUser == "" {
gotUser = tokenUser
}
}

if gotUser == "" || gotToken == "" {
Expand Down
29 changes: 24 additions & 5 deletions pkg/cmd/auth/gitcredential/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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{
Expand Down

0 comments on commit 6701b52

Please sign in to comment.