github.com/naphatkrit/deis@v1.12.3/client/cmd/auth.go (about)

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