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