/
login.go
70 lines (61 loc) · 1.7 KB
/
login.go
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
package cmd
import (
"fmt"
"io"
"os"
"strings"
"github.com/metal-stack/metal-lib/auth"
"github.com/metal-stack/metalctl/pkg/api"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
func newLoginCmd(c *config) *cobra.Command {
loginCmd := &cobra.Command{
Use: "login",
Short: "login user and receive token",
Long: "login and receive token that will be used to authenticate commands.",
RunE: func(cmd *cobra.Command, args []string) error {
var console io.Writer
var handler auth.TokenHandlerFunc
if viper.GetBool("print-only") {
// do not print to console
handler = func(tokenInfo auth.TokenInfo) error {
fmt.Fprintln(c.out, tokenInfo.IDToken)
return nil
}
} else {
cs, err := api.GetContexts()
if err != nil {
return err
}
console = os.Stdout
handler = auth.NewUpdateKubeConfigHandler(viper.GetString("kubeconfig"), console, auth.WithContextName(formatContextName(cloudContext, cs.CurrentContext)))
}
ctx := api.MustDefaultContext()
scopes := auth.DexScopes
if ctx.IssuerType == "generic" {
scopes = auth.GenericScopes
} else if ctx.CustomScopes != "" {
cs := strings.Split(ctx.CustomScopes, ",")
for i := range cs {
cs[i] = strings.TrimSpace(cs[i])
}
scopes = cs
}
config := auth.Config{
ClientID: ctx.ClientID,
ClientSecret: ctx.ClientSecret,
IssuerURL: ctx.IssuerURL,
Scopes: scopes,
TokenHandler: handler,
Console: console,
Debug: viper.GetBool("debug"),
Log: c.log,
}
fmt.Fprintln(c.out)
return auth.OIDCFlow(config)
},
}
loginCmd.Flags().Bool("print-only", false, "If true, the token is printed to stdout")
return loginCmd
}