|
1 | 1 | package githubclient
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "context" |
4 | 5 | "crypto/rsa"
|
5 | 6 | "crypto/x509"
|
6 |
| - "encoding/json" |
7 | 7 | "encoding/pem"
|
8 | 8 | "errors"
|
9 | 9 | "fmt"
|
10 |
| - "io" |
11 | 10 | "net/http"
|
12 | 11 | "strconv"
|
13 | 12 | "time"
|
@@ -96,36 +95,44 @@ func generateAppJWT(appID string, issuedAt time.Time, privateKeyPEM []byte) (str
|
96 | 95 | }
|
97 | 96 |
|
98 | 97 | func getInstallationAccessToken(baseURL, appJWT, installationID string) (string, error) {
|
99 |
| - url := fmt.Sprintf("%s/app/installations/%s/access_tokens", baseURL, installationID) |
| 98 | + tc := &http.Client{ |
| 99 | + Transport: &jwtTransport{ |
| 100 | + token: appJWT, |
| 101 | + rt: http.DefaultTransport, |
| 102 | + }, |
| 103 | + Timeout: 30 * time.Second, |
| 104 | + } |
100 | 105 |
|
101 |
| - req, err := http.NewRequest("POST", url, nil) |
| 106 | + client, err := github.NewClient(tc).WithEnterpriseURLs(baseURL, baseURL) |
102 | 107 | if err != nil {
|
103 |
| - return "", fmt.Errorf("failed to create request: %v", err) |
| 108 | + return "", fmt.Errorf("failed to create github client: %v", err) |
104 | 109 | }
|
105 | 110 |
|
106 |
| - req.Header.Set("Authorization", "Bearer "+appJWT) |
107 |
| - req.Header.Set("Accept", "application/vnd.github.v3+json") |
108 |
| - req.Header.Set("User-Agent", "terraform-provider-kw-github") |
109 |
| - |
110 |
| - client := &http.Client{Timeout: 30 * time.Second} |
111 |
| - resp, err := client.Do(req) |
| 111 | + installationIDInt, err := strconv.ParseInt(installationID, 10, 64) |
112 | 112 | if err != nil {
|
113 |
| - return "", fmt.Errorf("failed to make request: %v", err) |
| 113 | + return "", fmt.Errorf("invalid installation ID: %v", err) |
114 | 114 | }
|
115 |
| - defer resp.Body.Close() |
116 | 115 |
|
117 |
| - if resp.StatusCode != http.StatusCreated { |
118 |
| - body, _ := io.ReadAll(resp.Body) |
119 |
| - return "", fmt.Errorf("failed to get access token: %s - %s", resp.Status, string(body)) |
| 116 | + token, _, err := client.Apps.CreateInstallationToken( |
| 117 | + context.Background(), |
| 118 | + installationIDInt, |
| 119 | + &github.InstallationTokenOptions{}, |
| 120 | + ) |
| 121 | + if err != nil { |
| 122 | + return "", fmt.Errorf("failed to create installation token: %v", err) |
120 | 123 | }
|
121 | 124 |
|
122 |
| - var tokenResp struct { |
123 |
| - Token string `json:"token"` |
124 |
| - } |
| 125 | + return token.GetToken(), nil |
| 126 | +} |
125 | 127 |
|
126 |
| - if err := json.NewDecoder(resp.Body).Decode(&tokenResp); err != nil { |
127 |
| - return "", fmt.Errorf("failed to decode response: %v", err) |
128 |
| - } |
| 128 | +type jwtTransport struct { |
| 129 | + token string |
| 130 | + rt http.RoundTripper |
| 131 | +} |
129 | 132 |
|
130 |
| - return tokenResp.Token, nil |
| 133 | +func (t *jwtTransport) RoundTrip(req *http.Request) (*http.Response, error) { |
| 134 | + req.Header.Set("Authorization", "Bearer "+t.token) |
| 135 | + req.Header.Set("Accept", "application/vnd.github.v3+json") |
| 136 | + req.Header.Set("User-Agent", "terraform-provider-kw-github") |
| 137 | + return t.rt.RoundTrip(req) |
131 | 138 | }
|
0 commit comments