-
Notifications
You must be signed in to change notification settings - Fork 926
/
auth.go
37 lines (29 loc) · 1.11 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
package v2action
import (
"fmt"
"code.cloudfoundry.org/cli/actor/actionerror"
"code.cloudfoundry.org/cli/api/uaa/constant"
)
// 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 {
if grantType == constant.GrantTypePassword && actor.Config.UAAGrantType() == string(constant.GrantTypeClientCredentials) {
return actionerror.PasswordGrantTypeLogoutRequiredError{}
}
actor.Config.UnsetOrganizationAndSpaceInformation()
accessToken, refreshToken, err := actor.UAAClient.Authenticate(ID, secret, 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(string(grantType))
actor.Config.SetUAAClientCredentials(ID, secret)
}
return nil
}