github.com/technosophos/deis@v1.7.1-0.20150915173815-f9005256004b/client/cmd/auth.go (about) 1 package cmd 2 3 import ( 4 "errors" 5 "fmt" 6 "net/url" 7 "strings" 8 "syscall" 9 10 "github.com/deis/deis/client/controller/client" 11 "github.com/deis/deis/client/controller/models/auth" 12 "golang.org/x/crypto/ssh/terminal" 13 ) 14 15 // Register creates a account on a Deis controller. 16 func Register(controller string, username string, password string, email string, 17 sslVerify bool) error { 18 19 u, err := url.Parse(controller) 20 httpClient := client.CreateHTTPClient(sslVerify) 21 22 if err != nil { 23 return err 24 } 25 26 controllerURL, err := chooseScheme(*u) 27 28 if err != nil { 29 return err 30 } 31 32 if err = client.CheckConnection(httpClient, controllerURL); err != nil { 33 return err 34 } 35 36 if username == "" { 37 fmt.Print("username: ") 38 fmt.Scanln(&username) 39 } 40 41 if password == "" { 42 fmt.Print("password: ") 43 password, err = readPassword() 44 fmt.Printf("\npassword (confirm): ") 45 passwordConfirm, err := readPassword() 46 fmt.Println() 47 48 if err != nil { 49 return err 50 } 51 52 if password != passwordConfirm { 53 return errors.New("Password mismatch, aborting registration.") 54 } 55 } 56 57 if email == "" { 58 fmt.Print("email: ") 59 fmt.Scanln(&email) 60 } 61 62 c := &client.Client{ControllerURL: controllerURL, SSLVerify: sslVerify, HTTPClient: httpClient} 63 64 tempClient, err := client.New() 65 66 if err == nil { 67 c.Token = tempClient.Token 68 } 69 70 err = auth.Register(c, username, password, email) 71 72 c.Token = "" 73 74 if err != nil { 75 fmt.Print("Registration failed: ") 76 return err 77 } 78 79 fmt.Printf("Registered %s\n", username) 80 return doLogin(c, username, password) 81 } 82 83 func doLogin(c *client.Client, username, password string) error { 84 token, err := auth.Login(c, username, password) 85 86 if err != nil { 87 return err 88 } 89 90 c.Token = token 91 c.Username = username 92 93 err = c.Save() 94 95 if err != nil { 96 return nil 97 } 98 99 fmt.Printf("Logged in as %s\n", username) 100 return nil 101 } 102 103 // Login to a Deis controller. 104 func Login(controller string, username string, password string, sslVerify bool) error { 105 u, err := url.Parse(controller) 106 107 if err != nil { 108 return err 109 } 110 111 controllerURL, err := chooseScheme(*u) 112 httpClient := client.CreateHTTPClient(sslVerify) 113 114 if err != nil { 115 return err 116 } 117 118 if err = client.CheckConnection(httpClient, controllerURL); err != nil { 119 return err 120 } 121 122 if username == "" { 123 fmt.Print("username: ") 124 fmt.Scanln(&username) 125 } 126 127 if password == "" { 128 fmt.Print("password: ") 129 password, err = readPassword() 130 fmt.Println() 131 132 if err != nil { 133 return err 134 } 135 } 136 137 c := &client.Client{ControllerURL: controllerURL, SSLVerify: sslVerify, HTTPClient: httpClient} 138 139 return doLogin(c, username, password) 140 } 141 142 // Logout from a Deis controller. 143 func Logout() error { 144 if err := client.Delete(); err != nil { 145 return err 146 } 147 148 fmt.Println("Logged out") 149 return nil 150 } 151 152 // Passwd changes a user's password. 153 func Passwd(username string, password string, newPassword string) error { 154 c, err := client.New() 155 156 if err != nil { 157 return err 158 } 159 160 if password == "" && username == "" { 161 fmt.Print("current password: ") 162 password, err = readPassword() 163 fmt.Println() 164 165 if err != nil { 166 return err 167 } 168 } 169 170 if newPassword == "" { 171 fmt.Print("new password: ") 172 newPassword, err = readPassword() 173 fmt.Printf("\nnew password (confirm): ") 174 passwordConfirm, err := readPassword() 175 176 fmt.Println() 177 178 if err != nil { 179 return err 180 } 181 182 if newPassword != passwordConfirm { 183 return errors.New("Password mismatch, not changing.") 184 } 185 } 186 187 err = auth.Passwd(c, username, password, newPassword) 188 189 if err != nil { 190 fmt.Print("Password change failed: ") 191 return err 192 } 193 194 fmt.Println("Password change succeeded.") 195 return nil 196 } 197 198 // Cancel deletes a user's account. 199 func Cancel(username string, password string, yes bool) error { 200 c, err := client.New() 201 202 if err != nil { 203 return err 204 } 205 206 if username == "" || password != "" { 207 fmt.Println("Please log in again in order to cancel this account") 208 209 if err = Login(c.ControllerURL.String(), username, password, c.SSLVerify); err != nil { 210 return err 211 } 212 } 213 214 if yes == false { 215 confirm := "" 216 217 c, err = client.New() 218 219 if err != nil { 220 return err 221 } 222 223 deletedUser := username 224 225 if deletedUser == "" { 226 deletedUser = c.Username 227 } 228 229 fmt.Printf("cancel account %s at %s? (y/N): ", deletedUser, c.ControllerURL.String()) 230 fmt.Scanln(&confirm) 231 232 if strings.ToLower(confirm) == "y" { 233 yes = true 234 } 235 } 236 237 if yes == false { 238 fmt.Println("Account not changed") 239 return nil 240 } 241 242 err = auth.Delete(c, username) 243 244 if err != nil { 245 return err 246 } 247 248 // If user targets themselves, logout. 249 if username != "" || c.Username == username { 250 if err := client.Delete(); err != nil { 251 return err 252 } 253 } 254 255 fmt.Println("Account cancelled") 256 return nil 257 } 258 259 // Whoami prints the logged in user. 260 func Whoami() error { 261 c, err := client.New() 262 263 if err != nil { 264 return err 265 } 266 267 fmt.Printf("You are %s at %s\n", c.Username, c.ControllerURL.String()) 268 return nil 269 } 270 271 // Regenerate regenenerates a user's token. 272 func Regenerate(username string, all bool) error { 273 c, err := client.New() 274 275 if err != nil { 276 return err 277 } 278 279 token, err := auth.Regenerate(c, username, all) 280 281 if err != nil { 282 return err 283 } 284 285 if username == "" && all == false { 286 c.Token = token 287 288 err = c.Save() 289 290 if err != nil { 291 return err 292 } 293 } 294 295 fmt.Println("Token Regenerated") 296 return nil 297 } 298 299 func readPassword() (string, error) { 300 password, err := terminal.ReadPassword(int(syscall.Stdin)) 301 302 return string(password), err 303 } 304 305 func chooseScheme(u url.URL) (url.URL, error) { 306 if u.Scheme == "" { 307 u.Scheme = "http" 308 u, err := url.Parse(u.String()) 309 310 if err != nil { 311 return url.URL{}, err 312 } 313 314 return *u, nil 315 } 316 317 return u, nil 318 }