github.com/cloudfoundry/cli@v7.1.0+incompatible/actor/v3action/auth.go (about)

     1  package v3action
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"code.cloudfoundry.org/cli/actor/actionerror"
     7  	"code.cloudfoundry.org/cli/api/uaa/constant"
     8  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
     9  )
    10  
    11  func (actor Actor) Authenticate(credentials map[string]string, origin string, grantType constant.GrantType) error {
    12  	if grantType == constant.GrantTypePassword && actor.Config.UAAGrantType() == string(constant.GrantTypeClientCredentials) {
    13  		return actionerror.PasswordGrantTypeLogoutRequiredError{}
    14  	}
    15  
    16  	actor.Config.UnsetOrganizationAndSpaceInformation()
    17  
    18  	accessToken, refreshToken, err := actor.UAAClient.Authenticate(credentials, origin, grantType)
    19  	if err != nil {
    20  		actor.Config.SetTokenInformation("", "", "")
    21  		return err
    22  	}
    23  
    24  	accessToken = fmt.Sprintf("bearer %s", accessToken)
    25  	actor.Config.SetTokenInformation(accessToken, refreshToken, "")
    26  
    27  	if grantType == constant.GrantTypePassword {
    28  		actor.Config.SetUAAGrantType("")
    29  	} else {
    30  		actor.Config.SetUAAGrantType(string(grantType))
    31  	}
    32  
    33  	if grantType == constant.GrantTypeClientCredentials {
    34  		actor.Config.SetUAAClientCredentials(credentials["client_id"], "")
    35  	}
    36  
    37  	return nil
    38  }
    39  
    40  func (actor Actor) GetLoginPrompts() map[string]coreconfig.AuthPrompt {
    41  	rawPrompts := actor.UAAClient.LoginPrompts()
    42  	prompts := make(map[string]coreconfig.AuthPrompt)
    43  	for key, val := range rawPrompts {
    44  		prompts[key] = coreconfig.AuthPrompt{
    45  			Type:        knownAuthPromptTypes[val[0]],
    46  			DisplayName: val[1],
    47  		}
    48  	}
    49  	return prompts
    50  }
    51  
    52  var knownAuthPromptTypes = map[string]coreconfig.AuthPromptType{
    53  	"text":     coreconfig.AuthPromptTypeText,
    54  	"password": coreconfig.AuthPromptTypePassword,
    55  }