github.com/franc20/ayesa_sap@v7.0.0-beta.28.0.20200124003224-302d4d52fa6c+incompatible/command/v7/auth_command.go (about) 1 package v7 2 3 import ( 4 "fmt" 5 6 "code.cloudfoundry.org/cli/actor/v7action" 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/v7/shared" 13 "code.cloudfoundry.org/clock" 14 ) 15 16 //go:generate counterfeiter . AuthActor 17 18 type AuthActor interface { 19 Authenticate(credentials map[string]string, origin string, grantType constant.GrantType) error 20 CloudControllerAPIVersion() string 21 UAAAPIVersion() string 22 } 23 24 type AuthCommand struct { 25 RequiredArgs flag.Authentication `positional-args:"yes"` 26 ClientCredentials bool `long:"client-credentials" description:"Use (non-user) service account (also called client credentials)"` 27 Origin string `long:"origin" description:"Indicates the identity provider to be used for authentication"` 28 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)"` 29 relatedCommands interface{} `related_commands:"api, login, target"` 30 31 UI command.UI 32 Config command.Config 33 Actor AuthActor 34 } 35 36 func (cmd *AuthCommand) Setup(config command.Config, ui command.UI) error { 37 cmd.UI = ui 38 cmd.Config = config 39 40 ccClient, uaaClient, err := shared.GetNewClientsAndConnectToCF(config, ui, "") 41 if err != nil { 42 return err 43 } 44 cmd.Actor = v7action.NewActor(ccClient, config, nil, uaaClient, clock.NewClock()) 45 46 return nil 47 } 48 49 func (cmd AuthCommand) Execute(args []string) error { 50 if len(cmd.Origin) > 0 { 51 err := command.MinimumUAAAPIVersionCheck(cmd.Actor.UAAAPIVersion(), uaaversion.MinVersionOrigin, "Option '--origin'") 52 if err != nil { 53 return err 54 } 55 } 56 57 if cmd.ClientCredentials && cmd.Origin != "" { 58 return translatableerror.ArgumentCombinationError{ 59 Args: []string{"--client-credentials", "--origin"}, 60 } 61 } 62 63 username, password, err := cmd.getUsernamePassword() 64 if err != nil { 65 return err 66 } 67 68 if !cmd.ClientCredentials { 69 if cmd.Config.UAAGrantType() == string(constant.GrantTypeClientCredentials) { 70 return translatableerror.PasswordGrantTypeLogoutRequiredError{} 71 } else if cmd.Config.UAAOAuthClient() != "cf" || cmd.Config.UAAOAuthClientSecret() != "" { 72 cmd.UI.DisplayWarning("Deprecation warning: Manually writing your client credentials to the config.json is deprecated and will be removed in the future. For similar functionality, please use the `cf auth --client-credentials` command instead.") 73 } 74 } 75 76 err = command.WarnIfCLIVersionBelowAPIDefinedMinimum(cmd.Config, cmd.Actor.CloudControllerAPIVersion(), cmd.UI) 77 if err != nil { 78 return err 79 } 80 81 cmd.UI.DisplayTextWithFlavor( 82 "API endpoint: {{.Endpoint}}", 83 map[string]interface{}{ 84 "Endpoint": cmd.Config.Target(), 85 }) 86 cmd.UI.DisplayText("Authenticating...") 87 88 credentials := make(map[string]string) 89 grantType := constant.GrantTypePassword 90 if cmd.ClientCredentials { 91 grantType = constant.GrantTypeClientCredentials 92 credentials["client_id"] = username 93 credentials["client_secret"] = password 94 } else { 95 credentials = map[string]string{ 96 "username": username, 97 "password": password, 98 } 99 } 100 101 err = cmd.Actor.Authenticate(credentials, cmd.Origin, grantType) 102 if err != nil { 103 return err 104 } 105 106 cmd.UI.DisplayOK() 107 cmd.UI.DisplayTextWithFlavor( 108 "Use '{{.Command}}' to view or set your target org and space.", 109 map[string]interface{}{ 110 "Command": fmt.Sprintf("%s target", cmd.Config.BinaryName()), 111 }) 112 113 return nil 114 } 115 116 func (cmd AuthCommand) getUsernamePassword() (string, string, error) { 117 var ( 118 userMissing bool 119 passwordMissing bool 120 ) 121 122 username := cmd.RequiredArgs.Username 123 if username == "" { 124 if envUser := cmd.Config.CFUsername(); envUser != "" { 125 username = envUser 126 } else { 127 userMissing = true 128 } 129 } 130 131 password := cmd.RequiredArgs.Password 132 if password == "" { 133 if envPassword := cmd.Config.CFPassword(); envPassword != "" { 134 password = envPassword 135 } else { 136 passwordMissing = true 137 } 138 } 139 140 if userMissing || passwordMissing { 141 return "", "", translatableerror.MissingCredentialsError{ 142 MissingUsername: userMissing, 143 MissingPassword: passwordMissing, 144 } 145 } 146 147 return username, password, nil 148 }