-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
335 lines (286 loc) · 9.49 KB
/
main.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
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
package main
import (
"bytes"
"context"
"fmt"
"github.com/bradleyfalzon/ghinstallation"
"github.com/google/go-github/v53/github"
"github.com/joho/godotenv"
"github.com/pkg/errors"
"golang.org/x/oauth2"
"io"
"log"
"net/http"
"net/url"
"os"
"strconv"
"strings"
)
func main() {
conf, err := getConfigFromEnv()
if err != nil {
log.Fatalf("Error loading config from environment: %v", err)
}
logger := log.Default()
logger.SetFlags(log.Ltime | log.Ldate | log.LUTC)
appTransport, err := ghinstallation.NewKeyFromFile(http.DefaultTransport, conf.GHAppID, conf.GHAppInstallationID, conf.GHAppKeyPath)
appClient := github.NewClient(&http.Client{Transport: appTransport})
http.HandleFunc("/link", handlePanicHTTP(handleLink(conf.GHOauthID)))
http.HandleFunc("/authorize", handlePanicHTTP(handleAuthorize(conf.GHOauthID, conf.GHOAuthSecret, appClient)))
err = http.ListenAndServe(":8080", nil)
if err != nil {
log.Fatal(err)
}
}
func getConfigFromEnv() (Config, error) {
_ = godotenv.Load()
ghOAuthID, ok := os.LookupEnv("GITHUB_OAUTH_ID")
if !ok {
return Config{}, errors.New("GITHUB_OAUTH_ID is required")
}
ghOAuthSecret, ok := os.LookupEnv("GITHUB_OAUTH_SECRET")
if !ok {
return Config{}, errors.New("GITHUB_OAUTH_SECRET is required")
}
ghAppIDStr, ok := os.LookupEnv("GITHUB_APP_ID")
if !ok {
return Config{}, errors.New("GITHUB_APP_ID is required")
}
ghAppID, err := strconv.ParseInt(ghAppIDStr, 0, 64)
if err != nil {
return Config{}, errors.New("GITHUB_APP_ID is not a valid int64")
}
ghInstallationIDStr, ok := os.LookupEnv("GITHUB_INSTALLATION_ID")
if !ok {
return Config{}, errors.New("GITHUB_INSTALLATION_ID is required")
}
ghAppInstallationID, err := strconv.ParseInt(ghInstallationIDStr, 0, 64)
if err != nil {
return Config{}, errors.New("GITHUB_INSTALLATION_ID is not a valid int64")
}
ghAppKeyPath, ok := os.LookupEnv("GITHUB_APP_KEY_PATH")
if !ok {
return Config{}, errors.New("GITHUB_APP_KEY_PATH is required")
}
return Config{
GHOauthID: ghOAuthID,
GHOAuthSecret: ghOAuthSecret,
GHAppID: ghAppID,
GHAppInstallationID: ghAppInstallationID,
GHAppKeyPath: ghAppKeyPath,
}, nil
}
type Config struct {
GHOauthID string
GHOAuthSecret string
GHAppID int64
GHAppInstallationID int64
GHAppKeyPath string
}
func handlePanicHTTP(handlerFunc http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
defer func() {
err := recover()
if err == nil {
return
}
log.Printf("HTTP request panicked: %v", err)
http.Error(w, fmt.Sprintf("Panicked: %v", err), 500)
}()
handlerFunc(w, r)
}
}
func handleLink(ghClientID string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
if strings.Contains(r.UserAgent(), "bot") {
return
}
log.Println("Redirecting a new request for linking")
http.Redirect(w, r, fmt.Sprintf("https://github.com/login/oauth/authorize?%s", url.Values{
"client_id": []string{ghClientID},
"scope": []string{strings.Join([]string{"repo:invite", string(github.ScopeReadOrg)}, ",")},
}.Encode()), http.StatusSeeOther)
}
}
func handleAuthorize(ghClientID, ghClientSecret string, appClient *github.Client) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
err := r.ParseForm()
if err != nil {
http.Error(w, "could not parse form", http.StatusInternalServerError)
return
}
code := r.Form.Get("code")
accessToken, err := getAccessToken(code, ghClientID, ghClientSecret)
if err != nil {
http.Error(w, fmt.Sprintf("error exchanging code for access token: %v", err), 500)
return
}
log.Println("Successfully received an access token")
ctx := context.Background()
tokenSource := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: accessToken})
client := github.NewClient(oauth2.NewClient(ctx, tokenSource))
log.Println("Getting authenticated user")
user, _, err := client.Users.Get(ctx, "")
if err != nil {
http.Error(w, fmt.Sprintf("error getting authenticated user: %v", err), 500)
return
}
username := *user.Login
log.Printf("Checking if %v already has repo access\n", username)
hasAccess, err := hasUserRepoAccess(client)
if err != nil {
http.Error(w, fmt.Sprintf("Error checking for repo access: %v", err), 500)
return
}
if hasAccess {
log.Printf("%v has access to repo, redirecting\n", username)
redirectToRepo(w, r)
return
}
log.Printf("Trying to accept the invitation for %v if it exists\n", username)
accepted, err := acceptInvitationIfPresent(client)
if err != nil {
http.Error(w, fmt.Sprintf("Error trying to accept the invitation: %v", err), 500)
return
}
if accepted {
log.Printf("Was able to accept the invitation for %v, redirecting\n", username)
redirectToRepo(w, r)
return
}
log.Printf("Checking if %v is in the EpicGames org\n", username)
isInOrg, err := isUserInAnyEpicOrg(client)
if err != nil {
http.Error(w, fmt.Sprintf("error getting org status: %v", err), 500)
return
}
if !isInOrg {
log.Printf("%v was not in the EpicGames organisation\n", username)
http.Error(w, fmt.Sprintf("You are not in the EpicGames organisation. Please follow these directions and try again: https://www.unrealengine.com/en-US/ue-on-github"), 403)
return
}
log.Printf("User %s was in the EpicGames organisation\n", username)
log.Printf("Sending invitation for %v\n", username)
err = sendCollaborationInvitation(appClient, username)
if err != nil {
http.Error(w, fmt.Sprintf("Could not send you an invitation: %v", err), 500)
return
}
log.Printf("Accepting the invitation for %v\n", username)
err = acceptInvitation(client)
if err != nil {
http.Error(w, fmt.Sprintf("Error accepting the invitation: %v", err), 500)
return
}
log.Printf("Everything went ok, redirecting %v to repo\n", username)
redirectToRepo(w, r)
}
}
func getAccessToken(code, ghClientID, ghClientSecret string) (string, error) {
var b bytes.Buffer
resp, err := http.Post(fmt.Sprintf("https://github.com/login/oauth/access_token?client_id=%s&client_secret=%s&code=%s",
ghClientID, ghClientSecret, code), "application/json", &b)
if err != nil {
return "", errors.Wrap(err, "error posting to GitHub")
}
defer resp.Body.Close()
data, err := io.ReadAll(resp.Body)
if err != nil {
return "", errors.Wrap(err, "error reading GitHub's response")
}
query, err := url.ParseQuery(string(data))
if err != nil {
return "", errors.Wrap(err, "error parsing returned query")
}
return query.Get("access_token"), nil
}
var epicOrgs = []string{"EpicGames", "EpicGames-Mirror-A"}
func isUserInAnyEpicOrg(client *github.Client) (bool, error) {
for _, org := range epicOrgs {
isInOrg, err := isUserInEpicOrg(client, org)
if err != nil {
return false, errors.Wrap(err, "error checking user's org membership")
}
if isInOrg {
return true, nil
}
}
return false, nil
}
func isUserInEpicOrg(client *github.Client, org string) (bool, error) {
ctx := context.Background()
_, _, err := client.Teams.GetTeamBySlug(ctx, org, "developers")
if err, ok := err.(*github.ErrorResponse); ok { // We rely on an implementation bug to check if the user can access a team
if err.Response.StatusCode == 404 {
return false, nil
}
if err.Response.StatusCode == 403 {
return true, nil
}
}
if err != nil {
return false, errors.Wrap(err, "error checking user's org membership")
}
return true, nil
}
func hasUserRepoAccess(client *github.Client) (bool, error) {
ctx := context.Background()
_, _, err := client.Repositories.Get(ctx, "SatisfactoryModding", "UnrealEngine")
if err, ok := err.(*github.ErrorResponse); ok { // We rely on an implementation bug to check if the user can access a repo
if err.Response.StatusCode == 404 {
return false, nil
}
if err.Response.StatusCode == 403 {
return true, nil
}
}
if err != nil {
return false, errors.Wrap(err, "error checking user's repo access")
}
return true, nil
}
func acceptInvitationIfPresent(client *github.Client) (bool, error) {
ctx := context.Background()
invitations, _, err := client.Users.ListInvitations(ctx, nil)
if err, ok := err.(*github.ErrorResponse); ok { // We rely on an implementation bug to check if the user can access a repo
if err.Response.StatusCode == 404 {
return false, nil
}
}
if err != nil {
return false, errors.Wrap(err, "error listing collaboration invitations")
}
for _, invitation := range invitations {
repo := *invitation.Repo
if strings.ToLower(*repo.Owner.Login) != "satisfactorymodding" || *repo.Name != "UnrealEngine" {
continue
}
_, err = client.Users.AcceptInvitation(ctx, *invitation.ID)
if err != nil {
return false, errors.Wrap(err, "error accepting collaboration invitation")
}
return true, nil
}
return false, nil
}
func acceptInvitation(client *github.Client) error {
accepted, err := acceptInvitationIfPresent(client)
if err != nil {
return err
}
if !accepted {
return errors.New("Could not find your invitation. Check your email to see if you received one.")
}
return nil
}
func sendCollaborationInvitation(authenticatedClient *github.Client, user string) error {
_, _, err := authenticatedClient.Repositories.AddCollaborator(context.Background(), "SatisfactoryModding", "UnrealEngine", user, &github.RepositoryAddCollaboratorOptions{
Permission: "pull",
})
return err
}
func redirectToRepo(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "https://github.com/SatisfactoryModding/UnrealEngine", http.StatusSeeOther)
}