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.
(httpClient *http.Client, oauthHost string, IO *iostreams.IOStreams, notice string, additionalScopes []string, isInteractive bool, b browser.Browser, isCopyToClipboard bool)
| 28 | // token. The provided HTTP client should be a plain client that does not set |
| 29 | // auth or other headers. |
| 30 | func AuthFlow(httpClient *http.Client, oauthHost string, IO *iostreams.IOStreams, notice string, additionalScopes []string, isInteractive bool, b browser.Browser, isCopyToClipboard bool) (string, string, error) { |
| 31 | w := IO.ErrOut |
| 32 | cs := IO.ColorScheme() |
| 33 | |
| 34 | minimumScopes := []string{"repo", "read:org", "gist"} |
| 35 | scopes := append(minimumScopes, additionalScopes...) |
| 36 | |
| 37 | host, err := oauth.NewGitHubHost(ghinstance.HostPrefix(oauthHost)) |
| 38 | if err != nil { |
| 39 | return "", "", err |
| 40 | } |
| 41 | |
| 42 | flow := &oauth.Flow{ |
| 43 | Host: host, |
| 44 | ClientID: oauthClientID, |
| 45 | ClientSecret: oauthClientSecret, |
| 46 | CallbackURI: getCallbackURI(oauthHost), |
| 47 | Scopes: scopes, |
| 48 | DisplayCode: func(code, verificationURL string) error { |
| 49 | if isCopyToClipboard { |
| 50 | err := clipboard.WriteAll(code) |
| 51 | if err == nil { |
| 52 | fmt.Fprintf(w, "%s One-time code (%s) copied to clipboard\n", cs.Yellow("!"), cs.Bold(code)) |
| 53 | return nil |
| 54 | } |
| 55 | fmt.Fprintf(w, "%s Failed to copy one-time code to clipboard\n", cs.Red("!")) |
| 56 | fmt.Fprintf(w, " %s\n", err) |
| 57 | } |
| 58 | fmt.Fprintf(w, "%s First copy your one-time code: %s\n", cs.Yellow("!"), cs.Bold(code)) |
| 59 | return nil |
| 60 | }, |
| 61 | BrowseURL: func(authURL string) error { |
| 62 | if u, err := url.Parse(authURL); err == nil { |
| 63 | if u.Scheme != "http" && u.Scheme != "https" { |
| 64 | return fmt.Errorf("invalid URL: %s", authURL) |
| 65 | } |
| 66 | } else { |
| 67 | return err |
| 68 | } |
| 69 | |
| 70 | if !isInteractive { |
| 71 | fmt.Fprintf(w, "%s to continue in your web browser: %s\n", cs.Bold("Open this URL"), authURL) |
| 72 | return nil |
| 73 | } |
| 74 | |
| 75 | fmt.Fprintf(w, "%s to open %s in your browser... ", cs.Bold("Press Enter"), authURL) |
| 76 | _ = waitForEnter(IO.In) |
| 77 | |
| 78 | if err := b.Browse(authURL); err != nil { |
| 79 | fmt.Fprintf(w, "%s Failed opening a web browser at %s\n", cs.Red("!"), authURL) |
| 80 | fmt.Fprintf(w, " %s\n", err) |
| 81 | fmt.Fprint(w, " Please try entering the URL in your browser manually\n") |
| 82 | } |
| 83 | return nil |
| 84 | }, |
| 85 | WriteSuccessHTML: func(w io.Writer) { |
| 86 | fmt.Fprint(w, oauthSuccessPage) |
| 87 | }, |
no test coverage detected