Skip to content

Commit

Permalink
Use url.URL when it makes sense
Browse files Browse the repository at this point in the history
  • Loading branch information
cyx committed Jan 25, 2021
1 parent b6fb068 commit 99dcf18
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
3 changes: 2 additions & 1 deletion internal/auth/exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ func ExchangeCodeForToken(baseDomain, clientID, clientSecret, code, cbURL string
"redirect_uri": {cbURL},
}

r, err := http.PostForm(fmt.Sprintf("https://%s/oauth/token", baseDomain), data)
u := url.URL{Scheme: "https", Host: baseDomain, Path: "/oauth/token"}
r, err := http.PostForm(u.String(), data)
if err != nil {
return nil, fmt.Errorf("unable to exchange code for token: %w", err)
}
Expand Down
5 changes: 3 additions & 2 deletions internal/auth/user_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"time"
)

Expand Down Expand Up @@ -32,9 +33,9 @@ type UserInfo struct {

// FetchUserInfo fetches and parses user information with the provided access token.
func FetchUserInfo(baseDomain, token string) (*UserInfo, error) {
userInfoEndpoint := fmt.Sprintf("https://%s/userinfo", baseDomain)
endpoint := url.URL{Scheme: "https", Host: baseDomain, Path: "/userinfo"}

req, err := http.NewRequest("GET", userInfoEndpoint, nil)
req, err := http.NewRequest("GET", endpoint.String(), nil)
if err != nil {
return nil, fmt.Errorf("unable to exchange code for token: %w", err)
}
Expand Down
9 changes: 8 additions & 1 deletion internal/cli/try_login.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,14 @@ func buildInitiateLoginURL(domain, clientID string) (string, error) {
q.Add("scope", cliLoginTestingScopes)
q.Add("redirect_uri", cliLoginTestingCallbackURL)

return fmt.Sprintf("https://%s%s?%s", domain, path, q.Encode()), nil
u := &url.URL{
Scheme: "https",
Host: domain,
Path: path,
RawQuery: q.Encode(),
}

return u.String(), nil
}

// waitForBrowserCallback lauches a new HTTP server listening on
Expand Down

0 comments on commit 99dcf18

Please sign in to comment.