From e079e9b0f6b5e01388ad3764403de4b9d511db78 Mon Sep 17 00:00:00 2001 From: Jordan Jones Date: Fri, 29 Jul 2022 09:03:20 -0700 Subject: [PATCH] Use offline mode as default authentication, fallback to offline mode if invalid option. (#998) * Default n-m-p to use offline unless specified. * Remove expression * actually make offline default * Revert examples * Revert all example changes * Add deprecation warning that mojang servers are no longer accepting mojang auth tokens. --- docs/API.md | 2 +- docs/README.md | 4 ++-- src/client/{auth.js => mojangAuth.js} | 2 +- src/createClient.js | 19 ++++++++++++++----- src/index.d.ts | 2 +- test/clientTest.js | 15 ++++++++++----- 6 files changed, 29 insertions(+), 15 deletions(-) rename src/client/{auth.js => mojangAuth.js} (98%) diff --git a/docs/API.md b/docs/API.md index 091e15f..29abce3 100644 --- a/docs/API.md +++ b/docs/API.md @@ -93,7 +93,7 @@ Returns a `Client` instance and perform login. `options` is an object containing the properties : * username * port : default to 25565 - * auth : the type of account to use, either `microsoft` or `mojang`. default to 'mojang' + * auth : the type of account to use, either `microsoft`, `mojang`, or `offline`. default to 'offline' * password : can be omitted * (microsoft account) leave this blank to use device code auth. If you provide a password, we try to do username and password auth, but this does not always work. diff --git a/docs/README.md b/docs/README.md index c73c8fa..3b91205 100644 --- a/docs/README.md +++ b/docs/README.md @@ -78,7 +78,7 @@ var client = mc.createClient({ port: 25565, // optional username: "email@example.com", password: "12345678", - auth: 'mojang' // optional; by default uses mojang, if using a microsoft account, set to 'microsoft' + auth: 'microsoft' // optional; by default uses offline mode, if using a microsoft account, set to 'microsoft' }); client.on('chat', function(packet) { // Listen for chat messages and echo them back. @@ -92,7 +92,7 @@ client.on('chat', function(packet) { }); ``` -If the server is in offline mode, you may leave out the `password` option. +If the server is in offline mode, you may leave out the `password` option and switch auth to `offline`. You can also leave out `password` when using a Microsoft account. If provided, password based auth will be attempted first which may fail. *Note:* if using a Microsoft account, your account age must be >= 18 years old. ### Hello World server example diff --git a/src/client/auth.js b/src/client/mojangAuth.js similarity index 98% rename from src/client/auth.js rename to src/client/mojangAuth.js index ce74c72..ea1a6d8 100644 --- a/src/client/auth.js +++ b/src/client/mojangAuth.js @@ -82,7 +82,7 @@ module.exports = async function (client, options) { remoteId: oldProfileObj?.remoteId ?? '', username: options.username, localId: profile, - type: (options.auth?.toLowerCase() === 'microsoft' ? 'Xbox' : 'Mojang'), + type: (options.auth?.toLowerCase() === 'mojang' ? 'Mojang' : 'Xbox'), persistent: true } auths.accounts[profile] = newProfileObj diff --git a/src/createClient.js b/src/createClient.js index ffdc5ed..8a3ac2e 100644 --- a/src/createClient.js +++ b/src/createClient.js @@ -6,7 +6,7 @@ const assert = require('assert') const encrypt = require('./client/encrypt') const keepalive = require('./client/keepalive') const compress = require('./client/compress') -const auth = require('./client/auth') +const auth = require('./client/mojangAuth') const microsoftAuth = require('./client/microsoftAuth') const setProtocol = require('./client/setProtocol') const play = require('./client/play') @@ -34,10 +34,19 @@ function createClient (options) { const client = new Client(false, version.minecraftVersion, options.customPackets, hideErrors) tcpDns(client, options) - if (options.auth === 'microsoft') { - microsoftAuth.authenticate(client, options) - } else { - auth(client, options) + switch (options.auth) { + case 'mojang': + console.warn('[deprecated] mojang auth servers no longer accept mojang accounts to login. convert your account.\nhttps://help.minecraft.net/hc/en-us/articles/4403181904525-How-to-Migrate-Your-Mojang-Account-to-a-Microsoft-Account') + auth(client, options) + break + case 'microsoft': + microsoftAuth.authenticate(client, options) + break + case 'offline': + default: + client.username = options.username + options.connect(client) + break } if (options.version === false) autoVersion(client, options) setProtocol(client, options) diff --git a/src/index.d.ts b/src/index.d.ts index ac76a1d..17bb00b 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -101,7 +101,7 @@ declare module 'minecraft-protocol' { export interface ClientOptions { username: string port?: number - auth?: 'mojang' | 'microsoft' + auth?: 'mojang' | 'microsoft' | 'offline' password?: string host?: string clientToken?: string diff --git a/test/clientTest.js b/test/clientTest.js index 079b6e6..b9c219e 100644 --- a/test/clientTest.js +++ b/test/clientTest.js @@ -100,7 +100,8 @@ for (const supportedVersion of mc.supportedVersions) { const client = mc.createClient({ username: 'Player', version: version.minecraftVersion, - port: PORT + port: PORT, + auth: 'offline' }) client.on('error', err => done(err)) const lineListener = function (line) { @@ -149,7 +150,8 @@ for (const supportedVersion of mc.supportedVersions) { const client = mc.createClient({ username: 'Player', version: version.minecraftVersion, - port: PORT + port: PORT, + auth: 'offline' }) client.on('error', err => done(err)) client.on('login', function () { @@ -167,7 +169,8 @@ for (const supportedVersion of mc.supportedVersions) { const client = mc.createClient({ username: 'Player', version: version.minecraftVersion === '1.8.8' ? '1.11.2' : '1.8.8', - port: PORT + port: PORT, + auth: 'offline' }) client.once('error', function (err) { if (err.message.startsWith('This server is version')) { @@ -215,7 +218,8 @@ for (const supportedVersion of mc.supportedVersions) { username: process.env.MC_USERNAME, password: process.env.MC_PASSWORD, version: version.minecraftVersion, - port: PORT + port: PORT, + auth: 'offline' }) client.on('error', err => done(err)) const lineListener = function (line) { @@ -252,7 +256,8 @@ for (const supportedVersion of mc.supportedVersions) { const client = mc.createClient({ username: 'Player', version: version.minecraftVersion, - port: PORT + port: PORT, + auth: 'offline' }) client.on('error', err => done(err)) let gotKicked = false