This repository has been archived by the owner on Mar 24, 2022. It is now read-only.
forked from cloudfoundry/bosh-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uaa_login_strategy.go
132 lines (102 loc) · 2.61 KB
/
uaa_login_strategy.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package cmd
import (
boshlog "github.com/cloudfoundry/bosh-utils/logger"
cmdconf "github.com/cloudfoundry/bosh-cli/cmd/config"
boshuaa "github.com/cloudfoundry/bosh-cli/uaa"
boshui "github.com/cloudfoundry/bosh-cli/ui"
)
type UAALoginStrategy struct {
sessionFactory func(cmdconf.Config) Session
config cmdconf.Config
ui boshui.UI
logTag string
logger boshlog.Logger
successMsg string
failureMsg string
}
func NewUAALoginStrategy(
sessionFactory func(cmdconf.Config) Session,
config cmdconf.Config,
ui boshui.UI,
logger boshlog.Logger,
) UAALoginStrategy {
return UAALoginStrategy{
sessionFactory: sessionFactory,
config: config,
ui: ui,
logTag: "UAALoginStrategy",
logger: logger,
successMsg: "Successfully authenticated with UAA",
failureMsg: "Failed to authenticate with UAA",
}
}
func (c UAALoginStrategy) Try() error {
sess := c.sessionFactory(c.config)
uaa, err := sess.UAA()
if err != nil {
return err
}
if sess.Credentials().IsUAAClient() {
return c.tryClient(uaa) // Only try once
} else {
return c.tryUser(sess, uaa)
}
}
func (c UAALoginStrategy) tryClient(uaa boshuaa.UAA) error {
_, err := uaa.ClientCredentialsGrant()
if err == nil {
c.ui.PrintLinef(c.successMsg)
} else {
c.ui.ErrorLinef(c.failureMsg)
}
// Dont bother saving client token since there is no way to refresh it
return err
}
func (c UAALoginStrategy) tryUser(sess Session, uaa boshuaa.UAA) error {
prompts, err := uaa.Prompts()
if err != nil {
return err
}
for {
authed, err := c.tryUserOnce(sess.Environment(), prompts, uaa)
if err != nil {
return err
}
if authed {
return nil
}
}
}
func (c UAALoginStrategy) tryUserOnce(environment string, prompts []boshuaa.Prompt, uaa boshuaa.UAA) (bool, error) {
var answers []boshuaa.PromptAnswer
for _, prompt := range prompts {
var askFunc func(string) (string, error)
if prompt.IsPassword() {
askFunc = c.ui.AskForPassword
} else {
askFunc = c.ui.AskForText
}
value, err := askFunc(prompt.Label)
if err != nil {
return false, err
}
answer := boshuaa.PromptAnswer{Key: prompt.Key, Value: value}
answers = append(answers, answer)
}
accessToken, err := uaa.OwnerPasswordCredentialsGrant(answers)
if err != nil {
c.logger.Error(c.logTag, "Failed to get access token: %s", err)
c.ui.ErrorLinef(c.failureMsg)
return false, nil
}
creds := cmdconf.Creds{
RefreshToken: accessToken.RefreshToken().Value(),
}
updatedConfig := c.config.SetCredentials(environment, creds)
err = updatedConfig.Save()
if err != nil {
return false, err
}
c.ui.PrintLinef(c.successMsg)
return true, nil
}