-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
177 lines (138 loc) · 4.29 KB
/
index.js
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
import * as cfg from "./config.js";
import SteamID from "steamid";
const api = "https://discordapp.com/api/v6";
const scope = "identify connections guilds.join";
// Cloudflare entrypoint
addEventListener("fetch", event => {
event.respondWith(handleRequest(event.request));
});
async function handleRequest(request) {
let params = new URL(request.url).searchParams;
// If this is an OAuth2 error result
if (params.has("error")) {
return err(params.get("error_description"));
}
// If this isn't an OAuth2 result, redirect into OAuth2 flow
if (!params.has("code")) {
let params = new URLSearchParams({
client_id: cfg.client_id,
redirect_uri: cfg.redirect_uri,
response_type: "code",
scope
});
return Response.redirect(
`https://discordapp.com/api/oauth2/authorize?${params.toString()}`
);
}
// This is an OAuth2 result, fetch a token
let token_resp = await fetch(`${api}/oauth2/token`, {
method: "POST",
headers: { "Content-Type": "application/x-www-form-urlencoded" },
body: new URLSearchParams({
client_id: cfg.client_id,
client_secret: cfg.client_secret,
code: params.get("code"),
grant_type: "authorization_code",
redirect_uri: cfg.redirect_uri,
scope
})
});
let token_json = await token_resp.json();
if (!token_resp.ok) {
return err(token_json.error_description)
}
let access_token = token_json.access_token;
// We have the user's access_token, fetch their identity
let ident_resp = await fetch(`${api}/users/@me`, {
headers: { Authorization: `Bearer ${access_token}` }
});
let ident_json = await ident_resp.json();
// unlikely
if (!ident_resp.ok) {
return err(ident_json.message);
}
let user_id = ident_json.id;
// fetch their linked accounts
let conn_resp = await fetch(`${api}/users/@me/connections`, {
headers: { Authorization: `Bearer ${access_token}` }
});
let conn_json = await conn_resp.json();
// unlikely
if (!conn_resp.ok) {
return err(conn_json.message);
}
let steam_conn = conn_json.find(c => c.type == "steam");
if (steam_conn === undefined) {
return err("You must link your Steam account in your Discord settings");
}
let broken_steam_id = new SteamID(steam_conn.id);
broken_steam_id.instance = SteamID.Instance.DESKTOP;
let steam_id = broken_steam_id.getSteamID64();
let steam_resp = await fetch(`https://steamcommunity.com/profiles/${steam_id}?xml=1`)
if (!steam_resp.ok) {
return err("Invalid Steam response")
}
let steam_body = await steam_resp.text()
if (steam_body.includes("<privacyMessage>")) {
return err("Invalid Steam response")
}
// We verfied everything we want, we can let them join
let add_resp = await fetch(
`${api}/guilds/${cfg.guild_id}/members/${user_id}`,
{
method: "PUT",
headers: {
Authorization: `Bot ${cfg.bot_token}`,
"Content-Type": "application/json"
},
body: JSON.stringify({
access_token,
roles: [cfg.role_id]
})
}
);
if (!add_resp.ok) {
let add_json = await add_resp.json();
return err(add_json.message);
}
// If they're already part of the server, give them the verification role
if (add_resp.status == 204) {
let role_resp = await fetch(
`${api}/guilds/${cfg.guild_id}/members/${user_id}/roles/${cfg.role_id}`,
{
method: "PUT",
headers: { Authorization: `Bot ${cfg.bot_token}` }
}
);
if (!role_resp.ok) {
let role_json = await role_resp.json();
return err(role_json.message);
}
}
let hook_resp = await fetch(`${api}/webhooks/${cfg.log_webhook_id}/${cfg.log_webhook_token}`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
content: `<@${user_id}> linked to <https://steamcommunity.com/profiles/${steam_id}>`
})
});
if (!hook_resp.ok) {
return err(await hook_resp.text())
}
return new Response(`
~ NaCl.gg Discord Auth ~
Authentication was successful
`);
}
function err(message) {
return new Response(`
~ NaCl.gg Discord Auth ~
Ran into an error:
${message}
It's very possible that trying again will fix it:
${cfg.redirect_uri}
If it doesn't, please contact administrators
`, {
status: 400
})
}