AuthFlow initiates an OAuth device or web application flow to acquire a token. The provided HTTP client should be a plain client that does not set auth or other headers. When requestRefreshToken is true, gh asks the server for an expiring, refreshable credential; the server may still issue a non-exp
(httpClient *http.Client, oauthHost string, IO *iostreams.IOStreams, notice string, additionalScopes []string, isInteractive bool, b browser.Browser, isCopyToClipboard bool, requestRefreshToken bool)
| 39 | // for an expiring, refreshable credential; the server may still issue a |
| 40 | // non-expiring token, in which case AuthResult.Refreshable is nil. |
| 41 | func AuthFlow(httpClient *http.Client, oauthHost string, IO *iostreams.IOStreams, notice string, additionalScopes []string, isInteractive bool, b browser.Browser, isCopyToClipboard bool, requestRefreshToken bool) (*AuthResult, error) { |
| 42 | w := IO.ErrOut |
| 43 | cs := IO.ColorScheme() |
| 44 | |
| 45 | minimumScopes := []string{"repo", "read:org", "gist"} |
| 46 | scopes := append(minimumScopes, additionalScopes...) |
| 47 | |
| 48 | host, err := oauth.NewGitHubHost(ghinstance.HostPrefix(oauthHost)) |
| 49 | if err != nil { |
| 50 | return nil, err |
| 51 | } |
| 52 | |
| 53 | flow := &oauth.Flow{ |
| 54 | Host: host, |
| 55 | ClientID: oauthClientID, |
| 56 | ClientSecret: oauthClientSecret, |
| 57 | CallbackURI: getCallbackURI(oauthHost), |
| 58 | Scopes: scopes, |
| 59 | RequestRefreshToken: requestRefreshToken, |
| 60 | DisplayCode: func(code, verificationURL string) error { |
| 61 | if isCopyToClipboard { |
| 62 | err := clipboard.WriteAll(code) |
| 63 | if err == nil { |
| 64 | fmt.Fprintf(w, "%s One-time code (%s) copied to clipboard\n", cs.Yellow("!"), cs.Bold(code)) |
| 65 | return nil |
| 66 | } |
| 67 | fmt.Fprintf(w, "%s Failed to copy one-time code to clipboard\n", cs.Red("!")) |
| 68 | fmt.Fprintf(w, " %s\n", err) |
| 69 | } |
| 70 | fmt.Fprintf(w, "%s First copy your one-time code: %s\n", cs.Yellow("!"), cs.Bold(code)) |
| 71 | return nil |
| 72 | }, |
| 73 | BrowseURL: func(authURL string) error { |
| 74 | if u, err := url.Parse(authURL); err == nil { |
| 75 | if u.Scheme != "http" && u.Scheme != "https" { |
| 76 | return fmt.Errorf("invalid URL: %s", authURL) |
| 77 | } |
| 78 | } else { |
| 79 | return err |
| 80 | } |
| 81 | |
| 82 | if !isInteractive { |
| 83 | fmt.Fprintf(w, "%s to continue in your web browser: %s\n", cs.Bold("Open this URL"), authURL) |
| 84 | return nil |
| 85 | } |
| 86 | |
| 87 | fmt.Fprintf(w, "%s to open %s in your browser... ", cs.Bold("Press Enter"), authURL) |
| 88 | _ = waitForEnter(IO.In) |
| 89 | |
| 90 | if err := b.Browse(authURL); err != nil { |
| 91 | fmt.Fprintf(w, "%s Failed opening a web browser at %s\n", cs.Red("!"), authURL) |
| 92 | fmt.Fprintf(w, " %s\n", err) |
| 93 | fmt.Fprint(w, " Please try entering the URL in your browser manually\n") |
| 94 | } |
| 95 | return nil |
| 96 | }, |
| 97 | WriteSuccessHTML: func(w io.Writer) { |
| 98 | fmt.Fprint(w, oauthSuccessPage) |
no test coverage detected