Skip to content

Commit db876b0

Browse files
authored
Add a skip_browser argument to make auto-launching of the default browser optional (#182)
1 parent 7311fc7 commit db876b0

File tree

1 file changed

+31
-10
lines changed

1 file changed

+31
-10
lines changed

cli.go

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"path"
1313
"regexp"
1414
"runtime"
15+
"strconv"
1516
"strings"
1617
"time"
1718

@@ -20,17 +21,20 @@ import (
2021
)
2122

2223
const (
23-
defaultMount = "oidc"
24-
defaultListenAddress = "localhost"
25-
defaultPort = "8250"
26-
defaultCallbackHost = "localhost"
27-
defaultCallbackMethod = "http"
24+
defaultMount = "oidc"
25+
defaultListenAddress = "localhost"
26+
defaultPort = "8250"
27+
defaultCallbackHost = "localhost"
28+
defaultCallbackMethod = "http"
29+
defaultSkipBrowserLaunch = false
2830
)
2931

3032
var errorRegex = regexp.MustCompile(`(?s)Errors:.*\* *(.*)`)
3133

3234
type CLIHandler struct{}
3335

36+
// loginResp implements vault's command.LoginHandler interface, but we do not check
37+
// the implementation as that'd cause an import loop.
3438
type loginResp struct {
3539
secret *api.Secret
3640
err error
@@ -74,6 +78,15 @@ func (h *CLIHandler) Auth(c *api.Client, m map[string]string) (*api.Secret, erro
7478
callbackPort = port
7579
}
7680

81+
skipBrowserLaunch := defaultSkipBrowserLaunch
82+
if x, ok := m["skip_browser"]; ok {
83+
parsed, err := strconv.ParseBool(x)
84+
if err != nil {
85+
return nil, fmt.Errorf("Failed to parse \"skip_browser\" as a boolean: %w", err)
86+
}
87+
skipBrowserLaunch = parsed
88+
}
89+
7790
role := m["role"]
7891

7992
authURL, clientNonce, err := fetchAuthURL(c, role, mount, callbackPort, callbackMethod, callbackHost)
@@ -91,10 +104,15 @@ func (h *CLIHandler) Auth(c *api.Client, m map[string]string) (*api.Secret, erro
91104
defer listener.Close()
92105

93106
// Open the default browser to the callback URL.
94-
fmt.Fprintf(os.Stderr, "Complete the login via your OIDC provider. Launching browser to:\n\n %s\n\n\n", authURL)
95-
if err := openURL(authURL); err != nil {
96-
fmt.Fprintf(os.Stderr, "Error attempting to automatically open browser: '%s'.\nPlease visit the authorization URL manually.", err)
107+
if !skipBrowserLaunch {
108+
fmt.Fprintf(os.Stderr, "Complete the login via your OIDC provider. Launching browser to:\n\n %s\n\n\n", authURL)
109+
if err := openURL(authURL); err != nil {
110+
fmt.Fprintf(os.Stderr, "Error attempting to automatically open browser: '%s'.\nPlease visit the authorization URL manually.", err)
111+
}
112+
} else {
113+
fmt.Fprintf(os.Stderr, "Complete the login via your OIDC provider. Open the following link in your browser:\n\n %s\n\n\n", authURL)
97114
}
115+
fmt.Fprintf(os.Stderr, "Waiting for OIDC authentication to complete...\n")
98116

99117
// Start local server
100118
go func() {
@@ -283,7 +301,7 @@ Usage: vault login -method=oidc [CONFIG K=V...]
283301
Configuration:
284302
285303
role=<string>
286-
Vault role of type "OIDC" to use for authentication.
304+
Vault role of type "OIDC" to use for authentication.
287305
288306
listenaddress=<string>
289307
Optional address to bind the OIDC callback listener to (default: localhost).
@@ -298,7 +316,10 @@ Configuration:
298316
Optional callback host address to use in OIDC redirect_uri (default: localhost).
299317
300318
callbackport=<string>
301-
Optional port to to use in OIDC redirect_uri (default: the value set for port).
319+
Optional port to to use in OIDC redirect_uri (default: the value set for port).
320+
321+
skip_browser=<bool>
322+
Toggle the automatic launching of the default browser to the login URL. (default: false).
302323
`
303324

304325
return strings.TrimSpace(help)

0 commit comments

Comments
 (0)