|
3 | 3 | package oauth
|
4 | 4 |
|
5 | 5 | import (
|
6 |
| - "errors" |
7 |
| - "fmt" |
8 |
| - "io" |
9 |
| - "net/http" |
10 |
| - "net/url" |
| 6 | + "errors" |
| 7 | + "fmt" |
| 8 | + "io" |
| 9 | + "net/http" |
| 10 | + "net/url" |
| 11 | + "strings" |
11 | 12 |
|
12 |
| - "github.com/cli/oauth/api" |
13 |
| - "github.com/cli/oauth/device" |
| 13 | + "github.com/cli/oauth/api" |
| 14 | + "github.com/cli/oauth/device" |
14 | 15 | )
|
15 | 16 |
|
16 | 17 | type httpClient interface {
|
17 |
| - PostForm(string, url.Values) (*http.Response, error) |
| 18 | + PostForm(string, url.Values) (*http.Response, error) |
18 | 19 | }
|
19 | 20 |
|
20 | 21 | // Host defines the endpoints used to authorize against an OAuth server.
|
21 | 22 | type Host struct {
|
22 |
| - DeviceCodeURL string |
23 |
| - AuthorizeURL string |
24 |
| - TokenURL string |
| 23 | + DeviceCodeURL string |
| 24 | + AuthorizeURL string |
| 25 | + TokenURL string |
| 26 | +} |
| 27 | + |
| 28 | +func NewGitHubHost(hostURL string) (*Host, error) { |
| 29 | + u, err := url.Parse(strings.TrimSpace(hostURL)) |
| 30 | + if err != nil { |
| 31 | + return nil, err |
| 32 | + } |
| 33 | + |
| 34 | + return &Host{ |
| 35 | + DeviceCodeURL: fmt.Sprintf("%s://%s/login/device/code", u.Scheme, u.Host), |
| 36 | + AuthorizeURL: fmt.Sprintf("%s://%s/login/oauth/authorize", u.Scheme, u.Host), |
| 37 | + TokenURL: fmt.Sprintf("%s://%s/login/oauth/access_token", u.Scheme, u.Host), |
| 38 | + }, nil |
25 | 39 | }
|
26 | 40 |
|
27 | 41 | // GitHubHost constructs a Host from the given URL to a GitHub instance.
|
28 | 42 | func GitHubHost(hostURL string) *Host {
|
29 |
| - u, _ := url.Parse(hostURL) |
| 43 | + u, _ := url.Parse(hostURL) |
30 | 44 |
|
31 |
| - return &Host{ |
32 |
| - DeviceCodeURL: fmt.Sprintf("%s://%s/login/device/code", u.Scheme, u.Host), |
33 |
| - AuthorizeURL: fmt.Sprintf("%s://%s/login/oauth/authorize", u.Scheme, u.Host), |
34 |
| - TokenURL: fmt.Sprintf("%s://%s/login/oauth/access_token", u.Scheme, u.Host), |
35 |
| - } |
| 45 | + return &Host{ |
| 46 | + DeviceCodeURL: fmt.Sprintf("%s://%s/login/device/code", u.Scheme, u.Host), |
| 47 | + AuthorizeURL: fmt.Sprintf("%s://%s/login/oauth/authorize", u.Scheme, u.Host), |
| 48 | + TokenURL: fmt.Sprintf("%s://%s/login/oauth/access_token", u.Scheme, u.Host), |
| 49 | + } |
36 | 50 | }
|
37 | 51 |
|
38 | 52 | // Flow facilitates a single OAuth authorization flow.
|
39 | 53 | type Flow struct {
|
40 |
| - // The hostname to authorize the app with. |
41 |
| - // |
42 |
| - // Deprecated: Use Host instead. |
43 |
| - Hostname string |
44 |
| - // Host configuration to authorize the app with. |
45 |
| - Host *Host |
46 |
| - // OAuth scopes to request from the user. |
47 |
| - Scopes []string |
48 |
| - // OAuth application ID. |
49 |
| - ClientID string |
50 |
| - // OAuth application secret. Only applicable in web application flow. |
51 |
| - ClientSecret string |
52 |
| - // The localhost URI for web application flow callback, e.g. "http://127.0.0.1/callback". |
53 |
| - CallbackURI string |
| 54 | + // The hostname to authorize the app with. |
| 55 | + // |
| 56 | + // Deprecated: Use Host instead. |
| 57 | + Hostname string |
| 58 | + // Host configuration to authorize the app with. |
| 59 | + Host *Host |
| 60 | + // OAuth scopes to request from the user. |
| 61 | + Scopes []string |
| 62 | + // OAuth application ID. |
| 63 | + ClientID string |
| 64 | + // OAuth application secret. Only applicable in web application flow. |
| 65 | + ClientSecret string |
| 66 | + // The localhost URI for web application flow callback, e.g. "http://127.0.0.1/callback". |
| 67 | + CallbackURI string |
54 | 68 |
|
55 |
| - // Display a one-time code to the user. Receives the code and the browser URL as arguments. Defaults to printing the |
56 |
| - // code to the user on Stdout with instructions to copy the code and to press Enter to continue in their browser. |
57 |
| - DisplayCode func(string, string) error |
58 |
| - // Open a web browser at a URL. Defaults to opening the default system browser. |
59 |
| - BrowseURL func(string) error |
60 |
| - // Render an HTML page to the user upon completion of web application flow. The default is to |
61 |
| - // render a simple message that informs the user they can close the browser tab and return to the app. |
62 |
| - WriteSuccessHTML func(io.Writer) |
| 69 | + // Display a one-time code to the user. Receives the code and the browser URL as arguments. Defaults to printing the |
| 70 | + // code to the user on Stdout with instructions to copy the code and to press Enter to continue in their browser. |
| 71 | + DisplayCode func(string, string) error |
| 72 | + // Open a web browser at a URL. Defaults to opening the default system browser. |
| 73 | + BrowseURL func(string) error |
| 74 | + // Render an HTML page to the user upon completion of web application flow. The default is to |
| 75 | + // render a simple message that informs the user they can close the browser tab and return to the app. |
| 76 | + WriteSuccessHTML func(io.Writer) |
63 | 77 |
|
64 |
| - // The HTTP client to use for API POST requests. Defaults to http.DefaultClient. |
65 |
| - HTTPClient httpClient |
66 |
| - // The stream to listen to keyboard input on. Defaults to os.Stdin. |
67 |
| - Stdin io.Reader |
68 |
| - // The stream to print UI messages to. Defaults to os.Stdout. |
69 |
| - Stdout io.Writer |
| 78 | + // The HTTP client to use for API POST requests. Defaults to http.DefaultClient. |
| 79 | + HTTPClient httpClient |
| 80 | + // The stream to listen to keyboard input on. Defaults to os.Stdin. |
| 81 | + Stdin io.Reader |
| 82 | + // The stream to print UI messages to. Defaults to os.Stdout. |
| 83 | + Stdout io.Writer |
70 | 84 | }
|
71 | 85 |
|
72 | 86 | // DetectFlow tries to perform Device flow first and falls back to Web application flow.
|
73 | 87 | func (oa *Flow) DetectFlow() (*api.AccessToken, error) {
|
74 |
| - accessToken, err := oa.DeviceFlow() |
75 |
| - if errors.Is(err, device.ErrUnsupported) { |
76 |
| - return oa.WebAppFlow() |
77 |
| - } |
78 |
| - return accessToken, err |
| 88 | + accessToken, err := oa.DeviceFlow() |
| 89 | + if errors.Is(err, device.ErrUnsupported) { |
| 90 | + return oa.WebAppFlow() |
| 91 | + } |
| 92 | + return accessToken, err |
79 | 93 | }
|
0 commit comments