github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+incompatible/cf/commands/auth.go (about) 1 package commands 2 3 import ( 4 "github.com/cloudfoundry/cli/cf" 5 "github.com/cloudfoundry/cli/cf/api/authentication" 6 "github.com/cloudfoundry/cli/cf/command_registry" 7 "github.com/cloudfoundry/cli/cf/configuration/core_config" 8 . "github.com/cloudfoundry/cli/cf/i18n" 9 "github.com/cloudfoundry/cli/cf/requirements" 10 "github.com/cloudfoundry/cli/cf/terminal" 11 "github.com/cloudfoundry/cli/flags" 12 ) 13 14 type Authenticate struct { 15 ui terminal.UI 16 config core_config.ReadWriter 17 authenticator authentication.AuthenticationRepository 18 } 19 20 func init() { 21 command_registry.Register(&Authenticate{}) 22 } 23 24 func (cmd *Authenticate) MetaData() command_registry.CommandMetadata { 25 return command_registry.CommandMetadata{ 26 Name: "auth", 27 Description: T("Authenticate user non-interactively"), 28 Usage: T("CF_NAME auth USERNAME PASSWORD\n\n") + 29 terminal.WarningColor(T("WARNING:\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\n")) + T("EXAMPLE:\n") + T(" CF_NAME auth name@example.com \"my password\" (use quotes for passwords with a space)\n") + T(" CF_NAME auth name@example.com \"\\\"password\\\"\" (escape quotes if used in password)"), 30 } 31 } 32 33 func (cmd *Authenticate) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) (reqs []requirements.Requirement, err error) { 34 if len(fc.Args()) != 2 { 35 cmd.ui.Failed(T("Incorrect Usage. Requires 'username password' as arguments\n\n") + command_registry.Commands.CommandUsage("auth")) 36 } 37 38 reqs = append(reqs, requirementsFactory.NewApiEndpointRequirement()) 39 return 40 } 41 42 func (cmd *Authenticate) SetDependency(deps command_registry.Dependency, pluginCall bool) command_registry.Command { 43 cmd.ui = deps.Ui 44 cmd.config = deps.Config 45 cmd.authenticator = deps.RepoLocator.GetAuthenticationRepository() 46 return cmd 47 } 48 49 func (cmd *Authenticate) Execute(c flags.FlagContext) { 50 cmd.config.ClearSession() 51 cmd.authenticator.GetLoginPromptsAndSaveUAAServerURL() 52 53 cmd.ui.Say(T("API endpoint: {{.ApiEndpoint}}", 54 map[string]interface{}{"ApiEndpoint": terminal.EntityNameColor(cmd.config.ApiEndpoint())})) 55 cmd.ui.Say(T("Authenticating...")) 56 57 apiErr := cmd.authenticator.Authenticate(map[string]string{"username": c.Args()[0], "password": c.Args()[1]}) 58 if apiErr != nil { 59 cmd.ui.Failed(apiErr.Error()) 60 return 61 } 62 63 cmd.ui.Ok() 64 cmd.ui.Say(T("Use '{{.Name}}' to view or set your target org and space", 65 map[string]interface{}{"Name": terminal.CommandColor(cf.Name() + " target")})) 66 67 cmd.ui.NotifyUpdateIfNeeded(cmd.config) 68 69 return 70 }