-
Notifications
You must be signed in to change notification settings - Fork 27
/
mojang.ts
442 lines (399 loc) · 12.7 KB
/
mojang.ts
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
import { Dispatcher, fetch, File, FormData } from 'undici'
/**
* Users defined question when they register this account
*
* The question id, content mapping is:
*
* 1. What is your favorite pet's name?
* 2. What is your favorite movie?
* 3. What is your favorite author's last name?
* 4. What is your favorite artist's last name?
* 5. What is your favorite actor's last name?
* 6. What is your favorite activity?
* 7. What is your favorite restaurant?
* 8. What is the name of your favorite cartoon?
* 9. What is the name of the first school you attended?
* 10. What is the last name of your favorite teacher?
* 11. What is your best friend's first name?
* 12. What is your favorite cousin's name?
* 13. What was the first name of your first girl/boyfriend?
* 14. What was the name of your first stuffed animal?
* 15. What is your mother's middle name?
* 16. What is your father's middle name?
* 17. What is your oldest sibling's middle name?
* 18. In what city did your parents meet?
* 19. In what hospital were you born?
* 20. What is your favorite team?
* 21. How old were you when you got your first computer?
* 22. How old were you when you got your first gaming console?
* 23. What was your first video game?
* 24. What is your favorite card game?
* 25. What is your favorite board game?
* 26. What was your first gaming console?
* 27. What was the first book you ever read?
* 28. Where did you go on your first holiday?
* 29. In what city does your grandmother live?
* 30. In what city does your grandfather live?
* 31. What is your grandmother's first name?
* 32. What is your grandfather's first name?
* 33. What is your least favorite food?
* 34. What is your favorite ice cream flavor?
* 35. What is your favorite ice cream flavor?
* 36. What is your favorite place to visit?
* 37. What is your dream job?
* 38. What color was your first pet?
* 39. What is your lucky number?s
*
*/
export interface MojangChallenge {
readonly answer: { id: number }
readonly question: { id: number; question: string }
}
export interface MojangChallengeResponse {
id: number
answer: string
}
export interface MinecraftProfileResponse {
id: string // the real uuid of the account, woo
name: string // the mc user name of the account
skins: [{
id: string
state: 'ACTIVE' | 'string'
url: string
variant: 'CLASSIC' | string
alias: 'STEVE' | string
}]
capes: [{
id: string
state: 'ACTIVE' | string
url: string
}]
}
export interface MinecraftOwnershipResponse {
/**
* If the account doesn't own the game, the items array will be empty.
*/
items: Array<{
name: 'product_minecraft' | 'game_minecraft'
/**
* jwt signature
*/
signature: string
}>
/**
* jwt signature
*/
signature: string
keyId: string
}
export interface MinecraftProfileErrorResponse {
path: '/minecraft/profile'
errorType: 'NOT_FOUND' | string
error: string | 'NOT_FOUND'
errorMessage: string
developerMessage: string
}
export interface MojangSkin {
id: string
state: 'ACTIVE' | 'INACTIVE'
url: string
variant: 'SLIM' | 'CLASSIC'
}
export interface MojangCape {
id: string
state: 'ACTIVE' | 'INACTIVE'
url: string
/**
* Capes name
*/
alias: string
}
export interface MicrosoftMinecraftProfile {
id: string
name: string
skins: MojangSkin[]
capes: MojangCape[]
}
export interface NameChangeInformation {
changedAt: string
createdAt: string
nameChangeAllowed: boolean
}
export enum NameAvailability {
DUPLICATE = 'DUPLICATE',
AVAILABLE = 'AVAILABLE',
NOT_ALLOWED = 'NOT_ALLOWED',
}
export class SetNameError extends Error {
public path: string
public errorType: string
public error: string
public details: object
public errorMessage: string
public developerMessage: string
constructor(message: string, err: any) {
super(message)
this.name = 'SetNameError'
this.path = err.path
this.errorType = err.errorType
this.error = err.error
this.details = err.details
this.errorMessage = err.errorMessage
this.developerMessage = err.developerMessage
}
}
export class SetSkinError extends Error {
public path: string
public errorType: string
public error: string
public details: object
public errorMessage: string
public developerMessage: string
constructor(message: string, err: any) {
super(message)
this.name = 'SetSkinError'
this.path = err.path
this.errorType = err.errorType
this.error = err.error
this.details = err.details
this.errorMessage = err.errorMessage
this.developerMessage = err.developerMessage
}
}
export class MojangError extends Error {
public path: string
public errorMessage: string
public developerMessage: string
constructor(err: any) {
super(err.errorMessage)
this.path = err.path
this.errorMessage = err.errorMessage
this.developerMessage = err.developerMessage
Object.assign(this, err)
}
}
export class UnauthorizedError extends MojangError {
name = 'UnauthorizedError'
constructor(err: any) {
super(err)
}
}
export class ProfileNotFoundError extends MojangError {
name = 'ProfileNotFoundError'
constructor(err: any) {
super(err)
}
}
/**
* The mojang api client. Please referece https://wiki.vg/Mojang_API.
*
* All the apis need user to authenticate the access token from microsoft.
* @see {@link MicrosoftAuthenticator}
*/
export class MojangClient {
constructor(private dispatcher?: Dispatcher) { }
async setName(name: string, token: string, signal?: AbortSignal) {
const resp = await fetch(`https://api.minecraftservices.com/minecraft/profile/name/${name}`, {
method: 'PUT',
headers: {
Authorization: `Bearer ${token}`,
},
dispatcher: this.dispatcher,
signal,
})
switch (resp.status) {
case 200: return await resp.json() as MicrosoftMinecraftProfile
case 400: throw new SetNameError('Name is unavailable (Either taken or has not become available)', await resp.json())
case 403: throw new SetNameError('Name is unavailable (Either taken or has not become available)', await resp.json())
case 401: throw new SetNameError('Unauthorized (Bearer token expired or is not correct)', await resp.json())
case 429: throw new SetNameError('Too many requests sent', await resp.json())
case 500: throw new SetNameError('Timed out (API lagged out and could not respond)', await resp.json())
}
throw new SetNameError('Unknown error', await resp.json())
}
async getNameChangeInformation(token: string) {
const resp = await fetch('https://api.minecraftservices.com/minecraft/profile/namechange', {
method: 'GET',
headers: {
Authorization: `Bearer ${token}`,
},
dispatcher: this.dispatcher,
})
return await resp.json() as NameChangeInformation
}
async checkNameAvailability(name: string, token: string, signal?: AbortSignal): Promise<NameAvailability> {
const resp = await fetch(`https://api.minecraftservices.com/minecraft/profile/name/${name}/available`, {
method: 'GET',
headers: {
Authorization: `Bearer ${token}`,
},
dispatcher: this.dispatcher,
signal,
})
const result = await resp.json() as any
return result.status
}
async getProfile(token: string, signal?: AbortSignal): Promise<MicrosoftMinecraftProfile> {
const resp = await fetch('https://api.minecraftservices.com/minecraft/profile', {
method: 'GET',
headers: {
Authorization: `Bearer ${token}`,
},
dispatcher: this.dispatcher,
signal,
})
if (resp.headers.get('content-type')?.toLocaleLowerCase() !== 'application/json') {
throw new Error(await resp.text())
}
const json = await resp.json() as any
if (resp.ok) {
return json as MicrosoftMinecraftProfile
} else if (json.error === 'NOT_FOUND') {
throw new ProfileNotFoundError(json)
} else if (resp.status === 401) {
throw new UnauthorizedError(json)
}
throw Object.assign(new Error('Unknown Error'), json)
}
async setSkin(fileName: string, skin: string | Buffer, variant: 'slim' | 'classic', token: string, signal?: AbortSignal) {
const body = typeof skin === 'string' ? JSON.stringify({ url: skin, variant }) : getSkinFormData(skin, fileName, variant)
const headers: Record<string, string> = {
Authorization: `Bearer ${token}`,
}
if (typeof body === 'string') {
headers['Content-Type'] = 'application/json'
}
const resp = await fetch('https://api.minecraftservices.com/minecraft/profile/skins', {
method: 'POST',
headers,
body,
signal,
})
const profileResponse: MinecraftProfileResponse | MinecraftProfileErrorResponse = await resp.json() as any
if (resp.status === 401) {
throw new UnauthorizedError(await resp.json())
}
if ('error' in profileResponse || 'errorMessage' in profileResponse) {
throw new SetSkinError(`Fail to set skin ${profileResponse.errorMessage}`, profileResponse)
}
return profileResponse
}
async resetSkin(token: string, signal?: AbortSignal) {
const resp = await fetch('https://api.minecraftservices.com/minecraft/profile/skins/active', {
method: 'DELETE',
headers: {
Authorization: `Bearer ${token}`,
},
dispatcher: this.dispatcher,
signal,
})
if (resp.status === 401) {
throw new UnauthorizedError(await resp.json())
}
}
async hideCape(token: string, signal?: AbortSignal) {
const resp = await fetch('https://api.minecraftservices.com/minecraft/profile/capes/active', {
method: 'DELETE',
headers: {
Authorization: `Bearer ${token}`,
},
dispatcher: this.dispatcher,
signal,
})
if (resp.status === 401) {
throw new UnauthorizedError(await resp.json())
}
}
async showCape(capeId: string, token: string, signal?: AbortSignal) {
const resp = await fetch('https://api.minecraftservices.com/minecraft/profile/capes/active', {
method: 'PUT',
headers: {
Authorization: `Bearer ${token}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({ capeId }),
dispatcher: this.dispatcher,
signal,
})
if (resp.status === 401) {
throw new UnauthorizedError(await resp.json())
}
if (resp.status === 400) {
throw new Error()
}
const profile = await resp.json() as MicrosoftMinecraftProfile
return profile
}
async verifySecurityLocation(token: string, signal?: AbortSignal) {
const resp = await fetch('https://api.mojang.com/user/security/location', {
method: 'GET',
headers: {
Authorization: `Bearer ${token}`,
},
dispatcher: this.dispatcher,
signal,
})
if (resp.status === 204) {
return true
}
return false
}
async getSecurityChallenges(token: string) {
const resp = await fetch('https://api.mojang.com/user/security/challenges', {
method: 'GET',
headers: {
Authorization: `Bearer ${token}`,
},
dispatcher: this.dispatcher,
})
if (resp.status === 401) {
throw new UnauthorizedError(await resp.json())
}
return await resp.json() as MojangChallenge[]
}
async submitSecurityChallenges(answers: MojangChallengeResponse[], token: string) {
const resp = await fetch('https://api.mojang.com/user/security/location', {
method: 'POST',
headers: {
Authorization: `Bearer ${token}`,
'Content-Type': 'application/json',
},
body: JSON.stringify(answers),
dispatcher: this.dispatcher,
})
if (resp.status === 204) {
return
}
if (resp.status === 401) {
throw new UnauthorizedError(await resp.json())
}
throw new Error()
}
/**
* Return the owner ship list of the player with those token.
*/
async checkGameOwnership(token: string, signal?: AbortSignal) {
const mcResponse = await fetch('https://api.minecraftservices.com/entitlements/mcstore', {
headers: {
Authorization: `Bearer ${token}`,
},
dispatcher: this.dispatcher,
signal,
})
if (mcResponse.status === 401) {
throw new UnauthorizedError(await mcResponse.text())
}
if (!mcResponse.ok || mcResponse.headers.get('content-type')?.toLocaleLowerCase() !== 'application/json') {
throw new Error(await mcResponse.text())
}
const result = await mcResponse.json() as MinecraftOwnershipResponse
return result
}
}
function getSkinFormData(buf: Buffer, fileName: string, variant: 'slim' | 'classic') {
const form = new FormData()
form.append('variant', variant)
const file = new File([buf], fileName, { type: 'image/png' })
form.append('file', file)
return form
}