/
cli.go
224 lines (200 loc) · 6.36 KB
/
cli.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// Package signup is for the signup command backed by a signup service
package signup
import (
"bufio"
"fmt"
"os"
"strings"
"syscall"
"time"
cl "github.com/micro/go-micro/v3/client"
clinamespace "github.com/micro/micro/v3/client/cli/namespace"
clitoken "github.com/micro/micro/v3/client/cli/token"
cliutil "github.com/micro/micro/v3/client/cli/util"
"github.com/micro/micro/v3/cmd"
"github.com/micro/micro/v3/internal/report"
pb "github.com/micro/micro/v3/proto/signup"
"github.com/micro/micro/v3/service/auth"
"github.com/micro/micro/v3/service/client"
"github.com/micro/micro/v3/service/context"
"github.com/urfave/cli/v2"
"golang.org/x/crypto/ssh/terminal"
)
// Run execs flow for the signup
func Run(ctx *cli.Context) error {
email := ctx.String("email")
if ctx.Bool("recover") {
signupService := pb.NewSignupService("signup", client.DefaultClient)
_, err := signupService.Recover(context.DefaultContext, &pb.RecoverRequest{
Email: email,
}, cl.WithRequestTimeout(10*time.Second))
return err
}
env := cliutil.GetEnv(ctx)
reader := bufio.NewReader(os.Stdin)
// no email specified
if len(email) == 0 {
// get email from prompt
fmt.Print("Enter email address: ")
email, _ = reader.ReadString('\n')
email = strings.TrimSpace(email)
}
// send a verification email to the user
signupService := pb.NewSignupService("signup", client.DefaultClient)
_, err := signupService.SendVerificationEmail(context.DefaultContext, &pb.SendVerificationEmailRequest{
Email: email,
}, cl.WithRequestTimeout(10*time.Second))
if err != nil {
fmt.Printf("Error sending email during signup: %s\n", err)
report.Errorf(ctx, "%v: Error sending email during signup: %s", email, err)
os.Exit(1)
}
fmt.Print("Enter the OTP sent to your email address: ")
otp, _ := reader.ReadString('\n')
otp = strings.TrimSpace(otp)
// verify the email and password entered
rsp, err := signupService.Verify(context.DefaultContext, &pb.VerifyRequest{
Email: email,
Token: otp,
}, cl.WithRequestTimeout(10*time.Second))
if err != nil {
fmt.Printf("Error verifying: %s\n", err)
os.Exit(1)
}
isJoining := false
if ns := rsp.Namespaces; len(ns) > 0 {
fmt.Printf("\nYou've been invited to the '%v' namespace.\nDo you want to join it or create your own? Please type \"own\" or \"join\": ", ns[0])
for {
answer, _ := reader.ReadString('\n')
answer = strings.TrimSpace(answer)
validAnswer := false
switch answer {
case "join":
isJoining = true
validAnswer = true
break
case "own":
validAnswer = true
default:
fmt.Printf("Answer \"%v\" is invalid. Valid answers are: \"own\" or \"join\": ", answer)
}
if validAnswer {
break
}
}
}
password := ctx.String("password")
if len(password) == 0 {
for {
fmt.Print("Enter a new password: ")
bytePw, _ := terminal.ReadPassword(int(syscall.Stdin))
pw := string(bytePw)
pw = strings.TrimSpace(pw)
fmt.Println()
fmt.Print("Verify your password: ")
bytePwVer, _ := terminal.ReadPassword(int(syscall.Stdin))
pwVer := string(bytePwVer)
pwVer = strings.TrimSpace(pwVer)
fmt.Println()
if pw != pwVer {
fmt.Println("Passwords do not match. Please try again.")
continue
}
password = pw
break
}
}
// payment method id read from user input
var paymentMethodID string
// Only take payment method if not joining, ie. creating their own namespace
// and M3O platform subscription
if !isJoining {
// print the message returned from the verification process
if len(rsp.Message) > 0 {
// print with space
fmt.Printf("\n%s\n", rsp.Message)
}
// payment required
if rsp.PaymentRequired {
for {
hasRsp, err := signupService.HasPaymentMethod(context.DefaultContext, &pb.HasPaymentMethodRequest{
Token: otp,
})
if err == nil && hasRsp != nil && hasRsp.Has {
break
}
time.Sleep(2 * time.Second)
}
}
}
// complete the signup flow
signupNamespace := ""
if isJoining && len(rsp.Namespaces) > 0 {
signupNamespace = rsp.Namespaces[0]
}
signupRsp, err := signupService.CompleteSignup(context.DefaultContext, &pb.CompleteSignupRequest{
Email: email,
Token: otp,
PaymentMethodID: paymentMethodID,
Secret: password,
Namespace: signupNamespace,
}, cl.WithRequestTimeout(30*time.Second))
if err != nil {
fmt.Printf("Error completing signup: %s\n", err)
os.Exit(1)
}
tok := signupRsp.AuthToken
if err := clinamespace.Add(signupRsp.Namespace, env.Name); err != nil {
fmt.Printf("Error adding namespace: %s\n", err)
report.Errorf(ctx, "Error adding namespace: %s", err)
os.Exit(1)
}
if err := clinamespace.Set(signupRsp.Namespace, env.Name); err != nil {
fmt.Printf("Error setting namespace: %s\n", err)
report.Errorf(ctx, "Error setting namespace: %s", err)
os.Exit(1)
}
if err := clitoken.Save(env.Name, &auth.AccountToken{
AccessToken: tok.AccessToken,
RefreshToken: tok.RefreshToken,
Expiry: time.Unix(tok.Expiry, 0),
}); err != nil {
fmt.Printf("Error saving token: %s\n", err)
report.Errorf(ctx, "Error saving token: %s", err)
os.Exit(1)
}
// the user has now signed up and logged in
// @todo save the namespace from the last call and use that.
fmt.Println("\nSignup complete! You're now logged in.")
report.Success(ctx, email)
return nil
}
func init() {
cmd.Register(&cli.Command{
Name: "signup",
Usage: "Signup to the Micro Platform",
Description: "Enables signup to the Micro Platform which can then be accessed via `micro env set platform` and `micro login`",
Action: Run,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "email",
Usage: "Email address to use for signup",
},
// In fact this is only here currently to help testing
// as the signup flow can't be automated yet.
// The testing breaks because we take the password
// with the `terminal` package that makes input invisible.
// That breaks tests though so password flag is used to get around tests.
// @todo maybe payment method token and email sent verification
// code should also be invisible. Problem for an other day.
&cli.StringFlag{
Name: "password",
Usage: "Password to use for login. If not provided, will be asked for during login. Useful for automated scripts",
},
&cli.BoolFlag{
Name: "recover",
Usage: "Emails you the namespaces you have access to. micro signup --recover --email=youremail@domain.com",
},
},
})
}