code.cloudfoundry.org/cli@v7.1.0+incompatible/actor/v2action/auth.go (about)

     1  package v2action
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"code.cloudfoundry.org/cli/api/uaa/constant"
     7  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
     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  
    17  	actor.Config.UnsetOrganizationAndSpaceInformation()
    18  	credentials := make(map[string]string)
    19  
    20  	if grantType == constant.GrantTypePassword {
    21  		credentials["username"] = ID
    22  		credentials["password"] = secret
    23  	} else if grantType == constant.GrantTypeClientCredentials {
    24  		credentials["client_id"] = ID
    25  		credentials["client_secret"] = secret
    26  	}
    27  
    28  	accessToken, refreshToken, err := actor.UAAClient.Authenticate(credentials, origin, grantType)
    29  	if err != nil {
    30  		actor.Config.SetTokenInformation("", "", "")
    31  		return err
    32  	}
    33  
    34  	accessToken = fmt.Sprintf("bearer %s", accessToken)
    35  	actor.Config.SetTokenInformation(accessToken, refreshToken, "")
    36  
    37  	if grantType == constant.GrantTypePassword {
    38  		actor.Config.SetUAAGrantType("")
    39  	} else {
    40  		actor.Config.SetUAAGrantType(string(grantType))
    41  	}
    42  
    43  	if grantType == constant.GrantTypeClientCredentials {
    44  		actor.Config.SetUAAClientCredentials(ID, "")
    45  	}
    46  
    47  	return nil
    48  }
    49  
    50  func (actor Actor) GetLoginPrompts() map[string]coreconfig.AuthPrompt {
    51  	rawPrompts := actor.UAAClient.LoginPrompts()
    52  	prompts := make(map[string]coreconfig.AuthPrompt)
    53  	for key, val := range rawPrompts {
    54  		prompts[key] = coreconfig.AuthPrompt{
    55  			Type:        knownAuthPromptTypes[val[0]],
    56  			DisplayName: val[1],
    57  		}
    58  	}
    59  	return prompts
    60  }
    61  
    62  var knownAuthPromptTypes = map[string]coreconfig.AuthPromptType{
    63  	"text":     coreconfig.AuthPromptTypeText,
    64  	"password": coreconfig.AuthPromptTypePassword,
    65  }