github.com/sleungcy/cli@v7.1.0+incompatible/command/v7/auth_command.go (about)

     1  package v7
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"code.cloudfoundry.org/cli/api/uaa/constant"
     7  	"code.cloudfoundry.org/cli/api/uaa/uaaversion"
     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/v7/shared"
    12  )
    13  
    14  type AuthCommand struct {
    15  	BaseCommand
    16  
    17  	RequiredArgs      flag.Authentication `positional-args:"yes"`
    18  	ClientCredentials bool                `long:"client-credentials" description:"Use (non-user) service account (also called client credentials)"`
    19  	Origin            string              `long:"origin" description:"Indicates the identity provider to be used for authentication"`
    20  	usage             interface{}         `usage:"CF_NAME auth USERNAME PASSWORD\n   CF_NAME auth USERNAME PASSWORD --origin ORIGIN\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)"`
    21  	relatedCommands   interface{}         `related_commands:"api, login, target"`
    22  }
    23  
    24  func (cmd AuthCommand) Execute(args []string) error {
    25  	if len(cmd.Origin) > 0 {
    26  		err := command.MinimumUAAAPIVersionCheck(cmd.Actor.UAAAPIVersion(), uaaversion.MinUAAClientVersion, "Option '--origin'")
    27  		if err != nil {
    28  			return err
    29  		}
    30  	}
    31  
    32  	if cmd.ClientCredentials && cmd.Origin != "" {
    33  		return translatableerror.ArgumentCombinationError{
    34  			Args: []string{"--client-credentials", "--origin"},
    35  		}
    36  	}
    37  
    38  	username, password, err := cmd.getUsernamePassword()
    39  	if err != nil {
    40  		return err
    41  	}
    42  
    43  	cmd.UI.DisplayTextWithFlavor(
    44  		"API endpoint: {{.Endpoint}}",
    45  		map[string]interface{}{
    46  			"Endpoint": cmd.Config.Target(),
    47  		})
    48  
    49  	versionWarning, err := shared.CheckCCAPIVersion(cmd.Config.APIVersion())
    50  	if err != nil {
    51  		cmd.UI.DisplayWarning("Warning: unable to determine whether targeted API's version meets minimum supported.")
    52  	}
    53  	if versionWarning != "" {
    54  		cmd.UI.DisplayWarning(versionWarning)
    55  	}
    56  
    57  	if !cmd.ClientCredentials {
    58  		if cmd.Config.UAAGrantType() == string(constant.GrantTypeClientCredentials) {
    59  			return translatableerror.PasswordGrantTypeLogoutRequiredError{}
    60  		} else if cmd.Config.UAAOAuthClient() != "cf" || cmd.Config.UAAOAuthClientSecret() != "" {
    61  			return translatableerror.ManualClientCredentialsError{}
    62  		}
    63  	}
    64  
    65  	cmd.UI.DisplayNewline()
    66  
    67  	cmd.UI.DisplayText("Authenticating...")
    68  
    69  	credentials := make(map[string]string)
    70  	grantType := constant.GrantTypePassword
    71  	if cmd.ClientCredentials {
    72  		grantType = constant.GrantTypeClientCredentials
    73  		credentials["client_id"] = username
    74  		credentials["client_secret"] = password
    75  	} else {
    76  		credentials = map[string]string{
    77  			"username": username,
    78  			"password": password,
    79  		}
    80  	}
    81  
    82  	err = cmd.Actor.Authenticate(credentials, cmd.Origin, grantType)
    83  	if err != nil {
    84  		return err
    85  	}
    86  
    87  	cmd.UI.DisplayOK()
    88  	cmd.UI.DisplayTextWithFlavor(
    89  		"Use '{{.Command}}' to view or set your target org and space.",
    90  		map[string]interface{}{
    91  			"Command": fmt.Sprintf("%s target", cmd.Config.BinaryName()),
    92  		})
    93  
    94  	return nil
    95  }
    96  
    97  func (cmd AuthCommand) getUsernamePassword() (string, string, error) {
    98  	var (
    99  		userMissing     bool
   100  		passwordMissing bool
   101  	)
   102  
   103  	username := cmd.RequiredArgs.Username
   104  	if username == "" {
   105  		if envUser := cmd.Config.CFUsername(); envUser != "" {
   106  			username = envUser
   107  		} else {
   108  			userMissing = true
   109  		}
   110  	}
   111  
   112  	password := cmd.RequiredArgs.Password
   113  	if password == "" {
   114  		if envPassword := cmd.Config.CFPassword(); envPassword != "" {
   115  			password = envPassword
   116  		} else {
   117  			passwordMissing = true
   118  		}
   119  	}
   120  
   121  	if userMissing || passwordMissing {
   122  		return "", "", translatableerror.MissingCredentialsError{
   123  			MissingUsername: userMissing,
   124  			MissingPassword: passwordMissing,
   125  		}
   126  	}
   127  
   128  	return username, password, nil
   129  }