-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
269 lines (217 loc) · 6.06 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
package main
import (
"bytes"
"encoding/json"
"fmt"
"log"
"math/rand"
"net/http"
"net/url"
"os"
"strings"
"github.com/joho/godotenv"
)
var (
discordBotToken = ""
discordClientID = ""
discordClientSecret = ""
guildID = ""
cfSecurityToken = ""
)
var rolesArr []string
var guildsArr []string
var (
postURL = "https://qa-cdn.altmp.workers.dev/new-token"
)
type userInfo struct {
ID string `json:"id"`
Username string `json:"username"`
Discrim string `json:"discriminator"`
Roles []string `json:"roles"`
}
type response struct {
Token string `json:"token"`
}
func main() {
godotenv.Load()
discordBotToken = os.Getenv("DISCORD_BOT_TOKEN")
guildsArr = strings.Split(os.Getenv("DISCORD_GUILDS"), ",")
rolesArr = strings.Split(os.Getenv("DISCORD_QA_ROLES"), ",")
cfSecurityToken = os.Getenv("CF_SECURITY_TOKEN")
discordClientID = os.Getenv("DISCORD_CLIENT_ID")
discordClientSecret = os.Getenv("DISCORD_CLIENT_SECRET")
host := os.Getenv("HTTP_HOST")
port := os.Getenv("HTTP_PORT")
http.HandleFunc("/auth", checkRoleHandler)
log.Println("Server started on http://" + host + ":" + port)
log.Fatal(http.ListenAndServe(host+":"+port, nil))
}
func checkRoleHandler(w http.ResponseWriter, r *http.Request) {
code := r.Header.Get("Authorization")
if code == "" {
http.Error(w, "Access token not provided", http.StatusBadRequest)
return
}
accessToken, err := exchangeCodeForToken(code)
if err != nil {
http.Error(w, "Failed to exchange code for access_token", http.StatusInternalServerError)
return
}
user, err := getUserInfo(accessToken)
if err != nil {
http.Error(w, "Failed to retrieve user information", http.StatusInternalServerError)
return
}
hasRole := false
// Check if the user has the specified role in the guild
for _, guild := range guildsArr {
tmpHasRole, err := checkUserRole(user.ID, guild)
if err != nil {
http.Error(w, "Failed to check user role", http.StatusInternalServerError)
return
}
if tmpHasRole {
hasRole = true
}
}
if !hasRole {
http.Error(w, "User does not have the required role", http.StatusForbidden)
return
}
// Generate a random string
randomString := generateRandomString(32)
// Make a POST request with the random string in the body
postData := map[string]string{"new_token": randomString}
postJSON, _ := json.Marshal(postData)
req, err := http.NewRequest("POST", postURL, bytes.NewBuffer(postJSON))
if err != nil {
http.Error(w, "Failed to create POST request", http.StatusInternalServerError)
return
}
// Set the "Security" header
req.Header.Set("Security", cfSecurityToken)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
http.Error(w, "Failed to make POST request", http.StatusInternalServerError)
return
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
http.Error(w, "POST request failed", resp.StatusCode)
return
}
responseData := response{Token: randomString}
responseJSON, err := json.Marshal(responseData)
if err != nil {
http.Error(w, "Failed to generate response", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(responseJSON)
}
func exchangeCodeForToken(code string) (string, error) {
clientID := discordClientID
clientSecret := discordClientSecret
authCode := code
// Prepare the request body
data := url.Values{}
data.Set("grant_type", "authorization_code")
data.Set("code", authCode)
data.Set("client_id", clientID)
data.Set("client_secret", clientSecret)
// Create a new HTTP client
client := &http.Client{}
// Create a new request
req, err := http.NewRequest("POST", "https://discord.com/api/v10/oauth2/token", strings.NewReader(data.Encode()))
if err != nil {
return "", err
}
// Set the Content-Type header
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
// Make the request
resp, err := client.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
// Check the response status code
if resp.StatusCode != http.StatusOK {
return "", err
}
// Read the response body
// Assuming the response body is in JSON format
var result struct {
AccessToken string `json:"access_token"`
RefreshToken string `json:"refresh_token"`
ExpiresIn int `json:"expires_in"`
}
// Decode the JSON response body into the result struct
err = json.NewDecoder(resp.Body).Decode(&result)
if err != nil {
return "", err
}
return result.AccessToken, nil
}
func getUserInfo(accessToken string) (*userInfo, error) {
client := &http.Client{}
req, err := http.NewRequest("GET", "https://discord.com/api/v10/users/@me", nil)
if err != nil {
return nil, err
}
req.Header.Set("Authorization", "Bearer "+accessToken)
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("failed to retrieve user information: %s", resp.Status)
}
var user userInfo
err = json.NewDecoder(resp.Body).Decode(&user)
if err != nil {
return nil, err
}
return &user, nil
}
func checkUserRole(userID string, guildID string) (bool, error) {
client := &http.Client{}
req, err := http.NewRequest("GET", fmt.Sprintf("https://discord.com/api/v10/guilds/%s/members/%s", guildID, userID), nil)
if err != nil {
return false, err
}
req.Header.Set("Authorization", "Bot "+discordBotToken)
resp, err := client.Do(req)
if err != nil {
return false, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return false, nil
}
var member struct {
Roles []string `json:"roles"`
}
err = json.NewDecoder(resp.Body).Decode(&member)
if err != nil {
return false, err
}
for _, role := range member.Roles {
for _, neededRole := range rolesArr {
if role == neededRole {
return true, nil
}
}
}
return false, nil
}
func generateRandomString(length int) string {
const chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
result := make([]byte, length)
for i := range result {
result[i] = chars[rand.Intn(len(chars))]
}
return string(result)
}