bun icon indicating copy to clipboard operation
bun copied to clipboard

Mineflayer error: Cant Authenticate with Microsoft

Open link-discord opened this issue 3 months ago • 4 comments

What version of Bun is running?

1.1.2+c8d072c2a

What platform is your computer?

Microsoft Windows NT 10.0.22631.0 x64

What steps can reproduce the bug?

Run this code with the mineflayer package installed and you get an error when you set the auth parameter to 'microsoft' like in the code below.

const { createBot } = require('mineflayer')

const bot = createBot({
    username: 'Bot',
    host: 'localhost',
    auth: 'microsoft',
    version: '1.19.4',
    port: 25565
})

The code doesn't work because somewhere down the line the code depends on the nodejs crypto module to run this function:

crypto.generateKeyPairSync('ec', { namedCurve: 'P-256' })

The function doesn't return the correct type though.

What is the expected behavior?

The code is supposed to prompt me for a microsoft login and when that is done, the bot should join the microsoft server I setup on my pc that is open on port 25565

What do you see instead?

PS C:\Users\Link\Downloads\mc> bun run index.js
 7 |         return {
 8 |             kty: 'oct',
 9 |             k: base64url(key),
10 |         };
11 |     }
12 |     if (!isCryptoKey(key)) {
                            ^
TypeError: Key must be one of type CryptoKey or Uint8Array. Received an instance of KeyObject
      at C:\Users\Link\Downloads\mc\node_modules\jose\dist\browser\runtime\key_to_jwk.js:12:24
      at keyToJWK (C:\Users\Link\Downloads\mc\node_modules\jose\dist\browser\runtime\key_to_jwk.js:5:25)
      at exportJWK (C:\Users\Link\Downloads\mc\node_modules\jose\dist\browser\key\export.js:10:33)
      at new XboxTokenManager (C:\Users\Link\Downloads\mc\node_modules\prismarine-auth\src\TokenManagers\XboxTokenManager.js:25:5)
      at initTokenManagers (C:\Users\Link\Downloads\mc\node_modules\prismarine-auth\src\MicrosoftAuthFlow.js:72:20)
      at new MicrosoftAuthFlow (C:\Users\Link\Downloads\mc\node_modules\prismarine-auth\src\MicrosoftAuthFlow.js:35:10)
      at C:\Users\Link\Downloads\mc\node_modules\minecraft-protocol\src\client\microsoftAuth.js:21:47
      at authenticate (C:\Users\Link\Downloads\mc\node_modules\minecraft-protocol\src\client\microsoftAuth.js:18:30)
      at createClient (C:\Users\Link\Downloads\mc\node_modules\minecraft-protocol\src\createClient.js:52:25)
      at createBot (C:\Users\Link\Downloads\mc\node_modules\mineflayer\lib\loader.js:98:35)

Additional information

Mineflayer is a library that can be used to make bots for the game Minecraft. A link to Mineflayers repo can be found here: https://github.com/PrismarineJS/mineflayer

link-discord avatar Apr 07 '24 19:04 link-discord

Sorry I noticed the generateKeyPairSync function does actually exist now, but it doesn't change the issue.

link-discord avatar Apr 08 '24 04:04 link-discord

This is due to an incompatibility with the library "jose" and bun's KeyObject, mineflayer or prismarineJS are not at fault.

GustavoWidman avatar Apr 08 '24 16:04 GustavoWidman

This is due to an incompatibility with the library "jose" and bun's KeyObject, mineflayer or prismarineJS are not at fault.

Although this can be fixed on the prismarine-auth side by adding a compat layer like so:

prismarine-auth/src/TokenManagers/XboxTokenManager.js

change constructor of class XboxTokenManager to this

constructor (ecKey, cache) {
    this.key = ecKey

    importSPKI(ecKey.publicKey.export({ type: 'spki', format: 'pem' }), 'ES256', { extractable: true }).then(pubKey => {
      exportJWK(pubKey).then(jwk => {
        this.jwk = { ...jwk, alg: 'ES256', use: 'sig' }
      })
    })

    this.cache = cache

    this.headers = { 'Cache-Control': 'no-store, must-revalidate, no-cache', 'x-xbl-contract-version': 1 }
  }

and import the "importSPKI" function at the top of the file with the "exportJWK"

const { exportJWK, importSPKI } = require('jose')

GustavoWidman avatar Apr 08 '24 17:04 GustavoWidman