github.com/dcarley/cf-cli@v6.24.1-0.20170220111324-4225ff346898+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/internal"
    10  )
    11  
    12  // RefreshToken represents the UAA refresh token response.
    13  type RefreshToken struct {
    14  	AccessToken  string `json:"access_token"`
    15  	RefreshToken string `json:"refresh_token"`
    16  	Type         string `json:"token_type"`
    17  }
    18  
    19  // AuthorizationToken returns formatted authorization header.
    20  func (refreshTokenResponse RefreshToken) AuthorizationToken() string {
    21  	return fmt.Sprintf("%s %s", refreshTokenResponse.Type, refreshTokenResponse.AccessToken)
    22  }
    23  
    24  // RefreshAccessToken refreshes the current access token.
    25  func (client *Client) RefreshAccessToken(refreshToken string) (RefreshToken, error) {
    26  	body := strings.NewReader(url.Values{
    27  		"client_id":     {client.id},
    28  		"client_secret": {client.secret},
    29  		"grant_type":    {"refresh_token"},
    30  		"refresh_token": {refreshToken},
    31  	}.Encode())
    32  
    33  	request, err := client.newRequest(requestOptions{
    34  		RequestName: internal.RefreshTokenRequest,
    35  		Header: http.Header{
    36  			"Content-Type": {"application/x-www-form-urlencoded"},
    37  		},
    38  		Body: body,
    39  	})
    40  	if err != nil {
    41  		return RefreshToken{}, err
    42  	}
    43  
    44  	var refreshResponse RefreshToken
    45  	response := Response{
    46  		Result: &refreshResponse,
    47  	}
    48  
    49  	err = client.connection.Make(request, &response)
    50  	if err != nil {
    51  		return RefreshToken{}, err
    52  	}
    53  
    54  	return refreshResponse, nil
    55  }