github.com/swisscom/cloudfoundry-cli@v7.1.0+incompatible/cf/commands/auth.go (about)

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