Add more OAuth flow debugging to stderr with DEBUG=oauth
This commit is contained in:
parent
d6a437a93c
commit
853fda13e9
2 changed files with 21 additions and 1 deletions
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue