Skip to content
Open
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
7 changes: 4 additions & 3 deletions docs/stackit_auth_login.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ stackit auth login [flags]
### Options

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

### Options inherited from parent commands
Expand Down
15 changes: 12 additions & 3 deletions internal/cmd/auth/login/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ import (
)

const (
portFlag = "port"
portFlag = "port"
noBrowserOpenFlag = "no-browser-open"
)

type inputModel struct {
Port *int
Port *int
NoBrowserOpen bool
}

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

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

noBrowserOpen := flags.FlagToBoolValue(p, cmd, noBrowserOpenFlag)

model := inputModel{
Port: port,
Port: port,
NoBrowserOpen: noBrowserOpen,
}

p.DebugInputModel(model)
Expand Down
29 changes: 20 additions & 9 deletions internal/pkg/auth/user_login.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ type UserAuthConfig struct {
IsReauthentication bool
// Port defines which port should be used for the UserAuthFlow callback
Port *int
// NoBrowserOpen stops the browser from opening automatically during the login flow
NoBrowserOpen bool
}

type apiClient interface {
Expand Down Expand Up @@ -250,19 +252,28 @@ func AuthorizeUser(p *print.Printer, authConfig UserAuthConfig) error {
}
})

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

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

// Print the link
p.Info("Your browser has been opened to visit:\n\n")
p.Info("%s\n\n", authorizationURL)
// Print the link in terminal
p.Info("Open the following URL in your browser:\n\n")
p.Info("%s\n\n", authorizationURL)
} else {
p.Debug(print.DebugLevel, "opening browser for authentication: %s", authorizationURL)

// Open a browser window to the authorizationURL
err = openBrowser(authorizationURL)
if err != nil {
return fmt.Errorf("open browser to URL %s: %w", authorizationURL, err)
}

// Print the link
p.Info("Your browser has been opened to visit:\n\n")
p.Info("%s\n\n", authorizationURL)
}

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