Skip to content

Commit 306f09b

Browse files
authored
Add fetchCertificates option to getMinecraftJavaToken (#51)
* Add `fetchCertificate` option to `getMinecraftJavaToken` * Add types for fetch options * Pluralise `fetchCertificate`
1 parent 0ebf96d commit 306f09b

File tree

6 files changed

+52
-2
lines changed

6 files changed

+52
-2
lines changed

docs/API.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ flow.getXboxToken().then(console.log)
3939
Returns a Minecraft Java Edition auth token.
4040
* If you specify `fetchEntitlements` optional option, we will check if the account owns Minecraft and return the results of the API call. Undefined if request fails.
4141
* If you specify `fetchProfile`, we will do a call to `https://api.minecraftservices.com/minecraft/profile` for the currently signed in user and returns the results. Undefined if request fails.
42+
* If you specify `fetchCertificates` we will return the Mojang provided key-pair for a player, which are used for cryptographically signing chat messages
4243

4344
### getMinecraftBedrockToken (publicKey: KeyObject): Promise<string[]>
4445

examples/mcpc/deviceCode.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ if (!username) {
99

1010
async function doAuth () {
1111
const flow = new Authflow(username, cacheDir, { authTitle: Titles.MinecraftNintendoSwitch, deviceType: 'Nintendo' })
12-
const response = await flow.getMinecraftJavaToken({ fetchEntitlements: true, fetchProfile: true })
12+
const response = await flow.getMinecraftJavaToken({ fetchEntitlements: true, fetchProfile: true, fetchCertificates: true })
1313
console.log(response)
1414
}
1515

index.d.ts

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,50 @@ declare module 'prismarine-auth' {
2727
}>
2828
// Returns a Minecraft Java Edition auth token
2929
getMinecraftJavaToken(options?: {
30+
fetchCertificates?: boolean,
3031
fetchEntitlements?: boolean
3132
fetchProfile?: boolean
32-
}): Promise<{ token: string, entitlements: object, profile: object }>
33+
}): Promise<{ token: string, entitlements: MinecraftJavaEntitlements, profile: MinecraftJavaProfile, certificates: MinecraftJavaCertificates }>
3334
// Returns a Minecraft Bedrock Edition auth token. Public key parameter must be a KeyLike object.
3435
getMinecraftBedrockToken(publicKey: KeyObject): Promise<string>
3536
}
3637

38+
export interface MinecraftJavaEntitlements {
39+
items: MinecraftJavaEntitlementsItem[]
40+
signature: String
41+
keyId: String
42+
}
43+
44+
export interface MinecraftJavaEntitlementsItem {
45+
name: String
46+
signature: String
47+
}
48+
49+
export interface MinecraftJavaProfile {
50+
id: String
51+
name: String
52+
skins: MinecraftJavaProfileSkin[]
53+
capes: Array
54+
}
55+
56+
export interface MinecraftJavaProfileSkin {
57+
id: String,
58+
state: String,
59+
url: String,
60+
variant: 'CLASSIC'|'SLIM'
61+
}
62+
63+
export interface MinecraftJavaCertificates {
64+
keyPair: {
65+
privateKey: String
66+
publicKey: String
67+
}
68+
publicKeySignature: String
69+
publicKeySignatureV2: String
70+
expiresAt: String
71+
refreshedAfter: String
72+
}
73+
3774
export interface MicrosoftAuthFlowOptions {
3875
authTitle?: Titles
3976
deviceType?: String

src/MicrosoftAuthFlow.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,9 @@ class MicrosoftAuthFlow {
166166
if (options.fetchProfile) {
167167
response.profile = await this.mca.fetchProfile(response.token).catch(e => debug('Failed to obtain profile data', e))
168168
}
169+
if (options.fetchCertificates) {
170+
response.certificates = await this.mca.fetchCertificates(response.token).catch(e => debug('Failed to obtain keypair data', e))
171+
}
169172

170173
return response
171174
}

src/TokenManagers/MinecraftJavaTokenManager.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,5 +74,13 @@ class MinecraftJavaTokenManager {
7474
debug(`[mc] got entitlement response: ${entitlements}`)
7575
return entitlements
7676
}
77+
78+
async fetchCertificates (accessToken) {
79+
debug(`[mc] fetching key-pair with ${accessToken.slice(0, 16)}`)
80+
const headers = { ...fetchOptions.headers, Authorization: `Bearer ${accessToken}` }
81+
const certificate = await fetch(Endpoints.MinecraftServicesCertificate, { method: 'post', headers }).then(checkStatus)
82+
debug('[mc] got key-pair')
83+
return certificate
84+
}
7785
}
7886
module.exports = MinecraftJavaTokenManager

src/common/Constants.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ module.exports = {
1111
SisuAuthorize: 'https://sisu.xboxlive.com/authorize',
1212
XstsAuthorize: 'https://xsts.auth.xboxlive.com/xsts/authorize',
1313
MinecraftServicesLogWithXbox: 'https://api.minecraftservices.com/authentication/login_with_xbox',
14+
MinecraftServicesCertificate: 'https://api.minecraftservices.com/player/certificates',
1415
MinecraftServicesEntitlement: 'https://api.minecraftservices.com/entitlements/mcstore',
1516
MinecraftServicesProfile: 'https://api.minecraftservices.com/minecraft/profile',
1617
LiveDeviceCodeRequest: 'https://login.live.com/oauth20_connect.srf',

0 commit comments

Comments
 (0)