-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathauth.go
More file actions
233 lines (181 loc) · 5.12 KB
/
auth.go
File metadata and controls
233 lines (181 loc) · 5.12 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
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
225
226
227
228
229
230
231
232
233
package parser
import (
"fmt"
"github.com/deis/workflow/client/cmd"
docopt "github.com/docopt/docopt-go"
)
// Auth routes auth commands to the specific function.
func Auth(argv []string) error {
usage := `
Valid commands for auth:
auth:register register a new user
auth:login authenticate against a controller
auth:logout clear the current user session
auth:passwd change the password for the current user
auth:whoami display the current user
auth:cancel remove the current user account
auth:regenerate regenerate user tokens
Use 'deis help [command]' to learn more.
`
switch argv[0] {
case "auth:register":
return authRegister(argv)
case "auth:login":
return authLogin(argv)
case "auth:logout":
return authLogout(argv)
case "auth:passwd":
return authPasswd(argv)
case "auth:whoami":
return authWhoami(argv)
case "auth:cancel":
return authCancel(argv)
case "auth:regenerate":
return authRegenerate(argv)
case "auth":
fmt.Print(usage)
return nil
default:
PrintUsage()
return nil
}
}
func authRegister(argv []string) error {
usage := `
Registers a new user with a Deis controller.
Usage: deis auth:register <controller> [options]
Arguments:
<controller>
fully-qualified controller URI, e.g. 'http://deis.local3.deisapp.com/'
Options:
--username=<username>
provide a username for the new account.
--password=<password>
provide a password for the new account.
--email=<email>
provide an email address.
--ssl-verify=false
disables SSL certificate verification for API requests
`
args, err := docopt.Parse(usage, argv, true, "", false, true)
if err != nil {
return err
}
controller := safeGetValue(args, "<controller>")
username := safeGetValue(args, "--username")
password := safeGetValue(args, "--password")
email := safeGetValue(args, "--email")
sslVerify := false
if args["--ssl-verify"] != nil && args["--ssl-verify"].(string) == "true" {
sslVerify = true
}
return cmd.Register(controller, username, password, email, sslVerify)
}
func authLogin(argv []string) error {
usage := `
Logs in by authenticating against a controller.
Usage: deis auth:login <controller> [options]
Arguments:
<controller>
a fully-qualified controller URI, e.g. "http://deis.local3.deisapp.com/".
Options:
--username=<username>
provide a username for the account.
--password=<password>
provide a password for the account.
--ssl-verify=false
disables SSL certificate verification for API requests
`
args, err := docopt.Parse(usage, argv, true, "", false, true)
if err != nil {
return err
}
controller := safeGetValue(args, "<controller>")
username := safeGetValue(args, "--username")
password := safeGetValue(args, "--password")
sslVerify := false
if args["--ssl-verify"] != nil && args["--ssl-verify"].(string) == "true" {
sslVerify = true
}
return cmd.Login(controller, username, password, sslVerify)
}
func authLogout(argv []string) error {
usage := `
Logs out from a controller and clears the user session.
Usage: deis auth:logout
`
if _, err := docopt.Parse(usage, argv, true, "", false, true); err != nil {
return err
}
return cmd.Logout()
}
func authPasswd(argv []string) error {
usage := `
Changes the password for the current user.
Usage: deis auth:passwd [options]
Options:
--password=<password>
the current password for the account.
--new-password=<new-password>
the new password for the account.
--username=<username>
the account's username.
`
args, err := docopt.Parse(usage, argv, true, "", false, true)
if err != nil {
return err
}
username := safeGetValue(args, "--username")
password := safeGetValue(args, "--password")
newPassword := safeGetValue(args, "--new-password")
return cmd.Passwd(username, password, newPassword)
}
func authWhoami(argv []string) error {
usage := `
Displays the currently logged in user.
Usage: deis auth:whoami
`
if _, err := docopt.Parse(usage, argv, true, "", false, true); err != nil {
return err
}
return cmd.Whoami()
}
func authCancel(argv []string) error {
usage := `
Cancels and removes the current account.
Usage: deis auth:cancel [options]
Options:
--username=<username>
provide a username for the account.
--password=<password>
provide a password for the account.
--yes
force "yes" when prompted.
`
args, err := docopt.Parse(usage, argv, true, "", false, true)
if err != nil {
return err
}
username := safeGetValue(args, "--username")
password := safeGetValue(args, "--password")
yes := args["--yes"].(bool)
return cmd.Cancel(username, password, yes)
}
func authRegenerate(argv []string) error {
usage := `
Regenerates auth token, defaults to regenerating token for the current user.
Usage: deis auth:regenerate [options]
Options:
-u --username=<username>
specify user to regenerate. Requires admin privilages.
--all
regenerate token for every user. Requires admin privilages.
`
args, err := docopt.Parse(usage, argv, true, "", false, true)
if err != nil {
return err
}
username := safeGetValue(args, "--username")
all := args["--all"].(bool)
return cmd.Regenerate(username, all)
}