github.com/arunkumar7540/cli@v6.45.0+incompatible/command/v6/auth_command.go (about) 1 package v6 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/api/uaa/uaaversion" 9 "code.cloudfoundry.org/cli/command" 10 "code.cloudfoundry.org/cli/command/flag" 11 "code.cloudfoundry.org/cli/command/translatableerror" 12 "code.cloudfoundry.org/cli/command/v6/shared" 13 ) 14 15 //go:generate counterfeiter . AuthActor 16 17 type AuthActor interface { 18 Authenticate(ID string, secret string, origin string, grantType constant.GrantType) error 19 CloudControllerAPIVersion() string 20 UAAAPIVersion() string 21 } 22 23 type AuthCommand struct { 24 RequiredArgs flag.Authentication `positional-args:"yes"` 25 ClientCredentials bool `long:"client-credentials" description:"Use (non-user) service account (also called client credentials)"` 26 Origin string `long:"origin" description:"Indicates the identity provider to be used for authentication"` 27 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)"` 28 relatedCommands interface{} `related_commands:"api, login, target"` 29 30 UI command.UI 31 Config command.Config 32 Actor AuthActor 33 } 34 35 func (cmd *AuthCommand) Setup(config command.Config, ui command.UI) error { 36 cmd.UI = ui 37 cmd.Config = config 38 39 ccClient, uaaClient, err := shared.NewClients(config, ui, true) 40 if err != nil { 41 return err 42 } 43 cmd.Actor = v2action.NewActor(ccClient, uaaClient, config) 44 45 return nil 46 } 47 48 func (cmd AuthCommand) Execute(args []string) error { 49 if len(cmd.Origin) > 0 { 50 err := command.MinimumUAAAPIVersionCheck(cmd.Actor.UAAAPIVersion(), uaaversion.MinVersionOrigin, "Option '--origin'") 51 if err != nil { 52 return err 53 } 54 } 55 56 if cmd.ClientCredentials && cmd.Origin != "" { 57 return translatableerror.ArgumentCombinationError{ 58 Args: []string{"--client-credentials", "--origin"}, 59 } 60 } 61 62 username, password, err := cmd.getUsernamePassword() 63 if err != nil { 64 return err 65 } 66 67 err = command.WarnIfCLIVersionBelowAPIDefinedMinimum(cmd.Config, cmd.Actor.CloudControllerAPIVersion(), cmd.UI) 68 if err != nil { 69 return err 70 } 71 72 cmd.UI.DisplayTextWithFlavor( 73 "API endpoint: {{.Endpoint}}", 74 map[string]interface{}{ 75 "Endpoint": cmd.Config.Target(), 76 }) 77 cmd.UI.DisplayText("Authenticating...") 78 79 grantType := constant.GrantTypePassword 80 if cmd.ClientCredentials { 81 grantType = constant.GrantTypeClientCredentials 82 } 83 84 err = cmd.Actor.Authenticate(username, password, cmd.Origin, grantType) 85 if err != nil { 86 return err 87 } 88 89 cmd.UI.DisplayOK() 90 cmd.UI.DisplayTextWithFlavor( 91 "Use '{{.Command}}' to view or set your target org and space.", 92 map[string]interface{}{ 93 "Command": fmt.Sprintf("%s target", cmd.Config.BinaryName()), 94 }) 95 96 return nil 97 } 98 99 func (cmd AuthCommand) getUsernamePassword() (string, string, error) { 100 var ( 101 userMissing bool 102 passwordMissing bool 103 ) 104 105 username := cmd.RequiredArgs.Username 106 if username == "" { 107 if envUser := cmd.Config.CFUsername(); envUser != "" { 108 username = envUser 109 } else { 110 userMissing = true 111 } 112 } 113 114 password := cmd.RequiredArgs.Password 115 if password == "" { 116 if envPassword := cmd.Config.CFPassword(); envPassword != "" { 117 password = envPassword 118 } else { 119 passwordMissing = true 120 } 121 } 122 123 if userMissing || passwordMissing { 124 return "", "", translatableerror.MissingCredentialsError{ 125 MissingUsername: userMissing, 126 MissingPassword: passwordMissing, 127 } 128 } 129 130 return username, password, nil 131 }