MCPcopy Create free account
hub / github.com/cli/cli / AuthFlow

Function AuthFlow

internal/authflow/flow.go:42–136  ·  view source on GitHub ↗

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 on supported hosts; the server may st

(httpClient *http.Client, oauthHost string, IO *iostreams.IOStreams, notice string, additionalScopes []string, isInteractive bool, b browser.Browser, isCopyToClipboard bool, requestRefreshToken bool)

Source from the content-addressed store, hash-verified

40// for an expiring, refreshable credential on supported hosts; the server may
41// still issue a non-expiring token, in which case AuthResult.Refreshable is nil.
42func AuthFlow(httpClient *http.Client, oauthHost string, IO *iostreams.IOStreams, notice string, additionalScopes []string, isInteractive bool, b browser.Browser, isCopyToClipboard bool, requestRefreshToken bool) (*AuthResult, error) {
43 w := IO.ErrOut
44 cs := IO.ColorScheme()
45
46 minimumScopes := []string{"repo", "read:org", "gist"}
47 scopes := append(minimumScopes, additionalScopes...)
48
49 host, err := oauth.NewGitHubHost(ghinstance.HostPrefix(oauthHost))
50 if err != nil {
51 return nil, err
52 }
53
54 // TODO expiringTokensCleanup
55 if requestRefreshToken {
56 features, err := featuredetection.NewDetector(httpClient, oauthHost).AuthFeatures()
57 if err != nil {
58 return nil, err
59 }
60 requestRefreshToken = features.ExpiringTokens
61 if !requestRefreshToken {
62 fmt.Fprintf(w, "%s Expiring tokens require GitHub Enterprise Server 3.23 or later. Requesting a non-expiring token instead.\n", cs.WarningIcon())
63 }
64 }
65
66 flow := &oauth.Flow{
67 Host: host,
68 ClientID: oauthClientID,
69 ClientSecret: oauthClientSecret,
70 CallbackURI: getCallbackURI(oauthHost),
71 Scopes: scopes,
72 RequestRefreshToken: requestRefreshToken,
73 DisplayCode: func(code, verificationURL string) error {
74 if isCopyToClipboard {
75 err := clipboard.WriteAll(code)
76 if err == nil {
77 fmt.Fprintf(w, "%s One-time code (%s) copied to clipboard\n", cs.Yellow("!"), cs.Bold(code))
78 return nil
79 }
80 fmt.Fprintf(w, "%s Failed to copy one-time code to clipboard\n", cs.Red("!"))
81 fmt.Fprintf(w, " %s\n", err)
82 }
83 fmt.Fprintf(w, "%s First copy your one-time code: %s\n", cs.Yellow("!"), cs.Bold(code))
84 return nil
85 },
86 BrowseURL: func(authURL string) error {
87 if u, err := url.Parse(authURL); err == nil {
88 if u.Scheme != "http" && u.Scheme != "https" {
89 return fmt.Errorf("invalid URL: %s", authURL)
90 }
91 } else {
92 return err
93 }
94
95 if !isInteractive {
96 fmt.Fprintf(w, "%s to continue in your web browser: %s\n", cs.Bold("Open this URL"), authURL)
97 return nil
98 }
99

Callers 1

NewCmdRefreshFunction · 0.92

Calls 14

HostPrefixFunction · 0.92
NewDetectorFunction · 0.92
getCallbackURIFunction · 0.85
getViewerFunction · 0.85
authResultFromTokenFunction · 0.85
ColorSchemeMethod · 0.80
WarningIconMethod · 0.80
YellowMethod · 0.80
BoldMethod · 0.80
RedMethod · 0.80
waitForEnterFunction · 0.70
AuthFeaturesMethod · 0.65

Tested by

no test coverage detected