github.com/cloudfoundry/cli@v7.1.0+incompatible/command/v7/oauth_token_command.go (about)

     1  package v7
     2  
     3  import (
     4  	"errors"
     5  	"time"
     6  
     7  	"code.cloudfoundry.org/cli/api/uaa/constant"
     8  )
     9  
    10  type OauthTokenCommand struct {
    11  	BaseCommand
    12  
    13  	usage           interface{} `usage:"CF_NAME oauth-token"`
    14  	relatedCommands interface{} `related_commands:"curl"`
    15  }
    16  
    17  func (cmd OauthTokenCommand) Execute(_ []string) error {
    18  	err := cmd.SharedActor.CheckTarget(false, false)
    19  	if err != nil {
    20  		return err
    21  	}
    22  
    23  	if cmd.Config.UAAGrantType() == string(constant.GrantTypeClientCredentials) && cmd.Config.UAAOAuthClientSecret() == "" {
    24  		token, err := cmd.Actor.ParseAccessToken(cmd.Config.AccessToken())
    25  		if err != nil {
    26  			return errors.New(cmd.UI.TranslateText("Access token is invalid."))
    27  		}
    28  
    29  		expiration, success := token.Claims().Expiration()
    30  		if !success {
    31  			return errors.New(cmd.UI.TranslateText("Access token is missing expiration claim."))
    32  		}
    33  
    34  		if expiration.Before(time.Now()) {
    35  			return errors.New(cmd.UI.TranslateText("Access token has expired."))
    36  		}
    37  
    38  		cmd.UI.DisplayText(cmd.Config.AccessToken())
    39  		return nil
    40  	}
    41  
    42  	accessToken, err := cmd.Actor.RefreshAccessToken()
    43  	if err != nil {
    44  		return err
    45  	}
    46  
    47  	cmd.UI.DisplayText(accessToken)
    48  	return nil
    49  }