github.com/ablease/cli@v6.37.1-0.20180613014814-3adbb7d7fb19+incompatible/command/v2/auth_command.go (about)

     1  package v2
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"code.cloudfoundry.org/cli/actor/v2action"
     7  	"code.cloudfoundry.org/cli/api/uaa/constant"
     8  	"code.cloudfoundry.org/cli/command"
     9  	"code.cloudfoundry.org/cli/command/flag"
    10  	"code.cloudfoundry.org/cli/command/translatableerror"
    11  	"code.cloudfoundry.org/cli/command/v2/shared"
    12  )
    13  
    14  //go:generate counterfeiter . AuthActor
    15  
    16  type AuthActor interface {
    17  	Authenticate(ID string, secret string, grantType constant.GrantType) error
    18  }
    19  
    20  type AuthCommand struct {
    21  	RequiredArgs      flag.Authentication `positional-args:"yes"`
    22  	ClientCredentials bool                `long:"client-credentials" description:"Use (non-user) service account (also called client credentials)"`
    23  	usage             interface{}         `usage:"CF_NAME auth USERNAME PASSWORD\n   CF_NAME auth CLIENT_ID CLIENT_SECRET --client-credentials\n\nENVIRONMENT VARIABLES:\n   CF_USERNAME=user		Authenticating user. Overridden if USERNAME argument is provided.\n   CF_PASSWORD=password		Password associated with user. Overriden if PASSWORD argument is provided.\n\nWARNING:\n   Providing your password as a command line option is highly discouraged\n   Your password may be visible to others and may be recorded in your shell history\n   Consider using the CF_PASSWORD environment variable instead\n\nEXAMPLES:\n   CF_NAME auth name@example.com \"my password\" (use quotes for passwords with a space)\n   CF_NAME auth name@example.com \"\\\"password\\\"\" (escape quotes if used in password)"`
    24  	relatedCommands   interface{}         `related_commands:"api, login, target"`
    25  
    26  	UI     command.UI
    27  	Config command.Config
    28  	Actor  AuthActor
    29  }
    30  
    31  func (cmd *AuthCommand) Setup(config command.Config, ui command.UI) error {
    32  	cmd.UI = ui
    33  	cmd.Config = config
    34  
    35  	ccClient, uaaClient, err := shared.NewClients(config, ui, true)
    36  	if err != nil {
    37  		return err
    38  	}
    39  	cmd.Actor = v2action.NewActor(ccClient, uaaClient, config)
    40  
    41  	return nil
    42  }
    43  
    44  func (cmd AuthCommand) Execute(args []string) error {
    45  	username, password, err := cmd.checkEnvVariables()
    46  	if err != nil {
    47  		return err
    48  	}
    49  
    50  	err = command.WarnCLIVersionCheck(cmd.Config, cmd.UI)
    51  	if err != nil {
    52  		return err
    53  	}
    54  
    55  	cmd.UI.DisplayTextWithFlavor(
    56  		"API endpoint: {{.Endpoint}}",
    57  		map[string]interface{}{
    58  			"Endpoint": cmd.Config.Target(),
    59  		})
    60  	cmd.UI.DisplayText("Authenticating...")
    61  
    62  	grantType := constant.GrantTypePassword
    63  	if cmd.ClientCredentials {
    64  		grantType = constant.GrantTypeClientCredentials
    65  	}
    66  
    67  	err = cmd.Actor.Authenticate(username, password, grantType)
    68  	if err != nil {
    69  		return err
    70  	}
    71  
    72  	cmd.UI.DisplayOK()
    73  	cmd.UI.DisplayTextWithFlavor(
    74  		"Use '{{.Command}}' to view or set your target org and space.",
    75  		map[string]interface{}{
    76  			"Command": fmt.Sprintf("%s target", cmd.Config.BinaryName()),
    77  		})
    78  
    79  	return nil
    80  }
    81  
    82  func (cmd AuthCommand) checkEnvVariables() (string, string, error) {
    83  	var (
    84  		userMissing     bool
    85  		passwordMissing bool
    86  	)
    87  
    88  	username := cmd.RequiredArgs.Username
    89  	if username == "" {
    90  		if envUser := cmd.Config.CFUsername(); envUser != "" {
    91  			username = envUser
    92  		} else {
    93  			userMissing = true
    94  		}
    95  	}
    96  
    97  	password := cmd.RequiredArgs.Password
    98  	if password == "" {
    99  		if envPassword := cmd.Config.CFPassword(); envPassword != "" {
   100  			password = envPassword
   101  		} else {
   102  			passwordMissing = true
   103  		}
   104  	}
   105  
   106  	if userMissing || passwordMissing {
   107  		return "", "", translatableerror.MissingCredentialsError{
   108  			MissingUsername: userMissing,
   109  			MissingPassword: passwordMissing,
   110  		}
   111  	}
   112  
   113  	return username, password, nil
   114  }