github.com/jenspinney/cli@v6.42.1-0.20190207184520-7450c600020e+incompatible/actor/v2action/auth.go (about)

     1  package v2action
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"code.cloudfoundry.org/cli/actor/actionerror"
     7  	"code.cloudfoundry.org/cli/api/uaa/constant"
     8  )
     9  
    10  // Authenticate authenticates the user in UAA and sets the returned tokens in
    11  // the config.
    12  //
    13  // It unsets the currently targeted org and space whether authentication
    14  // succeeds or not.
    15  func (actor Actor) Authenticate(ID string, secret string, origin string, grantType constant.GrantType) error {
    16  	if grantType == constant.GrantTypePassword && actor.Config.UAAGrantType() == string(constant.GrantTypeClientCredentials) {
    17  		return actionerror.PasswordGrantTypeLogoutRequiredError{}
    18  	}
    19  
    20  	actor.Config.UnsetOrganizationAndSpaceInformation()
    21  
    22  	accessToken, refreshToken, err := actor.UAAClient.Authenticate(ID, secret, origin, grantType)
    23  	if err != nil {
    24  		actor.Config.SetTokenInformation("", "", "")
    25  		return err
    26  	}
    27  
    28  	accessToken = fmt.Sprintf("bearer %s", accessToken)
    29  	actor.Config.SetTokenInformation(accessToken, refreshToken, "")
    30  
    31  	if grantType != constant.GrantTypePassword {
    32  		actor.Config.SetUAAGrantType(string(grantType))
    33  		actor.Config.SetUAAClientCredentials(ID, secret)
    34  	}
    35  
    36  	return nil
    37  }