Skip to content

Commit 853fda1

Browse files
committed
Add more OAuth flow debugging to stderr with DEBUG=oauth
1 parent d6a437a commit 853fda1

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

auth/oauth.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ type OAuthFlow struct {
3030
ClientID string
3131
ClientSecret string
3232
WriteSuccessHTML func(io.Writer)
33+
VerboseStream io.Writer
3334
}
3435

3536
// ObtainAccessToken guides the user through the browser OAuth flow on GitHub
@@ -52,12 +53,14 @@ func (oa *OAuthFlow) ObtainAccessToken() (accessToken string, err error) {
5253
q.Set("state", state)
5354

5455
startURL := fmt.Sprintf("https://%s/login/oauth/authorize?%s", oa.Hostname, q.Encode())
56+
oa.logf("open %s\n", startURL)
5557
if err := openInBrowser(startURL); err != nil {
5658
fmt.Fprintf(os.Stderr, "error opening web browser: %s\n", err)
5759
fmt.Fprintf(os.Stderr, "Please open the following URL manually:\n%s\n", startURL)
5860
}
5961

6062
http.Serve(listener, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
63+
oa.logf("server handler: %s\n", r.URL.Path)
6164
if r.URL.Path != "/callback" {
6265
w.WriteHeader(404)
6366
return
@@ -69,6 +72,7 @@ func (oa *OAuthFlow) ObtainAccessToken() (accessToken string, err error) {
6972
return
7073
}
7174
code = rq.Get("code")
75+
oa.logf("server received code %q\n", code)
7276
w.Header().Add("content-type", "text/html")
7377
if oa.WriteSuccessHTML != nil {
7478
oa.WriteSuccessHTML(w)
@@ -77,7 +81,9 @@ func (oa *OAuthFlow) ObtainAccessToken() (accessToken string, err error) {
7781
}
7882
}))
7983

80-
resp, err := http.PostForm(fmt.Sprintf("https://%s/login/oauth/access_token", oa.Hostname),
84+
tokenURL := fmt.Sprintf("https://%s/login/oauth/access_token", oa.Hostname)
85+
oa.logf("POST %s\n", tokenURL)
86+
resp, err := http.PostForm(tokenURL,
8187
url.Values{
8288
"client_id": {oa.ClientID},
8389
"client_secret": {oa.ClientSecret},
@@ -109,6 +115,13 @@ func (oa *OAuthFlow) ObtainAccessToken() (accessToken string, err error) {
109115
return
110116
}
111117

118+
func (oa *OAuthFlow) logf(format string, args ...interface{}) {
119+
if oa.VerboseStream == nil {
120+
return
121+
}
122+
fmt.Fprintf(oa.VerboseStream, format, args...)
123+
}
124+
112125
func openInBrowser(url string) error {
113126
var args []string
114127
switch runtime.GOOS {

context/config_setup.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"io"
77
"os"
88
"path/filepath"
9+
"strings"
910

1011
"github.com/github/gh-cli/api"
1112
"github.com/github/gh-cli/auth"
@@ -26,13 +27,19 @@ var (
2627
// TODO: have a conversation about whether this belongs in the "context" package
2728
// FIXME: make testable
2829
func setupConfigFile(filename string) (*configEntry, error) {
30+
var verboseStream io.Writer
31+
if strings.Contains(os.Getenv("DEBUG"), "oauth") {
32+
verboseStream = os.Stderr
33+
}
34+
2935
flow := &auth.OAuthFlow{
3036
Hostname: oauthHost,
3137
ClientID: oauthClientID,
3238
ClientSecret: oauthClientSecret,
3339
WriteSuccessHTML: func(w io.Writer) {
3440
fmt.Fprintln(w, oauthSuccessPage)
3541
},
42+
VerboseStream: verboseStream,
3643
}
3744

3845
fmt.Fprintln(os.Stderr, "Notice: authentication required")

0 commit comments

Comments
 (0)