-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_access_token.go
91 lines (73 loc) · 2.2 KB
/
generate_access_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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package gorhsm
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
)
const rhsmTokenURL = "https://sso.redhat.com/auth/realms/redhat-external/protocol/openid-connect/token"
// Token describes the response from RHSM token refresh requests
type Token struct {
AccessToken string `json:"access_token"`
ExpiresIn int `json:"expires_in"`
NotBeforePolicy int `json:"not-before-policy"`
RefreshExpiresIn int `json:"refresh_expires_in"`
RefreshToken string `json:"refresh_token"`
Scope string `json:"scope"`
SessionState string `json:"session_state"`
TokenType string `json:"token_type"`
}
type tokenErrorResponse struct {
// HTTP response that caused this error
Response *http.Response
// Error message
ErrorMsg string `json:"error"`
ErrorDescription string `json:"error_description"`
}
// GenerateAccessToken generats a new API bearer token
func GenerateAccessToken(refreshToken string) (*Token, error) {
form := url.Values{}
form.Add("grant_type", "refresh_token")
form.Add("client_id", "rhsm-api")
form.Add("refresh_token", refreshToken)
req, err := http.NewRequest(http.MethodPost, rhsmTokenURL, strings.NewReader(form.Encode()))
if err != nil {
return nil, err
}
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
client := new(http.Client)
resp, err := client.Do(req)
if err != nil {
return nil, err
}
err = checkTokenResponse(resp)
if err != nil {
return nil, err
}
token := new(Token)
err = json.NewDecoder(resp.Body).Decode(token)
if err != nil {
return nil, fmt.Errorf("error decoding token response body")
}
return token, nil
}
func checkTokenResponse(r *http.Response) error {
if c := r.StatusCode; c >= 200 && c <= 299 {
return nil
}
errorResponse := &tokenErrorResponse{Response: r}
data, err := ioutil.ReadAll(r.Body)
if err == nil && len(data) > 0 {
err := json.Unmarshal(data, errorResponse)
if err != nil {
errorResponse.ErrorMsg = string(data)
}
}
return errorResponse
}
func (r *tokenErrorResponse) Error() string {
return fmt.Sprintf("%v %v: %d %v",
r.Response.Request.Method, r.Response.Request.URL, r.Response.StatusCode, r.ErrorMsg+": "+r.ErrorDescription)
}