|
1 | 1 | package githubclient
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "context" |
5 | 4 | "crypto/rsa"
|
6 | 5 | "crypto/x509"
|
| 6 | + "encoding/json" |
7 | 7 | "encoding/pem"
|
8 | 8 | "errors"
|
9 | 9 | "fmt"
|
| 10 | + "io" |
10 | 11 | "net/http"
|
11 | 12 | "strconv"
|
12 | 13 | "time"
|
@@ -95,44 +96,36 @@ func generateAppJWT(appID string, issuedAt time.Time, privateKeyPEM []byte) (str
|
95 | 96 | }
|
96 | 97 |
|
97 | 98 | func getInstallationAccessToken(baseURL, appJWT, installationID string) (string, error) {
|
98 |
| - tc := &http.Client{ |
99 |
| - Transport: &jwtTransport{ |
100 |
| - token: appJWT, |
101 |
| - rt: http.DefaultTransport, |
102 |
| - }, |
103 |
| - Timeout: 30 * time.Second, |
104 |
| - } |
| 99 | + url := fmt.Sprintf("%s/app/installations/%s/access_tokens", baseURL, installationID) |
105 | 100 |
|
106 |
| - client, err := github.NewClient(tc).WithEnterpriseURLs(baseURL, baseURL) |
| 101 | + req, err := http.NewRequest("POST", url, nil) |
107 | 102 | if err != nil {
|
108 |
| - return "", fmt.Errorf("failed to create github client: %v", err) |
| 103 | + return "", fmt.Errorf("failed to create request: %v", err) |
109 | 104 | }
|
110 | 105 |
|
111 |
| - installationIDInt, err := strconv.ParseInt(installationID, 10, 64) |
| 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) |
112 | 112 | if err != nil {
|
113 |
| - return "", fmt.Errorf("invalid installation ID: %v", err) |
| 113 | + return "", fmt.Errorf("failed to make request: %v", err) |
114 | 114 | }
|
| 115 | + defer resp.Body.Close() |
115 | 116 |
|
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) |
| 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)) |
123 | 120 | }
|
124 | 121 |
|
125 |
| - return token.GetToken(), nil |
126 |
| -} |
| 122 | + var tokenResp struct { |
| 123 | + Token string `json:"token"` |
| 124 | + } |
127 | 125 |
|
128 |
| -type jwtTransport struct { |
129 |
| - token string |
130 |
| - rt http.RoundTripper |
131 |
| -} |
| 126 | + if err := json.NewDecoder(resp.Body).Decode(&tokenResp); err != nil { |
| 127 | + return "", fmt.Errorf("failed to decode response: %v", err) |
| 128 | + } |
132 | 129 |
|
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) |
| 130 | + return tokenResp.Token, nil |
138 | 131 | }
|
0 commit comments