Skip to content

Commit

Permalink
Merge pull request #63 from mouismail/mouismail-issue-61
Browse files Browse the repository at this point in the history
create new func  to avoid the bug on issue #61
  • Loading branch information
BagToad authored Oct 8, 2024
2 parents adf5f73 + 9585a35 commit afffc8e
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 3 deletions.
6 changes: 5 additions & 1 deletion examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@ import (
// github.com, as its Device flow support is globally available, but it enables logging in to
// self-hosted GitHub instances as well.
func ExampleFlow_DetectFlow() {
host, err := oauth.NewGitHubHost("https://github.com")
if err != nil {
panic(err)
}
flow := &oauth.Flow{
Host: oauth.GitHubHost("https://github.com"),
Host: host,
ClientID: os.Getenv("OAUTH_CLIENT_ID"),
ClientSecret: os.Getenv("OAUTH_CLIENT_SECRET"), // only applicable to web app flow
CallbackURI: "http://127.0.0.1/callback", // only applicable to web app flow
Expand Down
23 changes: 23 additions & 0 deletions oauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io"
"net/http"
"net/url"
"strings"

"github.com/cli/oauth/api"
"github.com/cli/oauth/device"
Expand All @@ -24,7 +25,29 @@ type Host struct {
TokenURL string
}

// NewGitHubHost constructs a Host from the given URL to a GitHub instance.
func NewGitHubHost(hostURL string) (*Host, error) {
base, err := url.Parse(strings.TrimSpace(hostURL))
if err != nil {
return nil, err
}

createURL := func(path string) string {
u := *base // Copy base URL
u.Path = path
return u.String()
}

return &Host{
DeviceCodeURL: createURL("/login/device/code"),
AuthorizeURL: createURL("/login/oauth/authorize"),
TokenURL: createURL("/login/oauth/access_token"),
}, nil
}

// GitHubHost constructs a Host from the given URL to a GitHub instance.
//
// Deprecated: `GitHubHost` can panic with a malformed `hostURL`. Use `NewGitHubHost` instead for graceful error handling.
func GitHubHost(hostURL string) *Host {
u, _ := url.Parse(hostURL)

Expand Down
7 changes: 6 additions & 1 deletion oauth_device.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,14 @@ func (oa *Flow) DeviceFlow() (*api.AccessToken, error) {
if stdout == nil {
stdout = os.Stdout
}

host := oa.Host
if host == nil {
host = GitHubHost("https://" + oa.Hostname)
host, err := NewGitHubHost("https://" + oa.Hostname)
if err != nil {
return nil, fmt.Errorf("error parsing the hostname '%s': %w", host, err)
}
oa.Host = host
}

code, err := device.RequestCode(httpClient, host.DeviceCodeURL, oa.ClientID, oa.Scopes)
Expand Down
7 changes: 6 additions & 1 deletion oauth_webapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@ import (
// flow, blocks until the user completes authorization and is redirected back, and returns the access token.
func (oa *Flow) WebAppFlow() (*api.AccessToken, error) {
host := oa.Host

if host == nil {
host = GitHubHost("https://" + oa.Hostname)
host, err := NewGitHubHost("https://" + oa.Hostname)
if err != nil {
return nil, fmt.Errorf("error parsing the hostname '%s': %w", host, err)
}
oa.Host = host
}

flow, err := webapp.InitFlow()
Expand Down

0 comments on commit afffc8e

Please sign in to comment.