From 853fda13e987f7eb3f1239cf7f78f8fa30daa66b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Mon, 13 Jan 2020 20:31:31 +0100 Subject: [PATCH] Add more OAuth flow debugging to stderr with `DEBUG=oauth` --- auth/oauth.go | 15 ++++++++++++++- context/config_setup.go | 7 +++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/auth/oauth.go b/auth/oauth.go index 5a09b9c1c..b2b199b48 100644 --- a/auth/oauth.go +++ b/auth/oauth.go @@ -30,6 +30,7 @@ type OAuthFlow struct { ClientID string ClientSecret string WriteSuccessHTML func(io.Writer) + VerboseStream io.Writer } // ObtainAccessToken guides the user through the browser OAuth flow on GitHub @@ -52,12 +53,14 @@ func (oa *OAuthFlow) ObtainAccessToken() (accessToken string, err error) { q.Set("state", state) startURL := fmt.Sprintf("https://%s/login/oauth/authorize?%s", oa.Hostname, q.Encode()) + oa.logf("open %s\n", startURL) if err := openInBrowser(startURL); err != nil { fmt.Fprintf(os.Stderr, "error opening web browser: %s\n", err) fmt.Fprintf(os.Stderr, "Please open the following URL manually:\n%s\n", startURL) } http.Serve(listener, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + oa.logf("server handler: %s\n", r.URL.Path) if r.URL.Path != "/callback" { w.WriteHeader(404) return @@ -69,6 +72,7 @@ func (oa *OAuthFlow) ObtainAccessToken() (accessToken string, err error) { return } code = rq.Get("code") + oa.logf("server received code %q\n", code) w.Header().Add("content-type", "text/html") if oa.WriteSuccessHTML != nil { oa.WriteSuccessHTML(w) @@ -77,7 +81,9 @@ func (oa *OAuthFlow) ObtainAccessToken() (accessToken string, err error) { } })) - resp, err := http.PostForm(fmt.Sprintf("https://%s/login/oauth/access_token", oa.Hostname), + tokenURL := fmt.Sprintf("https://%s/login/oauth/access_token", oa.Hostname) + oa.logf("POST %s\n", tokenURL) + resp, err := http.PostForm(tokenURL, url.Values{ "client_id": {oa.ClientID}, "client_secret": {oa.ClientSecret}, @@ -109,6 +115,13 @@ func (oa *OAuthFlow) ObtainAccessToken() (accessToken string, err error) { return } +func (oa *OAuthFlow) logf(format string, args ...interface{}) { + if oa.VerboseStream == nil { + return + } + fmt.Fprintf(oa.VerboseStream, format, args...) +} + func openInBrowser(url string) error { var args []string switch runtime.GOOS { diff --git a/context/config_setup.go b/context/config_setup.go index 48a037a24..d65ffb888 100644 --- a/context/config_setup.go +++ b/context/config_setup.go @@ -6,6 +6,7 @@ import ( "io" "os" "path/filepath" + "strings" "github.com/github/gh-cli/api" "github.com/github/gh-cli/auth" @@ -26,6 +27,11 @@ var ( // TODO: have a conversation about whether this belongs in the "context" package // FIXME: make testable func setupConfigFile(filename string) (*configEntry, error) { + var verboseStream io.Writer + if strings.Contains(os.Getenv("DEBUG"), "oauth") { + verboseStream = os.Stderr + } + flow := &auth.OAuthFlow{ Hostname: oauthHost, ClientID: oauthClientID, @@ -33,6 +39,7 @@ func setupConfigFile(filename string) (*configEntry, error) { WriteSuccessHTML: func(w io.Writer) { fmt.Fprintln(w, oauthSuccessPage) }, + VerboseStream: verboseStream, } fmt.Fprintln(os.Stderr, "Notice: authentication required")