-
Notifications
You must be signed in to change notification settings - Fork 9
/
login.go
129 lines (101 loc) · 3.11 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package auth
import (
"context"
"fmt"
"github.com/morikuni/aec"
"github.com/pkg/browser"
"github.com/spf13/cobra"
"namespacelabs.dev/foundation/internal/auth"
"namespacelabs.dev/foundation/internal/cli/fncobra"
"namespacelabs.dev/foundation/internal/console"
"namespacelabs.dev/foundation/internal/console/tui"
"namespacelabs.dev/foundation/internal/fnapi"
"namespacelabs.dev/foundation/internal/fnerrors"
)
func NewLoginCmd() *cobra.Command {
var (
kind string
)
cmd := &cobra.Command{
Use: "login",
Short: "Login to use Namespace services (DNS and SSL management, etc).",
Args: cobra.NoArgs,
RunE: fncobra.RunE(func(ctx context.Context, args []string) error {
if err := auth.RemoveUser(ctx); err != nil {
return err
}
if err := auth.RemoveTenantToken(ctx); err != nil {
return err
}
res, err := fnapi.StartLogin(ctx, kind)
if err != nil {
return nil
}
stdout := console.Stdout(ctx)
fmt.Fprintf(stdout, "%s\n", aec.Bold.Apply("Login to Namespace"))
if openURL(res.LoginUrl) {
fmt.Fprintf(stdout, "Please complete the login flow in your browser.\n\n %s\n", res.LoginUrl)
} else {
fmt.Fprintf(stdout, "In order to login, open the following URL in your browser:\n\n %s\n", res.LoginUrl)
}
userAuth, err := fnapi.CompleteLogin(ctx, res.LoginId, res.Kind, fnapi.TelemetryOn(ctx).GetClientID())
if err != nil {
return err
}
username, err := auth.StoreUser(ctx, userAuth)
if err != nil {
return err
}
userToken, err := auth.GenerateTokenFromUserAuth(ctx, userAuth)
if err != nil {
return err
}
tt, err := fnapi.ExchangeUserToken(ctx, userToken)
if err != nil {
return err
}
if err := auth.StoreTenantToken(tt.TenantToken); err != nil {
return err
}
fmt.Fprintf(stdout, "\nHi %s, you are now logged in, have a nice day.\n", username)
return nil
}),
}
cmd.Flags().StringVar(&kind, "kind", "clerk", "Internal kind.")
_ = cmd.Flags().MarkHidden("kind")
cmd.AddCommand(NewRobotLogin("robot"))
return cmd
}
func openURL(url string) bool {
err := browser.OpenURL(url)
return err == nil
}
func NewRobotLogin(use string) *cobra.Command {
robotLogin := &cobra.Command{
Use: use,
Short: "Login as a robot.",
Args: cobra.ExactArgs(1),
Hidden: true,
RunE: fncobra.RunE(func(ctx context.Context, args []string) error {
accessToken, err := tui.AskSecret(ctx, "Which Access Token would you like to use today?",
"That would be a Github access token.", "access token")
if err != nil {
return fnerrors.New("failed to read access token: %w", err)
}
userAuth, err := fnapi.RobotLogin(ctx, args[0], string(accessToken))
if err != nil {
return err
}
username, err := auth.StoreUser(ctx, userAuth)
if err != nil {
return err
}
fmt.Fprintf(console.Stdout(ctx), "\nHi %s, you are now logged in, have a nice day.\n", username)
return nil
}),
}
return robotLogin
}