Skip to content

Commit 428f564

Browse files
committed
feat(auth): add no browser open flag to login
Add an additional option to only print the authorization URL and not open it directly in the standard browser. Especially useful if another browser or an incognito tab need to be used.
1 parent eb13988 commit 428f564

3 files changed

Lines changed: 36 additions & 15 deletions

File tree

docs/stackit_auth_login.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ stackit auth login [flags]
2121
### Options
2222

2323
```
24-
-h, --help Help for "stackit auth login"
25-
--port int The port on which the callback server will listen to. By default, it tries to bind a port between 8000 and 8020.
26-
When a value is specified, it will only try to use the specified port. Valid values are within the range of 8000 to 8020.
24+
-h, --help Help for "stackit auth login"
25+
--no-browser-open If set, does not open the browser automatically but only prints the URL to the terminal
26+
--port int The port on which the callback server will listen to. By default, it tries to bind a port between 8000 and 8020.
27+
When a value is specified, it will only try to use the specified port. Valid values are within the range of 8000 to 8020.
2728
```
2829

2930
### Options inherited from parent commands

internal/cmd/auth/login/login.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@ import (
1414
)
1515

1616
const (
17-
portFlag = "port"
17+
portFlag = "port"
18+
noBrowserOpenFlag = "no-browser-open"
1819
)
1920

2021
type inputModel struct {
21-
Port *int
22+
Port *int
23+
NoBrowserOpen bool
2224
}
2325

2426
func NewCmd(params *types.CmdParams) *cobra.Command {
@@ -43,6 +45,7 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
4345
err = auth.AuthorizeUser(params.Printer, auth.UserAuthConfig{
4446
IsReauthentication: false,
4547
Port: model.Port,
48+
NoBrowserOpen: model.NoBrowserOpen,
4649
})
4750
if err != nil {
4851
return fmt.Errorf("authorization failed: %w", err)
@@ -62,6 +65,9 @@ func configureFlags(cmd *cobra.Command) {
6265
"The port on which the callback server will listen to. By default, it tries to bind a port between 8000 and 8020.\n"+
6366
"When a value is specified, it will only try to use the specified port. Valid values are within the range of 8000 to 8020.",
6467
)
68+
cmd.Flags().Bool(noBrowserOpenFlag, false,
69+
"If set, does not open the browser automatically but only prints the URL to the terminal",
70+
)
6571
}
6672

6773
func parseInput(p *print.Printer, cmd *cobra.Command, _ []string) (*inputModel, error) {
@@ -71,8 +77,11 @@ func parseInput(p *print.Printer, cmd *cobra.Command, _ []string) (*inputModel,
7177
return nil, fmt.Errorf("port must be between 8000 and 8020")
7278
}
7379

80+
noBrowserOpen := flags.FlagToBoolValue(p, cmd, noBrowserOpenFlag)
81+
7482
model := inputModel{
75-
Port: port,
83+
Port: port,
84+
NoBrowserOpen: noBrowserOpen,
7685
}
7786

7887
p.DebugInputModel(model)

internal/pkg/auth/user_login.go

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ type UserAuthConfig struct {
5151
IsReauthentication bool
5252
// Port defines which port should be used for the UserAuthFlow callback
5353
Port *int
54+
// NoBrowserOpen stops the browser from opening automatically during the login flow
55+
NoBrowserOpen bool
5456
}
5557

5658
type apiClient interface {
@@ -250,19 +252,28 @@ func AuthorizeUser(p *print.Printer, authConfig UserAuthConfig) error {
250252
}
251253
})
252254

253-
p.Debug(print.DebugLevel, "opening browser for authentication: %s", authorizationURL)
254255
p.Debug(print.DebugLevel, "using authentication server on %s", idpWellKnownConfig.Issuer)
255256
p.Debug(print.DebugLevel, "using client ID %s for authentication ", idpClientID)
256257

257-
// Open a browser window to the authorizationURL
258-
err = openBrowser(authorizationURL)
259-
if err != nil {
260-
return fmt.Errorf("open browser to URL %s: %w", authorizationURL, err)
261-
}
258+
if authConfig.NoBrowserOpen {
259+
p.Debug(print.DebugLevel, "skipping browser opening since the commandline flag was passed")
262260

263-
// Print the link
264-
p.Info("Your browser has been opened to visit:\n\n")
265-
p.Info("%s\n\n", authorizationURL)
261+
// Print the link in terminal
262+
p.Info("Open the following URL in your browser:\n\n")
263+
p.Info("%s\n\n", authorizationURL)
264+
} else {
265+
p.Debug(print.DebugLevel, "opening browser for authentication: %s", authorizationURL)
266+
267+
// Open a browser window to the authorizationURL
268+
err = openBrowser(authorizationURL)
269+
if err != nil {
270+
return fmt.Errorf("open browser to URL %s: %w", authorizationURL, err)
271+
}
272+
273+
// Print the link
274+
p.Info("Your browser has been opened to visit:\n\n")
275+
p.Info("%s\n\n", authorizationURL)
276+
}
266277

267278
// Start the blocking web server loop
268279
// It will exit when the handlers get fired and call server.Close()

0 commit comments

Comments
 (0)