github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+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 values := url.Values{ 28 "client_id": {client.config.UAAOAuthClient()}, 29 "client_secret": {client.config.UAAOAuthClientSecret()}, 30 } 31 32 // An empty grant_type implies that the authentication grant_type is 'password' 33 if client.config.UAAGrantType() != "" { 34 values.Add("grant_type", client.config.UAAGrantType()) 35 } else { 36 values.Add("grant_type", string(constant.GrantTypeRefreshToken)) 37 values.Add("refresh_token", refreshToken) 38 } 39 40 body := strings.NewReader(values.Encode()) 41 42 request, err := client.newRequest(requestOptions{ 43 RequestName: internal.PostOAuthTokenRequest, 44 Header: http.Header{"Content-Type": {"application/x-www-form-urlencoded"}}, 45 Body: body, 46 }) 47 if err != nil { 48 return RefreshedTokens{}, err 49 } 50 51 if client.config.UAAGrantType() != string(constant.GrantTypeClientCredentials) { 52 request.SetBasicAuth(client.config.UAAOAuthClient(), client.config.UAAOAuthClientSecret()) 53 } 54 55 var refreshResponse RefreshedTokens 56 response := Response{ 57 Result: &refreshResponse, 58 } 59 60 err = client.connection.Make(request, &response) 61 if err != nil { 62 return RefreshedTokens{}, err 63 } 64 65 return refreshResponse, nil 66 }