github.com/cloudfoundry/cli@v7.1.0+incompatible/api/uaa/refresh_token.go (about)

     1  package uaa
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"net/url"
     7  	"strings"
     8  
     9  	"code.cloudfoundry.org/cli/api/uaa/constant"
    10  	"code.cloudfoundry.org/cli/api/uaa/internal"
    11  )
    12  
    13  // RefreshedTokens represents the UAA refresh token response.
    14  type RefreshedTokens struct {
    15  	AccessToken  string `json:"access_token"`
    16  	RefreshToken string `json:"refresh_token"`
    17  	Type         string `json:"token_type"`
    18  }
    19  
    20  // AuthorizationToken returns formatted authorization header.
    21  func (refreshTokenResponse RefreshedTokens) AuthorizationToken() string {
    22  	return fmt.Sprintf("%s %s", refreshTokenResponse.Type, refreshTokenResponse.AccessToken)
    23  }
    24  
    25  // RefreshAccessToken refreshes the current access token.
    26  func (client *Client) RefreshAccessToken(refreshToken string) (RefreshedTokens, error) {
    27  	var values url.Values
    28  
    29  	switch client.config.UAAGrantType() {
    30  	case string(constant.GrantTypeClientCredentials):
    31  		values = client.clientCredentialRefreshBody()
    32  	case "", string(constant.GrantTypePassword): // CLI used to write empty string for grant type in the case of password; preserve compatibility with old config.json files
    33  		values = client.refreshTokenBody(refreshToken)
    34  	}
    35  
    36  	body := strings.NewReader(values.Encode())
    37  
    38  	request, err := client.newRequest(requestOptions{
    39  		RequestName: internal.PostOAuthTokenRequest,
    40  		Header:      http.Header{"Content-Type": {"application/x-www-form-urlencoded"}},
    41  		Body:        body,
    42  	})
    43  	if err != nil {
    44  		return RefreshedTokens{}, err
    45  	}
    46  
    47  	if client.config.UAAGrantType() != string(constant.GrantTypeClientCredentials) {
    48  		request.SetBasicAuth(client.config.UAAOAuthClient(), client.config.UAAOAuthClientSecret())
    49  	}
    50  
    51  	var refreshResponse RefreshedTokens
    52  	response := Response{
    53  		Result: &refreshResponse,
    54  	}
    55  
    56  	err = client.connection.Make(request, &response)
    57  	if err != nil {
    58  		return RefreshedTokens{}, err
    59  	}
    60  
    61  	return refreshResponse, nil
    62  }
    63  
    64  func (client *Client) clientCredentialRefreshBody() url.Values {
    65  	return url.Values{
    66  		"client_id":     {client.config.UAAOAuthClient()},
    67  		"client_secret": {client.config.UAAOAuthClientSecret()},
    68  		"grant_type":    {string(constant.GrantTypeClientCredentials)},
    69  	}
    70  }
    71  
    72  func (client *Client) refreshTokenBody(refreshToken string) url.Values {
    73  	return url.Values{
    74  		"refresh_token": {refreshToken},
    75  		"grant_type":    {string(constant.GrantTypeRefreshToken)},
    76  	}
    77  }