Skip to content

Commit 165a129

Browse files
committed
Refactor func
1 parent eac997c commit 165a129

File tree

3 files changed

+72
-67
lines changed

3 files changed

+72
-67
lines changed

oauth.go

Lines changed: 66 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -3,91 +3,97 @@
33
package oauth
44

55
import (
6-
"errors"
7-
"fmt"
8-
"io"
9-
"net/http"
10-
"net/url"
11-
"strings"
6+
"errors"
7+
"fmt"
8+
"io"
9+
"net/http"
10+
"net/url"
11+
"strings"
1212

13-
"github.com/cli/oauth/api"
14-
"github.com/cli/oauth/device"
13+
"github.com/cli/oauth/api"
14+
"github.com/cli/oauth/device"
1515
)
1616

1717
type httpClient interface {
18-
PostForm(string, url.Values) (*http.Response, error)
18+
PostForm(string, url.Values) (*http.Response, error)
1919
}
2020

2121
// Host defines the endpoints used to authorize against an OAuth server.
2222
type Host struct {
23-
DeviceCodeURL string
24-
AuthorizeURL string
25-
TokenURL string
23+
DeviceCodeURL string
24+
AuthorizeURL string
25+
TokenURL string
2626
}
2727

2828
func NewGitHubHost(hostURL string) (*Host, error) {
29-
u, err := url.Parse(strings.TrimSpace(hostURL))
30-
if err != nil {
31-
return nil, err
32-
}
29+
base, err := url.Parse(strings.TrimSpace(hostURL))
30+
if err != nil {
31+
return nil, err
32+
}
3333

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
34+
createURL := func(path string) string {
35+
u := *base // Copy base URL
36+
u.Path = path
37+
return u.String()
38+
}
39+
40+
return &Host{
41+
DeviceCodeURL: createURL("/login/device/code"),
42+
AuthorizeURL: createURL("/login/oauth/authorize"),
43+
TokenURL: createURL("/login/oauth/access_token"),
44+
}, nil
3945
}
4046

4147
// GitHubHost constructs a Host from the given URL to a GitHub instance.
4248
func GitHubHost(hostURL string) *Host {
43-
u, _ := url.Parse(hostURL)
49+
u, _ := url.Parse(hostURL)
4450

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-
}
51+
return &Host{
52+
DeviceCodeURL: fmt.Sprintf("%s://%s/login/device/code", u.Scheme, u.Host),
53+
AuthorizeURL: fmt.Sprintf("%s://%s/login/oauth/authorize", u.Scheme, u.Host),
54+
TokenURL: fmt.Sprintf("%s://%s/login/oauth/access_token", u.Scheme, u.Host),
55+
}
5056
}
5157

5258
// Flow facilitates a single OAuth authorization flow.
5359
type Flow struct {
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
60+
// The hostname to authorize the app with.
61+
//
62+
// Deprecated: Use Host instead.
63+
Hostname string
64+
// Host configuration to authorize the app with.
65+
Host *Host
66+
// OAuth scopes to request from the user.
67+
Scopes []string
68+
// OAuth application ID.
69+
ClientID string
70+
// OAuth application secret. Only applicable in web application flow.
71+
ClientSecret string
72+
// The localhost URI for web application flow callback, e.g. "http://127.0.0.1/callback".
73+
CallbackURI string
6874

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)
75+
// Display a one-time code to the user. Receives the code and the browser URL as arguments. Defaults to printing the
76+
// code to the user on Stdout with instructions to copy the code and to press Enter to continue in their browser.
77+
DisplayCode func(string, string) error
78+
// Open a web browser at a URL. Defaults to opening the default system browser.
79+
BrowseURL func(string) error
80+
// Render an HTML page to the user upon completion of web application flow. The default is to
81+
// render a simple message that informs the user they can close the browser tab and return to the app.
82+
WriteSuccessHTML func(io.Writer)
7783

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
84+
// The HTTP client to use for API POST requests. Defaults to http.DefaultClient.
85+
HTTPClient httpClient
86+
// The stream to listen to keyboard input on. Defaults to os.Stdin.
87+
Stdin io.Reader
88+
// The stream to print UI messages to. Defaults to os.Stdout.
89+
Stdout io.Writer
8490
}
8591

8692
// DetectFlow tries to perform Device flow first and falls back to Web application flow.
8793
func (oa *Flow) DetectFlow() (*api.AccessToken, error) {
88-
accessToken, err := oa.DeviceFlow()
89-
if errors.Is(err, device.ErrUnsupported) {
90-
return oa.WebAppFlow()
91-
}
92-
return accessToken, err
94+
accessToken, err := oa.DeviceFlow()
95+
if errors.Is(err, device.ErrUnsupported) {
96+
return oa.WebAppFlow()
97+
}
98+
return accessToken, err
9399
}

oauth_device.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,11 @@ func (oa *Flow) DeviceFlow() (*api.AccessToken, error) {
3232

3333
host := oa.Host
3434
if host == nil {
35-
_, err := NewGitHubHost("https://" + oa.Hostname)
35+
host, err := NewGitHubHost("https://" + oa.Hostname)
3636
if err != nil {
37-
return nil, fmt.Errorf("error parsing the hostname %w", err)
37+
return nil, fmt.Errorf("error parsing the hostname '%s': %w", host, err)
3838
}
39-
40-
//host = GitHubHost("https://" + oa.Hostname)
39+
oa.Host = host
4140
}
4241

4342
code, err := device.RequestCode(httpClient, host.DeviceCodeURL, oa.ClientID, oa.Scopes)

oauth_webapp.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ func (oa *Flow) WebAppFlow() (*api.AccessToken, error) {
1616
host := oa.Host
1717

1818
if host == nil {
19-
_, err := NewGitHubHost("https://" + oa.Hostname)
19+
host, err := NewGitHubHost("https://" + oa.Hostname)
2020
if err != nil {
21-
return nil, fmt.Errorf("error parsing the hostname %w", err)
21+
return nil, fmt.Errorf("error parsing the hostname '%s': %w", host, err)
2222
}
23-
//host = GitHubHost("https://" + oa.Hostname)
23+
oa.Host = host
2424
}
2525

2626
flow, err := webapp.InitFlow()

0 commit comments

Comments
 (0)