forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
65 lines (53 loc) · 1.8 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
package v2action
import (
"fmt"
"code.cloudfoundry.org/cli/api/uaa/constant"
"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
)
// Authenticate authenticates the user in UAA and sets the returned tokens in
// the config.
//
// It unsets the currently targeted org and space whether authentication
// succeeds or not.
func (actor Actor) Authenticate(ID string, secret string, origin string, grantType constant.GrantType) error {
actor.Config.UnsetOrganizationAndSpaceInformation()
credentials := make(map[string]string)
if grantType == constant.GrantTypePassword {
credentials["username"] = ID
credentials["password"] = secret
} else if grantType == constant.GrantTypeClientCredentials {
credentials["client_id"] = ID
credentials["client_secret"] = secret
}
accessToken, refreshToken, err := actor.UAAClient.Authenticate(credentials, origin, grantType)
if err != nil {
actor.Config.SetTokenInformation("", "", "")
return err
}
accessToken = fmt.Sprintf("bearer %s", accessToken)
actor.Config.SetTokenInformation(accessToken, refreshToken, "")
if grantType == constant.GrantTypePassword {
actor.Config.SetUAAGrantType("")
} else {
actor.Config.SetUAAGrantType(string(grantType))
}
if grantType == constant.GrantTypeClientCredentials {
actor.Config.SetUAAClientCredentials(ID, "")
}
return nil
}
func (actor Actor) GetLoginPrompts() map[string]coreconfig.AuthPrompt {
rawPrompts := actor.UAAClient.LoginPrompts()
prompts := make(map[string]coreconfig.AuthPrompt)
for key, val := range rawPrompts {
prompts[key] = coreconfig.AuthPrompt{
Type: knownAuthPromptTypes[val[0]],
DisplayName: val[1],
}
}
return prompts
}
var knownAuthPromptTypes = map[string]coreconfig.AuthPromptType{
"text": coreconfig.AuthPromptTypeText,
"password": coreconfig.AuthPromptTypePassword,
}