-
Notifications
You must be signed in to change notification settings - Fork 12
/
login.go
218 lines (178 loc) · 4.3 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
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
// Copyright (c) 2020 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.
package cmd
import (
"errors"
"fmt"
"net/http"
"github.com/go-vela/sdk-go/vela"
"github.com/go-vela/types/library"
"github.com/manifoldco/promptui"
"github.com/urfave/cli"
)
// loginCmd defines the command for authenticating and logging in to Vela.
var loginCmd = cli.Command{
Name: "login",
Category: "Authentication",
Aliases: []string{"l"},
Description: "Use this command to authenticate and login to Vela.",
Usage: "Authenticate and generate a token with Vela",
Action: authenticate,
Flags: []cli.Flag{
// required flags to be supplied to a command
cli.StringFlag{
Name: "addr",
Usage: "location of vela server",
},
// optional flags that can be supplied to a command
cli.StringFlag{
Name: "username,u",
Usage: "Override username prompt",
},
cli.StringFlag{
Name: "password,p",
Usage: "Override username prompt",
},
},
CustomHelpTemplate: fmt.Sprintf(`%s
EXAMPLES:
1. Login to Vela with terminal prompts.
$ {{.HelpName}} --addr https://vela.example.com
2. Login to Vela with username and password.
$ {{.HelpName}} --username foo --password bar
`, cli.CommandHelpTemplate),
}
func authenticate(c *cli.Context) error {
var (
message string
token string
)
url := c.String("addr")
if len(url) == 0 { // use global variable if flags isn't provided
url = c.GlobalString("addr")
}
// create a carval client
client, err := vela.NewClient(url, nil)
if err != nil {
return err
}
// get username from user input
username, err := getUsername(c)
if err != nil {
return err
}
// get password from user input
password, err := getPassword(c)
if err != nil {
return err
}
req := library.Login{
Username: &username,
Password: &password,
OTP: vela.String(""),
}
auth, resp, err := client.Authorization.Login(&req)
if err != nil {
//TODO: clean this workflow up
if resp.StatusCode != http.StatusUnauthorized {
return err
}
}
// If user hits an endpoint other than the
// Vela server that can't process request
// bomb out and throw error
if http.StatusUnauthorized < resp.StatusCode {
return fmt.Errorf("unable to process request")
}
// retry authentication in case user requires an OTP code
switch resp.StatusCode {
case http.StatusUnauthorized:
// get otp from user input
otp, err := getOTP()
if err != nil {
return err
}
req = library.Login{
Username: &username,
Password: &password,
OTP: &otp,
}
auth, _, err := client.Authorization.Login(&req)
if err != nil {
return err
}
// craft response to user
message = fmt.Sprintf("Generated token: %s", auth.GetToken())
token = auth.GetToken()
default:
// craft response to user
message = fmt.Sprintf("Generated token: %s", auth.GetToken())
token = auth.GetToken()
}
if token != "" {
fmt.Println(message)
}
return nil
}
// helper function get a username from user input
func getUsername(c *cli.Context) (string, error) {
if len(c.String("username")) != 0 {
return c.String("username"), nil
}
validate := func(input string) error {
if len(input) < 0 {
return errors.New("Username must not be blank")
}
return nil
}
prompt := promptui.Prompt{
Label: "Enter a username: ",
Validate: validate,
}
u, err := prompt.Run()
if err != nil {
return "", err
}
return u, nil
}
// helper function get a password from user input
func getPassword(c *cli.Context) (string, error) {
if len(c.String("password")) != 0 {
return c.String("password"), nil
}
validate := func(input string) error {
if len(input) < 0 {
return errors.New("Username must not be blank")
}
return nil
}
prompt := promptui.Prompt{
Label: "Enter a password: ",
Validate: validate,
Mask: '*',
}
p, err := prompt.Run()
if err != nil {
return "", err
}
return p, nil
}
// helper function get a one time passsword from user input
func getOTP() (string, error) {
validate := func(input string) error {
if len(input) < 0 {
return errors.New("OTP must not be blank")
}
return nil
}
prompt := promptui.Prompt{
Label: "Enter a 2fa code: ",
Validate: validate,
}
otp, err := prompt.Run()
if err != nil {
return "", err
}
return otp, nil
}