Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix default browser boolean semantics #266

Merged
merged 3 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/266.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
Fixed default browser opening logic.
```
20 changes: 10 additions & 10 deletions auth/browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,29 @@ import (
type browserLogin struct {
oauthConfig *oauth2.Config

// Wether to open a browser in an external window or not.
openBrowser bool
// Wether to open the default browser in an external window.
noDefaultBrowser bool
}

// NewBrowserLogin will return an oauth2.TokenSource that will return a Token from an interactive browser login.
func NewBrowserLogin(oauthConfig *oauth2.Config, openBrowser bool) *browserLogin {
func NewBrowserLogin(oauthConfig *oauth2.Config, noDefaultBrowser bool) *browserLogin {
return &browserLogin{
oauthConfig: oauthConfig,
openBrowser: openBrowser,
oauthConfig: oauthConfig,
noDefaultBrowser: noDefaultBrowser,
}
}

// Token will return an oauth2.Token retrieved from an interactive browser login.
func (b *browserLogin) Token() (*oauth2.Token, error) {
browser := &oauthBrowser{}
return browser.GetTokenFromBrowser(context.Background(), b.oauthConfig, b.openBrowser)
return browser.GetTokenFromBrowser(context.Background(), b.oauthConfig, b.noDefaultBrowser)
}

// oauthBrowser implements the Browser interface using the real OAuth2 login flow.
type oauthBrowser struct{}

// GetTokenFromBrowser opens a browser window for the user to log in and handles the OAuth2 flow to obtain a token.
func (b *oauthBrowser) GetTokenFromBrowser(ctx context.Context, conf *oauth2.Config, openBrowser bool) (*oauth2.Token, error) {
func (b *oauthBrowser) GetTokenFromBrowser(ctx context.Context, conf *oauth2.Config, noDefaultBrowser bool) (*oauth2.Token, error) {
// Prepare the /authorize request with randomly generated state, offline access option, and audience
aud := "https://api.hashicorp.cloud"
opt := oauth2.SetAuthURLParam("audience", aud)
Expand All @@ -57,14 +57,14 @@ func (b *oauthBrowser) GetTokenFromBrowser(ctx context.Context, conf *oauth2.Con
defer signal.Stop(sigintCh)

// Launch a request to HCP's authorization endpoint.
if openBrowser {
if noDefaultBrowser {
colorstring.Printf("[bold][yellow]Please open the following URL in your browser and follow the instructions to authenticate:\n%s\n", authzURL)
} else {
colorstring.Printf("[bold][yellow]The default web browser has been opened at %s. Please continue the login in the web browser.\n", authzURL)

if err := open.Start(authzURL); err != nil {
return nil, fmt.Errorf("failed to open browser at URL %q: %w", authzURL, err)
}
} else {
colorstring.Printf("[bold][yellow]Please open the following URL in your browser and follow the instructions to authenticate:\n%s\n", authzURL)
}

// Start callback server
Expand Down