Skip to content

Commit afffc8e

Browse files
authored
Merge pull request #63 from mouismail/mouismail-issue-61
create new func to avoid the bug on issue #61
2 parents adf5f73 + 9585a35 commit afffc8e

File tree

4 files changed

+40
-3
lines changed

4 files changed

+40
-3
lines changed

Diff for: examples_test.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,12 @@ import (
1212
// github.com, as its Device flow support is globally available, but it enables logging in to
1313
// self-hosted GitHub instances as well.
1414
func ExampleFlow_DetectFlow() {
15+
host, err := oauth.NewGitHubHost("https://github.com")
16+
if err != nil {
17+
panic(err)
18+
}
1519
flow := &oauth.Flow{
16-
Host: oauth.GitHubHost("https://github.com"),
20+
Host: host,
1721
ClientID: os.Getenv("OAUTH_CLIENT_ID"),
1822
ClientSecret: os.Getenv("OAUTH_CLIENT_SECRET"), // only applicable to web app flow
1923
CallbackURI: "http://127.0.0.1/callback", // only applicable to web app flow

Diff for: oauth.go

+23
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"io"
99
"net/http"
1010
"net/url"
11+
"strings"
1112

1213
"github.com/cli/oauth/api"
1314
"github.com/cli/oauth/device"
@@ -24,7 +25,29 @@ type Host struct {
2425
TokenURL string
2526
}
2627

28+
// NewGitHubHost constructs a Host from the given URL to a GitHub instance.
29+
func NewGitHubHost(hostURL string) (*Host, error) {
30+
base, err := url.Parse(strings.TrimSpace(hostURL))
31+
if err != nil {
32+
return nil, err
33+
}
34+
35+
createURL := func(path string) string {
36+
u := *base // Copy base URL
37+
u.Path = path
38+
return u.String()
39+
}
40+
41+
return &Host{
42+
DeviceCodeURL: createURL("/login/device/code"),
43+
AuthorizeURL: createURL("/login/oauth/authorize"),
44+
TokenURL: createURL("/login/oauth/access_token"),
45+
}, nil
46+
}
47+
2748
// GitHubHost constructs a Host from the given URL to a GitHub instance.
49+
//
50+
// Deprecated: `GitHubHost` can panic with a malformed `hostURL`. Use `NewGitHubHost` instead for graceful error handling.
2851
func GitHubHost(hostURL string) *Host {
2952
u, _ := url.Parse(hostURL)
3053

Diff for: oauth_device.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,14 @@ func (oa *Flow) DeviceFlow() (*api.AccessToken, error) {
2929
if stdout == nil {
3030
stdout = os.Stdout
3131
}
32+
3233
host := oa.Host
3334
if host == nil {
34-
host = GitHubHost("https://" + oa.Hostname)
35+
host, err := NewGitHubHost("https://" + oa.Hostname)
36+
if err != nil {
37+
return nil, fmt.Errorf("error parsing the hostname '%s': %w", host, err)
38+
}
39+
oa.Host = host
3540
}
3641

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

Diff for: oauth_webapp.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,13 @@ import (
1414
// flow, blocks until the user completes authorization and is redirected back, and returns the access token.
1515
func (oa *Flow) WebAppFlow() (*api.AccessToken, error) {
1616
host := oa.Host
17+
1718
if host == nil {
18-
host = GitHubHost("https://" + oa.Hostname)
19+
host, err := NewGitHubHost("https://" + oa.Hostname)
20+
if err != nil {
21+
return nil, fmt.Errorf("error parsing the hostname '%s': %w", host, err)
22+
}
23+
oa.Host = host
1924
}
2025

2126
flow, err := webapp.InitFlow()

0 commit comments

Comments
 (0)