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

     1  package parser
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/deis/deis/client/cmd"
     7  	docopt "github.com/docopt/docopt-go"
     8  )
     9  
    10  // Auth routes auth commands to the specific function.
    11  func Auth(argv []string) error {
    12  	usage := `
    13  Valid commands for auth:
    14  
    15  auth:register          register a new user
    16  auth:login             authenticate against a controller
    17  auth:logout            clear the current user session
    18  auth:passwd            change the password for the current user
    19  auth:whoami            display the current user
    20  auth:cancel            remove the current user account
    21  auth:regenerate        regenerate user tokens
    22  
    23  Use 'deis help [command]' to learn more.
    24  `
    25  
    26  	switch argv[0] {
    27  	case "auth:register":
    28  		return authRegister(argv)
    29  	case "auth:login":
    30  		return authLogin(argv)
    31  	case "auth:logout":
    32  		return authLogout(argv)
    33  	case "auth:passwd":
    34  		return authPasswd(argv)
    35  	case "auth:whoami":
    36  		return authWhoami(argv)
    37  	case "auth:cancel":
    38  		return authCancel(argv)
    39  	case "auth:regenerate":
    40  		return authRegenerate(argv)
    41  	case "auth":
    42  		fmt.Print(usage)
    43  		return nil
    44  	default:
    45  		PrintUsage()
    46  		return nil
    47  	}
    48  }
    49  
    50  func authRegister(argv []string) error {
    51  	usage := `
    52  Registers a new user with a Deis controller.
    53  
    54  Usage: deis auth:register <controller> [options]
    55  
    56  Arguments:
    57    <controller>
    58      fully-qualified controller URI, e.g. 'http://deis.local3.deisapp.com/'
    59  
    60  Options:
    61    --username=<username>
    62      provide a username for the new account.
    63    --password=<password>
    64      provide a password for the new account.
    65    --email=<email>
    66      provide an email address.
    67    --ssl-verify=false
    68      disables SSL certificate verification for API requests
    69  `
    70  	args, err := docopt.Parse(usage, argv, true, "", false, true)
    71  
    72  	if err != nil {
    73  		return err
    74  	}
    75  
    76  	controller := safeGetValue(args, "<controller>")
    77  	username := safeGetValue(args, "--username")
    78  	password := safeGetValue(args, "--password")
    79  	email := safeGetValue(args, "--email")
    80  	sslVerify := false
    81  
    82  	if args["--ssl-verify"] != nil && args["--ssl-verify"].(string) == "true" {
    83  		sslVerify = true
    84  	}
    85  
    86  	return cmd.Register(controller, username, password, email, sslVerify)
    87  }
    88  
    89  func authLogin(argv []string) error {
    90  	usage := `
    91  Logs in by authenticating against a controller.
    92  
    93  Usage: deis auth:login <controller> [options]
    94  
    95  Arguments:
    96    <controller>
    97      a fully-qualified controller URI, e.g. "http://deis.local3.deisapp.com/".
    98  
    99  Options:
   100    --username=<username>
   101      provide a username for the account.
   102    --password=<password>
   103      provide a password for the account.
   104    --ssl-verify=false
   105      disables SSL certificate verification for API requests
   106  `
   107  
   108  	args, err := docopt.Parse(usage, argv, true, "", false, true)
   109  
   110  	if err != nil {
   111  		return err
   112  	}
   113  
   114  	controller := safeGetValue(args, "<controller>")
   115  	username := safeGetValue(args, "--username")
   116  	password := safeGetValue(args, "--password")
   117  	sslVerify := false
   118  
   119  	if args["--ssl-verify"] != nil && args["--ssl-verify"].(string) == "true" {
   120  		sslVerify = true
   121  	}
   122  
   123  	return cmd.Login(controller, username, password, sslVerify)
   124  }
   125  
   126  func authLogout(argv []string) error {
   127  	usage := `
   128  Logs out from a controller and clears the user session.
   129  
   130  Usage: deis auth:logout
   131  `
   132  
   133  	if _, err := docopt.Parse(usage, argv, true, "", false, true); err != nil {
   134  		return err
   135  	}
   136  
   137  	return cmd.Logout()
   138  }
   139  
   140  func authPasswd(argv []string) error {
   141  	usage := `
   142  Changes the password for the current user.
   143  
   144  Usage: deis auth:passwd [options]
   145  
   146  Options:
   147    --password=<password>
   148      the current password for the account.
   149    --new-password=<new-password>
   150      the new password for the account.
   151    --username=<username>
   152      the account's username.
   153  `
   154  
   155  	args, err := docopt.Parse(usage, argv, true, "", false, true)
   156  
   157  	if err != nil {
   158  		return err
   159  	}
   160  
   161  	username := safeGetValue(args, "--username")
   162  	password := safeGetValue(args, "--password")
   163  	newPassword := safeGetValue(args, "--new-password")
   164  
   165  	return cmd.Passwd(username, password, newPassword)
   166  }
   167  
   168  func authWhoami(argv []string) error {
   169  	usage := `
   170  Displays the currently logged in user.
   171  
   172  Usage: deis auth:whoami
   173  `
   174  
   175  	if _, err := docopt.Parse(usage, argv, true, "", false, true); err != nil {
   176  		return err
   177  	}
   178  
   179  	return cmd.Whoami()
   180  }
   181  
   182  func authCancel(argv []string) error {
   183  	usage := `
   184  Cancels and removes the current account.
   185  
   186  Usage: deis auth:cancel [options]
   187  
   188  Options:
   189    --username=<username>
   190      provide a username for the account.
   191    --password=<password>
   192      provide a password for the account.
   193    --yes
   194      force "yes" when prompted.
   195  `
   196  
   197  	args, err := docopt.Parse(usage, argv, true, "", false, true)
   198  
   199  	if err != nil {
   200  		return err
   201  	}
   202  
   203  	username := safeGetValue(args, "--username")
   204  	password := safeGetValue(args, "--password")
   205  	yes := args["--yes"].(bool)
   206  
   207  	return cmd.Cancel(username, password, yes)
   208  }
   209  
   210  func authRegenerate(argv []string) error {
   211  	usage := `
   212  Regenerates auth token, defaults to regenerating token for the current user.
   213  
   214  Usage: deis auth:regenerate [options]
   215  
   216  Options:
   217    -u --username=<username>
   218      specify user to regenerate. Requires admin privilages.
   219    --all
   220      regenerate token for every user. Requires admin privilages.
   221  `
   222  
   223  	args, err := docopt.Parse(usage, argv, true, "", false, true)
   224  
   225  	if err != nil {
   226  		return err
   227  	}
   228  
   229  	username := safeGetValue(args, "--username")
   230  	all := args["--all"].(bool)
   231  
   232  	return cmd.Regenerate(username, all)
   233  }