-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
257 lines (217 loc) · 7.03 KB
/
client.go
File metadata and controls
257 lines (217 loc) · 7.03 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
// Package github provides GitHub API client with App authentication.
// handles JWT generation, installation token management, and automatic token
// refresh.
package github
import (
"context"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"fmt"
"net/http"
"sync"
"time"
"github.com/cockroachdb/errors"
"github.com/golang-jwt/jwt/v5"
"github.com/google/go-github/v79/github"
"golang.org/x/oauth2"
)
// Client wraps the GitHub API client with App authentication.
// automatically refreshes installation tokens before expiry.
type Client struct {
client *github.Client
org string
baseURL string
appID int64
privateKey *rsa.PrivateKey
installationID int64
tokenMu sync.RWMutex
token string
tokenExpAt time.Time
}
// NewAppClient creates a GitHub App client with default base URL.
func NewAppClient(appID, installationID int64, privateKeyPEM []byte, org string) (*Client, error) {
return NewAppClientWithBaseURL(appID, installationID, privateKeyPEM, org, "")
}
// NewAppClientWithBaseURL creates a GitHub App client with custom base URL.
// supports GitHub Enterprise Server instances.
func NewAppClientWithBaseURL(appID, installationID int64, privateKeyPEM []byte, org, baseURL string) (*Client, error) {
privateKey, err := parsePrivateKey(privateKeyPEM)
if err != nil {
return nil, errors.Wrap(err, "failed to parse private key")
}
c := &Client{
org: org,
appID: appID,
privateKey: privateKey,
installationID: installationID,
baseURL: baseURL,
}
if err := c.refreshToken(context.Background()); err != nil {
return nil, errors.Wrap(err, "failed to get initial token")
}
return c, nil
}
// parsePrivateKey parses RSA private key from PEM format.
// supports both PKCS1 and PKCS8 formats.
func parsePrivateKey(privateKeyPEM []byte) (*rsa.PrivateKey, error) {
block, _ := pem.Decode(privateKeyPEM)
if block == nil {
return nil, errors.New("failed to decode pem block: invalid format")
}
key, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
pkcs8Key, err2 := x509.ParsePKCS8PrivateKey(block.Bytes)
if err2 != nil {
return nil, errors.Wrap(err2, "failed to parse private key as pkcs1 or pkcs8")
}
rsaKey, ok := pkcs8Key.(*rsa.PrivateKey)
if !ok {
return nil, errors.New("private key is not rsa format")
}
return rsaKey, nil
}
return key, nil
}
// createJWT generates a JWT token for GitHub App authentication.
// token is valid for 10 minutes and backdated by 60 seconds for clock skew.
func (c *Client) createJWT() (string, error) {
now := time.Now()
claims := jwt.RegisteredClaims{
IssuedAt: jwt.NewNumericDate(now.Add(-60 * time.Second)),
ExpiresAt: jwt.NewNumericDate(now.Add(10 * time.Minute)),
Issuer: fmt.Sprintf("%d", c.appID),
}
token := jwt.NewWithClaims(jwt.SigningMethodRS256, claims)
return token.SignedString(c.privateKey)
}
// refreshToken exchanges JWT for installation token and updates client.
// installation tokens are valid for 1 hour.
func (c *Client) refreshToken(ctx context.Context) error {
jwtToken, err := c.createJWT()
if err != nil {
return errors.Wrap(err, "failed to create JWT")
}
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: jwtToken})
tc := oauth2.NewClient(ctx, ts)
appClient := github.NewClient(tc)
if c.baseURL != "" {
appClient.BaseURL, _ = appClient.BaseURL.Parse(c.baseURL)
}
installToken, resp, err := appClient.Apps.CreateInstallationToken(
ctx,
c.installationID,
&github.InstallationTokenOptions{},
)
if err != nil {
return errors.WithDetailf(
errors.Wrap(err, "failed to create installation token"),
"installation_id=%d app_id=%d", c.installationID, c.appID,
)
}
defer resp.Body.Close()
c.tokenMu.Lock()
c.token = installToken.GetToken()
c.tokenExpAt = installToken.GetExpiresAt().Time
ts2 := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: c.token})
tc2 := oauth2.NewClient(ctx, ts2)
c.client = github.NewClient(tc2)
if c.baseURL != "" {
c.client.BaseURL, _ = c.client.BaseURL.Parse(c.baseURL)
}
c.tokenMu.Unlock()
return nil
}
// ensureValidToken refreshes the installation token if it expires within 5
// minutes.
func (c *Client) ensureValidToken(ctx context.Context) error {
c.tokenMu.RLock()
needsRefresh := time.Now().Add(5 * time.Minute).After(c.tokenExpAt)
c.tokenMu.RUnlock()
if needsRefresh {
return c.refreshToken(ctx)
}
return nil
}
// GetOrg returns the GitHub organization name.
func (c *Client) GetOrg() string {
return c.org
}
// GetClient returns the underlying go-github client.
func (c *Client) GetClient() *github.Client {
return c.client
}
// Do executes an HTTP request with authentication.
// ensures token is valid before executing request.
func (c *Client) Do(ctx context.Context, req *http.Request) (*http.Response, error) {
if err := c.ensureValidToken(ctx); err != nil {
return nil, err
}
req = req.WithContext(ctx)
return c.client.Client().Do(req)
}
// GetAppSlug fetches the GitHub App slug identifier.
// used to detect changes made by the app itself.
// requires JWT authentication (not installation token).
func (c *Client) GetAppSlug(ctx context.Context) (string, error) {
jwtToken, err := c.createJWT()
if err != nil {
return "", errors.Wrap(err, "failed to create jwt for app slug fetch")
}
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: jwtToken})
tc := oauth2.NewClient(ctx, ts)
appClient := github.NewClient(tc)
if c.baseURL != "" {
appClient.BaseURL, _ = appClient.BaseURL.Parse(c.baseURL)
}
app, _, err := appClient.Apps.Get(ctx, "")
if err != nil {
return "", errors.Wrapf(err, "failed to fetch app info for app id %d", c.appID)
}
if app.Slug == nil {
return "", errors.Newf("app slug missing for app id %d", c.appID)
}
return *app.Slug, nil
}
// IsExternalCollaborator checks if a user is an outside collaborator rather
// than an organization member. returns true if user is not a full org member.
func (c *Client) IsExternalCollaborator(ctx context.Context, username string) (bool, error) {
if err := c.ensureValidToken(ctx); err != nil {
return false, err
}
membership, resp, err := c.client.Organizations.GetOrgMembership(ctx, username, c.org)
if err != nil {
if resp != nil && resp.StatusCode == 404 {
return true, nil
}
return false, errors.Wrapf(err, "failed to check org membership for user '%s'", username)
}
return membership == nil, nil
}
// ListOrgMembers returns all organization members excluding external
// collaborators.
func (c *Client) ListOrgMembers(ctx context.Context) ([]string, error) {
if err := c.ensureValidToken(ctx); err != nil {
return nil, err
}
opts := &github.ListMembersOptions{
ListOptions: github.ListOptions{PerPage: 100},
}
var allMembers []string
for {
members, resp, err := c.client.Organizations.ListMembers(ctx, c.org, opts)
if err != nil {
return nil, errors.Wrapf(err, "failed to list members for org '%s'", c.org)
}
for _, member := range members {
if member.Login != nil {
allMembers = append(allMembers, *member.Login)
}
}
if resp.NextPage == 0 {
break
}
opts.Page = resp.NextPage
}
return allMembers, nil
}