forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
54 lines (44 loc) · 1.59 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
package v7action
import (
"fmt"
"code.cloudfoundry.org/cli/actor/actionerror"
"code.cloudfoundry.org/cli/api/uaa/constant"
"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
)
func (actor Actor) Authenticate(credentials map[string]string, origin string, grantType constant.GrantType) error {
if grantType == constant.GrantTypePassword && actor.Config.UAAGrantType() == string(constant.GrantTypeClientCredentials) {
return actionerror.PasswordGrantTypeLogoutRequiredError{}
}
actor.Config.UnsetOrganizationAndSpaceInformation()
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(credentials["client_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,
}