Skip to content

Commit

Permalink
Fix default browser boolean semantics (#266)
Browse files Browse the repository at this point in the history
* Fix default browser boolean semantics

* Rename variables for clarity

* Add changelog
  • Loading branch information
ohm authored Aug 29, 2024
1 parent 61424ce commit 652f10c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
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

0 comments on commit 652f10c

Please sign in to comment.