-
-
Notifications
You must be signed in to change notification settings - Fork 90
Expand file tree
/
Copy pathoauthToken.ts
More file actions
377 lines (337 loc) · 12 KB
/
Copy pathoauthToken.ts
File metadata and controls
377 lines (337 loc) · 12 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
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
/*
* The Peacock Project - a HITMAN server replacement.
* Copyright (C) 2021-2026 The Peacock Project Team
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { decode, sign } from "jsonwebtoken"
import { extractToken, uuidRegex } from "./utils"
import type { GameVersion, RequestWithJwt, UserProfile } from "./types/types"
import { getVersionedConfig } from "./configSwizzleManager"
import { log, LogLevel } from "./loggingInterop"
import {
STEAM_NAMESPACE_2018,
STEAM_NAMESPACE_2021,
} from "./platformEntitlements"
import {
getExternalUserData,
getUserData,
loadUserData,
writeExternalUserData,
writeNewUserData,
} from "./databaseHandler"
import { OfficialServerAuth, userAuths } from "./officialServerAuth"
import { randomUUID, randomBytes } from "crypto"
import { controller } from "./controller"
import {
EpicH1Strategy,
EpicH3Strategy,
IOIStrategy,
SteamH1Strategy,
SteamH2Strategy,
SteamScpcStrategy,
} from "./entitlementStrategies"
export const JWT_SECRET = PEACOCK_DEV
? "secret"
: randomBytes(32).toString("hex")
export type OAuthTokenBody = {
grant_type:
| "external_steam"
| "external_epic"
| "external_apple"
| "refresh_token"
steam_userid?: string
epic_userid?: string
apple_userid?: string
apple_refreshtoken?: string
device_os?: string
device_id?: string
access_token: string
pId?: string
locale: string
rgn: string
gs: string
steam_appid: string
}
export type OAuthTokenResponse = {
access_token: string
token_type: "bearer" | string
expires_in: number
refresh_token: string
}
export const error400: unique symbol = Symbol("http400")
export const error406: unique symbol = Symbol("http406")
/**
* This is the code that handles the OAuth token request.
* We cannot do this without a request object because of the refresh token use case.
*
* @param req The request object.
*/
export async function handleOAuthToken(
req: RequestWithJwt<never, OAuthTokenBody>,
): Promise<typeof error400 | typeof error406 | OAuthTokenResponse> {
const isScpc = req.body.gs === "scpc-prod"
const signOptions = {
notBefore: -60000,
expiresIn: 6000,
issuer: "auth.hitman.io",
audience: (() => {
if (isScpc) return "scpc-prod"
if (req.body.grant_type === "external_apple") return "macos-prod"
return "pc_prod_8"
})(),
noTimestamp: true,
}
let external_platform: "steam" | "epic" | "apple",
external_userid: string,
external_users_folder: "steamids" | "epicids" | "appleids",
external_appid: string
switch (req.body.grant_type) {
case "external_steam":
if (!/^\d{1,20}$/.test(req.body.steam_userid || "")) {
return error400 // invalid steam user id
}
external_platform = "steam"
external_userid = req.body.steam_userid || ""
external_users_folder = "steamids"
external_appid = req.body.steam_appid
break
case "external_epic": {
if (!/^[\da-f]{32}$/.test(req.body.epic_userid || "")) {
return error400 // invalid epic user id
}
const epic_token = decode(
req.body.access_token.replace(/^eg1~/, ""),
) as {
appid: string
app: string
}
if (!epic_token || !(epic_token.appid || epic_token.app)) {
return error400 // invalid epic access token
}
external_appid = epic_token.appid || epic_token.app
external_platform = "epic"
external_userid = req.body.epic_userid || ""
external_users_folder = "epicids"
break
}
case "external_apple":
external_platform = "apple"
external_userid = req.body.apple_userid || ""
external_users_folder = "appleids"
external_appid = "apple"
break
case "refresh_token":
// send back the token from the request (re-signed so the timestamps update)
extractToken(req) // init req.jwt
// remove signOptions from existing jwt
// @ts-expect-error Non-optional, we're reassigning.
delete req.jwt.nbf // notBefore
// @ts-expect-error Non-optional, we're reassigning.
delete req.jwt.exp // expiresIn
// @ts-expect-error Non-optional, we're reassigning.
delete req.jwt.iss // issuer
// @ts-expect-error Non-optional, we're reassigning.
delete req.jwt.aud // audience
if (!isScpc) {
if (userAuths.has(req.jwt.unique_name)) {
userAuths
.get(req.jwt.unique_name)!
._doRefresh()
.then(() => undefined)
.catch(() => {
log(LogLevel.WARN, "Failed authentication refresh.")
userAuths.get(req.jwt.unique_name)!.initialized =
false
})
}
}
return {
access_token: sign(req.jwt, JWT_SECRET, signOptions),
token_type: "bearer",
expires_in: 5000,
refresh_token: randomUUID(),
}
default:
return error406 // unsupported auth method
}
if (req.body.pId && !uuidRegex.test(req.body.pId)) {
return error406 // pId is not a GUID
}
const isHitman3 =
external_appid === "fghi4567xQOCheZIin0pazB47qGUvZw4" ||
external_appid === STEAM_NAMESPACE_2021 ||
external_platform === "apple"
let gameVersion: GameVersion = "h1"
if (isScpc) {
gameVersion = "scpc"
} else if (isHitman3) {
gameVersion = "h3"
} else if (external_appid === STEAM_NAMESPACE_2018) {
gameVersion = "h2"
}
if (!req.body.pId) {
// if no profile id supplied
try {
req.body.pId = (
await getExternalUserData(
external_userid,
external_users_folder,
gameVersion,
)
).toString()
} catch {
req.body.pId = randomUUID()
await writeExternalUserData(
external_userid,
external_users_folder,
req.body.pId,
gameVersion,
)
}
} else {
// if a profile id is supplied
getExternalUserData(external_userid, external_users_folder, gameVersion)
.then(() => null)
.catch(async () => {
// external id is not yet linked to this profile
await writeExternalUserData(
external_userid,
external_users_folder,
// we've already confirmed this will be there, and it's a GUID
req.body.pId!,
gameVersion,
)
})
}
try {
await loadUserData(req.body.pId, gameVersion)
} catch (e) {
log(LogLevel.DEBUG, "Unable to load profile information.")
log(LogLevel.DEBUG, e)
}
/*
Store user auth for all games except scpc
*/
if (!isScpc) {
const authContainer = new OfficialServerAuth(
gameVersion,
req.body.access_token,
)
log(LogLevel.DEBUG, `Setting up container with ID ${req.body.pId}.`)
userAuths.set(req.body.pId, authContainer)
await authContainer._initiallyAuthenticate(req)
}
let userData = getUserData(req.body.pId, gameVersion)
if (userData === undefined) {
// User does not exist, create new profile from default:
log(LogLevel.DEBUG, `Create new profile ${req.body.pId}`)
userData = getVersionedConfig(
"UserDefault",
gameVersion,
true,
) as UserProfile
userData.Id = req.body.pId
userData.LinkedAccounts[external_platform] = external_userid
if (external_platform === "steam") {
userData.SteamId = req.body.steam_userid!
} else if (external_platform === "epic") {
userData.EpicId = req.body.epic_userid!
} else if (external_platform === "apple") {
userData.AppleId = req.body.apple_userid!
}
if (Object.hasOwn(userData.Extensions, "inventory")) {
// @ts-expect-error No longer in the typedefs.
delete userData.Extensions.inventory
}
}
async function getEntitlements(): Promise<string[]> {
if (isScpc) {
return new SteamScpcStrategy().get()
}
if (gameVersion === "h1") {
if (external_platform === "steam") {
return new SteamH1Strategy().get()
} else if (external_platform === "epic") {
return new EpicH1Strategy().get()
} else {
log(LogLevel.ERROR, "Unsupported platform.")
return []
}
}
if (gameVersion === "h2") {
return new SteamH2Strategy().get()
}
if (gameVersion === "h3") {
if (external_platform === "epic") {
return await new EpicH3Strategy().get(
req.body.access_token,
req.body.epic_userid!,
)
} else if (external_platform === "steam") {
return await new IOIStrategy(
gameVersion,
STEAM_NAMESPACE_2021,
).get(req.body.pId!)
} else if (external_platform === "apple") {
// TODO
return []
} else {
log(LogLevel.ERROR, "Unsupported platform.")
return []
}
}
log(LogLevel.ERROR, "Unsupported platform.")
return []
}
const newEntP = await getEntitlements()
if (newEntP.length === 0) {
if (userData.Extensions.entP) {
log(
LogLevel.WARN,
`Error getting latest entitlement data for user ${req.body.pId}. Recently acquired DLCs might not be displayed!`,
)
} else {
log(
LogLevel.ERROR,
`Error getting entitlement data for new user ${req.body.pId}!`,
)
userData.Extensions.entP = newEntP
writeNewUserData(req.body.pId, userData, gameVersion)
}
} else {
userData.Extensions.entP = newEntP
writeNewUserData(req.body.pId, userData, gameVersion)
}
// Format here follows steam_external, Epic jwt has some different fields
const userinfo = {
"auth:method": req.body.grant_type,
roles: "user",
sub: req.body.pId,
unique_name: req.body.pId,
userid: external_userid,
platform: external_platform,
locale: req.body.locale,
rgn: req.body.rgn,
pis: external_appid,
cntry: req.body.locale,
}
controller.inventoryService.clearInventoryFor(req.body.pId)
return {
access_token: sign(userinfo, JWT_SECRET, signOptions),
token_type: "bearer",
expires_in: 5000,
refresh_token: randomUUID(),
}
}