Skip to content

Commit 0e06fde

Browse files
mislavnate smith
authored andcommitted
Allow gh auth git-credential to authenticate GitHub Gist requests
When there are stored credentials for `example.com`, allow using them to authenticate requests to `gist.example.com` as well.
1 parent eaa64df commit 0e06fde

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

pkg/cmd/auth/gitcredential/helper.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,18 @@ func helperRun(opts *CredentialOptions) error {
100100
return err
101101
}
102102

103+
lookupHost := wants["host"]
103104
var gotUser string
104-
gotToken, source, _ := cfg.GetWithSource(wants["host"], "oauth_token")
105+
gotToken, source, _ := cfg.GetWithSource(lookupHost, "oauth_token")
106+
if gotToken == "" && strings.HasPrefix(lookupHost, "gist.") {
107+
lookupHost = strings.TrimPrefix(lookupHost, "gist.")
108+
gotToken, source, _ = cfg.GetWithSource(lookupHost, "oauth_token")
109+
}
110+
105111
if strings.HasSuffix(source, "_TOKEN") {
106112
gotUser = tokenUser
107113
} else {
108-
gotUser, _, _ = cfg.GetWithSource(wants["host"], "user")
114+
gotUser, _, _ = cfg.GetWithSource(lookupHost, "user")
109115
}
110116

111117
if gotUser == "" || gotToken == "" {

pkg/cmd/auth/gitcredential/helper_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,32 @@ func Test_helperRun(t *testing.T) {
7474
`),
7575
wantStderr: "",
7676
},
77+
{
78+
name: "gist host",
79+
opts: CredentialOptions{
80+
Operation: "get",
81+
Config: func() (config, error) {
82+
return tinyConfig{
83+
"_source": "/Users/monalisa/.config/gh/hosts.yml",
84+
"github.com:user": "monalisa",
85+
"github.com:oauth_token": "OTOKEN",
86+
}, nil
87+
},
88+
},
89+
input: heredoc.Doc(`
90+
protocol=https
91+
host=gist.github.com
92+
username=monalisa
93+
`),
94+
wantErr: false,
95+
wantStdout: heredoc.Doc(`
96+
protocol=https
97+
host=gist.github.com
98+
username=monalisa
99+
password=OTOKEN
100+
`),
101+
wantStderr: "",
102+
},
77103
{
78104
name: "url input",
79105
opts: CredentialOptions{

0 commit comments

Comments
 (0)