github.com/cloudfoundry-attic/cli-with-i18n@v6.32.1-0.20171002233121-7401370d3b85+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  // RefreshedTokens represents the UAA refresh token response.
    13  type RefreshedTokens 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 RefreshedTokens) 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) (RefreshedTokens, 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.PostOAuthTokenRequest,
    35  		Header:      http.Header{"Content-Type": {"application/x-www-form-urlencoded"}},
    36  		Body:        body,
    37  	})
    38  	if err != nil {
    39  		return RefreshedTokens{}, err
    40  	}
    41  
    42  	request.SetBasicAuth(client.id, client.secret)
    43  
    44  	var refreshResponse RefreshedTokens
    45  	response := Response{
    46  		Result: &refreshResponse,
    47  	}
    48  
    49  	err = client.connection.Make(request, &response)
    50  	if err != nil {
    51  		return RefreshedTokens{}, err
    52  	}
    53  
    54  	return refreshResponse, nil
    55  }