forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
refresh_token.go
55 lines (45 loc) · 1.41 KB
/
refresh_token.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package uaa
import (
"fmt"
"net/http"
"net/url"
"strings"
"code.cloudfoundry.org/cli/api/uaa/internal"
)
// RefreshedTokens represents the UAA refresh token response.
type RefreshedTokens struct {
AccessToken string `json:"access_token"`
RefreshToken string `json:"refresh_token"`
Type string `json:"token_type"`
}
// AuthorizationToken returns formatted authorization header.
func (refreshTokenResponse RefreshedTokens) AuthorizationToken() string {
return fmt.Sprintf("%s %s", refreshTokenResponse.Type, refreshTokenResponse.AccessToken)
}
// RefreshAccessToken refreshes the current access token.
func (client *Client) RefreshAccessToken(refreshToken string) (RefreshedTokens, error) {
body := strings.NewReader(url.Values{
"client_id": {client.id},
"client_secret": {client.secret},
"grant_type": {"refresh_token"},
"refresh_token": {refreshToken},
}.Encode())
request, err := client.newRequest(requestOptions{
RequestName: internal.PostOAuthTokenRequest,
Header: http.Header{"Content-Type": {"application/x-www-form-urlencoded"}},
Body: body,
})
if err != nil {
return RefreshedTokens{}, err
}
request.SetBasicAuth(client.id, client.secret)
var refreshResponse RefreshedTokens
response := Response{
Result: &refreshResponse,
}
err = client.connection.Make(request, &response)
if err != nil {
return RefreshedTokens{}, err
}
return refreshResponse, nil
}