-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy pathauth.js
416 lines (339 loc) · 14 KB
/
auth.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
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
import {
fetch,
parseSetCookie,
stringifyCookies,
extractTokensFromUri,
tokenExpiry,
decodeToken,
ensureUsersFolder, wait, getProxyManager
} from "../misc/util.js";
import config from "../misc/config.js";
import fs from "fs";
import {client} from "../discord/bot.js";
import {addUser, deleteUser, getAccountWithPuuid, getUserJson, readUserJson, saveUser} from "./accountSwitcher.js";
import {checkRateLimit, isRateLimited} from "../misc/rateLimit.js";
import {queueCookiesLogin, queueUsernamePasswordLogin} from "./authQueue.js";
import {waitForAuthQueueResponse} from "../discord/authManager.js";
export class User {
constructor({id, puuid, auth, alerts=[], username, region, authFailures, lastFetchedData, lastNoticeSeen, lastSawEasterEgg}) {
this.id = id;
this.puuid = puuid;
this.auth = auth;
this.alerts = alerts || [];
this.username = username;
this.region = region;
this.authFailures = authFailures || 0;
this.lastFetchedData = lastFetchedData || 0;
this.lastNoticeSeen = lastNoticeSeen || "";
this.lastSawEasterEgg = lastSawEasterEgg || 0;
}
}
export const transferUserDataFromOldUsersJson = () => {
if(!fs.existsSync("data/users.json")) return;
if(client.shard && client.shard.ids[0] !== 0) return;
console.log("Transferring user data from users.json to the new format...");
console.log("(The users.json file will be backed up as users.json.old, just in case)");
const usersJson = JSON.parse(fs.readFileSync("data/users.json", "utf-8"));
const alertsArray = fs.existsSync("data/alerts.json") ? JSON.parse(fs.readFileSync("data/alerts.json", "utf-8")) : [];
const alertsForUser = (id) => alertsArray.filter(a => a.id === id);
for(const id in usersJson) {
const userData = usersJson[id];
const user = new User({
id: id,
puuid: userData.puuid,
auth: {
rso: userData.rso,
idt: userData.idt,
ent: userData.ent,
cookies: userData.cookies,
},
alerts: alertsForUser(id).map(alert => {return {uuid: alert.uuid, channel_id: alert.channel_id}}),
username: userData.username,
region: userData.region
});
saveUser(user);
}
fs.renameSync("data/users.json", "data/users.json.old");
}
export const getUser = (id, account=null) => {
if(id instanceof User) {
const user = id;
const userJson = readUserJson(user.id);
if(!userJson) return null;
const userData = userJson.accounts.find(a => a.puuid === user.puuid);
return userData && new User(userData);
}
try {
const userData = getUserJson(id, account);
return userData && new User(userData);
} catch(e) {
return null;
}
}
const userFilenameRegex = /\d+\.json/
export const getUserList = () => {
ensureUsersFolder();
return fs.readdirSync("data/users").filter(filename => userFilenameRegex.test(filename)).map(filename => filename.replace(".json", ""));
}
export const authUser = async (id, account=null) => {
// doesn't check if token is valid, only checks it hasn't expired
const user = getUser(id, account);
if(!user || !user.auth || !user.auth.rso) return {success: false};
const rsoExpiry = tokenExpiry(user.auth.rso);
if(rsoExpiry - Date.now() > 10_000) return {success: true};
return await refreshToken(id, account);
}
export const redeemUsernamePassword = async (id, login, password) => {
let rateLimit = isRateLimited("auth.riotgames.com");
if(rateLimit) return {success: false, rateLimit: rateLimit};
const proxyManager = getProxyManager();
const proxy = await proxyManager.getProxy("auth.riotgames.com");
const agent = await proxy?.createAgent("auth.riotgames.com");
// prepare cookies for auth request
const req1 = await fetch("https://auth.riotgames.com/api/v1/authorization", {
method: "POST",
headers: {
'Content-Type': 'application/json',
'user-agent': await getUserAgent()
},
body: JSON.stringify({
"client_id": "riot-client",
"code_challenge": "",
"code_challenge_method": "",
"acr_values": "",
"claims": "",
"nonce": "69420",
"redirect_uri": "http://localhost/redirect",
"response_type": "token id_token",
"scope": "openid link ban lol_region"
}),
proxy: agent
});
console.assert(req1.statusCode === 200, `Auth Request Cookies status code is ${req1.statusCode}!`, req1);
rateLimit = checkRateLimit(req1, "auth.riotgames.com");
if(rateLimit) return {success: false, rateLimit: rateLimit};
if(detectCloudflareBlock(req1)) return {success: false, rateLimit: "cloudflare"};
let cookies = parseSetCookie(req1.headers["set-cookie"]);
// get access token
const req2 = await fetch("https://auth.riotgames.com/api/v1/authorization", {
method: "PUT",
headers: {
'Content-Type': 'application/json',
'user-agent': await getUserAgent(),
'cookie': stringifyCookies(cookies)
},
body: JSON.stringify({
'type': 'auth',
'username': login,
'password': password,
'remember': true
}),
proxy: agent
});
console.assert(req2.statusCode === 200, `Auth status code is ${req2.statusCode}!`, req2);
rateLimit = checkRateLimit(req2, "auth.riotgames.com")
if(rateLimit) return {success: false, rateLimit: rateLimit};
if(detectCloudflareBlock(req2)) return {success: false, rateLimit: "cloudflare"};
cookies = {
...cookies,
...parseSetCookie(req2.headers['set-cookie'])
};
const json2 = JSON.parse(req2.body);
if(json2.type === 'error') {
if(json2.error === "auth_failure") console.error("Authentication failure!", json2);
else console.error("Unknown auth error!", JSON.stringify(json2, null, 2));
return {success: false};
}
if(json2.type === 'response') {
const user = await processAuthResponse(id, {login, password, cookies}, json2.response.parameters.uri);
addUser(user);
return {success: true};
} else if(json2.type === 'multifactor') { // 2FA
const user = new User({id});
user.auth = {
...user.auth,
waiting2FA: Date.now(),
cookies: cookies
}
if(config.storePasswords) {
user.auth.login = login;
user.auth.password = btoa(password);
}
addUser(user);
return {success: false, mfa: true, method: json2.multifactor.method, email: json2.multifactor.email};
}
return {success: false};
}
export const redeem2FACode = async (id, code) => {
let rateLimit = isRateLimited("auth.riotgames.com");
if(rateLimit) return {success: false, rateLimit: rateLimit};
let user = getUser(id);
const req = await fetch("https://auth.riotgames.com/api/v1/authorization", {
method: "PUT",
headers: {
'Content-Type': 'application/json',
'user-agent': await getUserAgent(),
'cookie': stringifyCookies(user.auth.cookies)
},
body: JSON.stringify({
'type': 'multifactor',
'code': code.toString(),
'rememberDevice': true
})
});
console.assert(req.statusCode === 200, `2FA status code is ${req.statusCode}!`, req);
rateLimit = checkRateLimit(req, "auth.riotgames.com")
if(rateLimit) return {success: false, rateLimit: rateLimit};
deleteUser(id);
user.auth = {
...user.auth,
cookies: {
...user.auth.cookies,
...parseSetCookie(req.headers['set-cookie'])
}
};
const json = JSON.parse(req.body);
if(json.error === "multifactor_attempt_failed" || json.type === "error") {
console.error("Authentication failure!", json);
return {success: false};
}
user = await processAuthResponse(id, {login: user.auth.login, password: atob(user.auth.password || ""), cookies: user.auth.cookies}, json.response.parameters.uri, user);
delete user.auth.waiting2FA;
addUser(user);
return {success: true};
}
const processAuthResponse = async (id, authData, redirect, user=null) => {
if(!user) user = new User({id});
const [rso, idt] = extractTokensFromUri(redirect);
if(rso == null) {
console.error("Riot servers didn't return an RSO token!");
console.error("Most likely the Cloudflare firewall is blocking your IP address. Try hosting on your home PC and seeing if the issue still happens.");
throw "Riot servers didn't return an RSO token!";
}
user.auth = {
...user.auth,
rso: rso,
idt: idt,
}
// save either cookies or login/password
if(authData.login && config.storePasswords && !user.auth.waiting2FA) { // don't store login/password for people with 2FA
user.auth.login = authData.login;
user.auth.password = btoa(authData.password);
delete user.auth.cookies;
} else {
user.auth.cookies = authData.cookies;
delete user.auth.login; delete user.auth.password;
}
user.puuid = decodeToken(rso).sub;
const existingAccount = getAccountWithPuuid(id, user.puuid);
if(existingAccount) {
user.username = existingAccount.username;
user.region = existingAccount.region;
if(existingAccount.auth) user.auth.ent = existingAccount.auth.ent;
}
// get username
const userInfo = await getUserInfo(user);
user.username = userInfo.username;
// get entitlements token
if(!user.auth.ent) user.auth.ent = await getEntitlements(user);
// get region
if(!user.region) user.region = await getRegion(user);
user.lastFetchedData = Date.now();
user.authFailures = 0;
return user;
}
export const getUserInfo = async (user) => {
const req = await fetch("https://auth.riotgames.com/userinfo", {
headers: {
'Authorization': "Bearer " + user.auth.rso
}
});
console.assert(req.statusCode === 200, `User info status code is ${req.statusCode}!`, req);
const json = JSON.parse(req.body);
if(json.acct) return {
puuid: json.sub,
username: json.acct.game_name && json.acct.game_name + "#" + json.acct.tag_line
}
}
const getEntitlements = async (user) => {
const req = await fetch("https://entitlements.auth.riotgames.com/api/token/v1", {
method: "POST",
headers: {
'Content-Type': 'application/json',
'Authorization': "Bearer " + user.auth.rso
}
});
console.assert(req.statusCode === 200, `Auth status code is ${req.statusCode}!`, req);
const json = JSON.parse(req.body);
return json.entitlements_token;
}
export const getRegion = async (user) => {
const req = await fetch("https://riot-geo.pas.si.riotgames.com/pas/v1/product/valorant", {
method: "PUT",
headers: {
'Content-Type': 'application/json',
'Authorization': "Bearer " + user.auth.rso
},
body: JSON.stringify({
'id_token': user.auth.idt,
})
});
console.assert(req.statusCode === 200, `PAS token status code is ${req.statusCode}!`, req);
const json = JSON.parse(req.body);
return json.affinities.live;
}
export const redeemCookies = async (id, cookies) => {
let rateLimit = isRateLimited("auth.riotgames.com");
if(rateLimit) return {success: false, rateLimit: rateLimit};
const req = await fetch("https://auth.riotgames.com/authorize?redirect_uri=https%3A%2F%2Fplayvalorant.com%2Fopt_in&client_id=play-valorant-web-prod&response_type=token%20id_token&scope=account%20openid&nonce=1", {
headers: {
'user-agent': await getUserAgent(),
cookie: cookies
}
});
console.assert(req.statusCode === 303, `Cookie Reauth status code is ${req.statusCode}!`, req);
rateLimit = checkRateLimit(req, "auth.riotgames.com");
if(rateLimit) return {success: false, rateLimit: rateLimit};
if(detectCloudflareBlock(req)) return {success: false, rateLimit: "cloudflare"};
if(req.headers.location.startsWith("/login")) return {success: false}; // invalid cookies
cookies = {
...parseSetCookie(cookies),
...parseSetCookie(req.headers['set-cookie'])
}
const user = await processAuthResponse(id, {cookies}, req.headers.location);
addUser(user);
return {success: true};
}
export const refreshToken = async (id, account=null) => {
console.log(`Refreshing token for ${id}...`)
let response = {success: false}
let user = getUser(id, account);
if(!user) return response;
if(user.auth.cookies) {
response = await queueCookiesLogin(id, stringifyCookies(user.auth.cookies));
if(response.inQueue) response = await waitForAuthQueueResponse(response);
}
if(!response.success && user.auth.login && user.auth.password) {
response = await queueUsernamePasswordLogin(id, user.auth.login, atob(user.auth.password));
if(response.inQueue) response = await waitForAuthQueueResponse(response);
}
if(!response.success && !response.mfa && !response.rateLimit) deleteUserAuth(user);
return response;
}
const getUserAgent = async () => {
// temporary bypass for Riot adding hCaptcha (see github issue #93)
return "ShooterGame/13 Windows/10.0.19043.1.256.64bit";
if(!riotClientVersion) await fetchRiotClientVersion();
return `RiotClient/${riotClientVersion}.1234567 rso-auth (Windows;10;;Professional, x64)`;
}
const detectCloudflareBlock = (req) => {
const blocked = req.statusCode === 403 && req.headers["x-frame-options"] === "SAMEORIGIN";
if(blocked) {
console.error("[ !!! ] Error 1020: Your bot might be rate limited, it's best to check if your IP address/your hosting service is blocked by Riot - try hosting on your own PC to see if it solves the issue?")
}
return blocked;
}
export const deleteUserAuth = (user) => {
user.auth = null;
saveUser(user);
}