github.com/deis/deis@v1.13.5-0.20170519182049-1d9e59fbdbfc/client/cmd/auth.go (about)

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