github.com/engineyard/workflow-cli@v2.21.6+incompatible/parser/auth.go (about)

     1  package parser
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/teamhephy/workflow-cli/cmd"
     7  	docopt "github.com/docopt/docopt-go"
     8  )
     9  
    10  // Auth routes auth commands to the specific function.
    11  func Auth(argv []string, cmdr cmd.Commander) 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, cmdr)
    29  	case "auth:login":
    30  		return authLogin(argv, cmdr)
    31  	case "auth:logout":
    32  		return authLogout(argv, cmdr)
    33  	case "auth:passwd":
    34  		return authPasswd(argv, cmdr)
    35  	case "auth:whoami":
    36  		return authWhoami(argv, cmdr)
    37  	case "auth:cancel":
    38  		return authCancel(argv, cmdr)
    39  	case "auth:regenerate":
    40  		return authRegenerate(argv, cmdr)
    41  	case "auth":
    42  		fmt.Print(usage)
    43  		return nil
    44  	default:
    45  		PrintUsage(cmdr)
    46  		return nil
    47  	}
    48  }
    49  
    50  func authRegister(argv []string, cmdr cmd.Commander) 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    --login=true
    68      logs into the new account after registering.
    69    --ssl-verify=true
    70      enables/disables SSL certificate verification for API requests
    71  `
    72  
    73  	args, err := docopt.Parse(usage, argv, true, "", false, true)
    74  
    75  	if err != nil {
    76  		return err
    77  	}
    78  
    79  	controller := safeGetValue(args, "<controller>")
    80  	username := safeGetValue(args, "--username")
    81  	password := safeGetValue(args, "--password")
    82  	email := safeGetValue(args, "--email")
    83  	sslVerify := true
    84  	login := true
    85  
    86  	if args["--ssl-verify"] != nil && args["--ssl-verify"].(string) == "false" {
    87  		sslVerify = false
    88  	}
    89  
    90  	// NOTE(bacongobbler): two use cases to check here:
    91  	//
    92  	// 1) Legacy; calling `deis auth:register` without --login
    93  	// 2) calling `deis auth:register --login false`
    94  	if args["--login"] != nil && args["--login"].(string) == "false" {
    95  		login = false
    96  	}
    97  
    98  	return cmdr.Register(controller, username, password, email, sslVerify, login)
    99  }
   100  
   101  func authLogin(argv []string, cmdr cmd.Commander) error {
   102  	usage := `
   103  Logs in by authenticating against a controller.
   104  
   105  Usage: deis auth:login <controller> [options]
   106  
   107  Arguments:
   108    <controller>
   109      a fully-qualified controller URI, e.g. "http://deis.local3.deisapp.com/".
   110  
   111  Options:
   112    --username=<username>
   113      provide a username for the account.
   114    --password=<password>
   115      provide a password for the account.
   116    --ssl-verify=true
   117      enables/disables SSL certificate verification for API requests
   118  `
   119  
   120  	args, err := docopt.Parse(usage, argv, true, "", false, true)
   121  
   122  	if err != nil {
   123  		return err
   124  	}
   125  
   126  	controller := safeGetValue(args, "<controller>")
   127  	username := safeGetValue(args, "--username")
   128  	password := safeGetValue(args, "--password")
   129  	sslVerify := true
   130  
   131  	if args["--ssl-verify"] != nil && args["--ssl-verify"].(string) == "false" {
   132  		sslVerify = false
   133  	}
   134  
   135  	return cmdr.Login(controller, username, password, sslVerify)
   136  }
   137  
   138  func authLogout(argv []string, cmdr cmd.Commander) error {
   139  	usage := `
   140  Logs out from a controller and clears the user session.
   141  
   142  Usage: deis auth:logout
   143  
   144  Options:
   145  `
   146  
   147  	if _, err := docopt.Parse(usage, argv, true, "", false, true); err != nil {
   148  		return err
   149  	}
   150  
   151  	return cmdr.Logout()
   152  }
   153  
   154  func authPasswd(argv []string, cmdr cmd.Commander) error {
   155  	usage := `
   156  Changes the password for the current user.
   157  
   158  Usage: deis auth:passwd [options]
   159  
   160  Options:
   161    --password=<password>
   162      the current password for the account.
   163    --new-password=<new-password>
   164      the new password for the account.
   165    --username=<username>
   166      the account's username.
   167  `
   168  
   169  	args, err := docopt.Parse(usage, argv, true, "", false, true)
   170  
   171  	if err != nil {
   172  		return err
   173  	}
   174  
   175  	username := safeGetValue(args, "--username")
   176  	password := safeGetValue(args, "--password")
   177  	newPassword := safeGetValue(args, "--new-password")
   178  
   179  	return cmdr.Passwd(username, password, newPassword)
   180  }
   181  
   182  func authWhoami(argv []string, cmdr cmd.Commander) error {
   183  	usage := `
   184  Displays the currently logged in user.
   185  
   186  Usage: deis auth:whoami [options]
   187  
   188  Options:
   189    --all
   190      fetch a more detailed description about the user.
   191  `
   192  
   193  	args, err := docopt.Parse(usage, argv, true, "", false, true)
   194  
   195  	if err != nil {
   196  		return err
   197  	}
   198  
   199  	return cmdr.Whoami(args["--all"].(bool))
   200  }
   201  
   202  func authCancel(argv []string, cmdr cmd.Commander) error {
   203  	usage := `
   204  Cancels and removes the current account.
   205  
   206  Usage: deis auth:cancel [options]
   207  
   208  Options:
   209    --username=<username>
   210      provide a username for the account.
   211    --password=<password>
   212      provide a password for the account.
   213    --yes
   214      force "yes" when prompted.
   215  `
   216  
   217  	args, err := docopt.Parse(usage, argv, true, "", false, true)
   218  
   219  	if err != nil {
   220  		return err
   221  	}
   222  
   223  	username := safeGetValue(args, "--username")
   224  	password := safeGetValue(args, "--password")
   225  	yes := args["--yes"].(bool)
   226  
   227  	return cmdr.Cancel(username, password, yes)
   228  }
   229  
   230  func authRegenerate(argv []string, cmdr cmd.Commander) error {
   231  	usage := `
   232  Regenerates auth token, defaults to regenerating token for the current user.
   233  
   234  Usage: deis auth:regenerate [options]
   235  
   236  Options:
   237    -u --username=<username>
   238      specify user to regenerate. Requires admin privileges.
   239    --all
   240      regenerate token for every user. Requires admin privileges.
   241  `
   242  
   243  	args, err := docopt.Parse(usage, argv, true, "", false, true)
   244  
   245  	if err != nil {
   246  		return err
   247  	}
   248  
   249  	username := safeGetValue(args, "--username")
   250  	all := args["--all"].(bool)
   251  
   252  	return cmdr.Regenerate(username, all)
   253  }