-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathlogin.go
More file actions
80 lines (67 loc) · 2.33 KB
/
login.go
File metadata and controls
80 lines (67 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package login
import (
"fmt"
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
"github.com/stackitcloud/stackit-cli/internal/pkg/auth"
"github.com/stackitcloud/stackit-cli/internal/pkg/examples"
"github.com/stackitcloud/stackit-cli/internal/pkg/flags"
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
"github.com/stackitcloud/stackit-cli/internal/pkg/types"
"github.com/spf13/cobra"
)
const (
portFlag = "port"
)
type inputModel struct {
Port *int
}
func NewCmd(params *types.CmdParams) *cobra.Command {
cmd := &cobra.Command{
Use: "login",
Short: "Logs in to the STACKIT CLI",
Long: fmt.Sprintf("%s\n%s",
"Logs in to the STACKIT CLI using a user account.",
"The authentication is done via a web-based authorization flow, where the command will open a browser window in which you can login to your STACKIT account."),
Args: args.NoArgs,
Example: examples.Build(
examples.NewExample(
`Login to the STACKIT CLI. This command will open a browser window where you can login to your STACKIT account`,
"$ stackit auth login"),
),
RunE: func(cmd *cobra.Command, args []string) error {
model, err := parseInput(params.Printer, cmd, args)
if err != nil {
return err
}
err = auth.AuthorizeUser(params.Printer, auth.UserAuthConfig{
IsReauthentication: false,
Port: model.Port,
})
if err != nil {
return fmt.Errorf("authorization failed: %w", err)
}
params.Printer.Outputln("Successfully logged into STACKIT CLI.\n")
return nil
},
}
configureFlags(cmd)
return cmd
}
func configureFlags(cmd *cobra.Command) {
cmd.Flags().Int(portFlag, 0,
"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.",
)
}
func parseInput(p *print.Printer, cmd *cobra.Command, _ []string) (*inputModel, error) {
port := flags.FlagToIntPointer(p, cmd, portFlag)
// For the CLI client only callback URLs with localhost:[8000-8020] are valid. Additional callbacks must be enabled in the backend.
if port != nil && (*port < 8000 || *port > 8020) {
return nil, fmt.Errorf("port must be between 8000 and 8020")
}
model := inputModel{
Port: port,
}
p.DebugInputModel(model)
return &model, nil
}