Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add token authentication #780

Merged
merged 20 commits into from Dec 5, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 29 additions & 0 deletions examples/client_token_auth/client_token_auth.js
@@ -0,0 +1,29 @@
const mc = require('minecraft-protocol')

if (process.argv.length < 4 || process.argv.length > 6) {
console.log('Usage : node echo.js <host> <port> [<name>] [<profiles folder>]')
process.exit(1)
}

const client = mc.createClient({
host: process.argv[2],
port: parseInt(process.argv[3]),
username: process.argv[4] ? process.argv[4] : 'echo',
profilesFolder: process.argv[5]
})

client.on('error', function (err) {
console.log(err)
})
client.on('session', function (session) {
console.info('session initialized ', JSON.stringify(session))
})
client.on('connected', function () {
console.info('connected')
})
client.on('disconnect', function (packet) {
console.log('disconnected: ' + packet.reason)
})
client.on('end', function () {
console.log('Connection lost')
})
8 changes: 8 additions & 0 deletions examples/client_token_auth/package.json
@@ -0,0 +1,8 @@
{
"name": "node-minecraft-protocol-example",
"version": "0.0.0",
"private": true,
"dependencies": {
},
"description": "A node-minecraft-protocol example"
}
7 changes: 5 additions & 2 deletions package.json
Expand Up @@ -44,17 +44,20 @@
},
"dependencies": {
"aes-js": "^3.1.2",
"yggdrasil": "^1.3.0",
"buffer-equal": "^1.0.0",
"debug": "^4.1.0",
"endian-toggle": "^0.0.0",
"es6-promisify": "^5.0.0",
"lodash.get": "^4.1.2",
"lodash.merge": "^4.3.0",
"minecraft-data": "^2.70.0",
"mkdirp": "^0.5.1",
"mz": "^2.7.0",
"node-rsa": "^0.4.2",
"prismarine-nbt": "^1.3.0",
"protodef": "^1.8.0",
"readable-stream": "^3.0.6",
"uuid-1345": "^1.0.1"
"uuid-1345": "^1.0.1",
"yggdrasil": "^1.3.0"
}
}
91 changes: 86 additions & 5 deletions src/client/auth.js
@@ -1,16 +1,67 @@
const UUID = require('uuid-1345')
const yggdrasil = require('yggdrasil')
const fs = require('mz/fs')
stenlan marked this conversation as resolved.
Show resolved Hide resolved
const promisify = require('es6-promisify')
stenlan marked this conversation as resolved.
Show resolved Hide resolved
const mkdirp = promisify(require('mkdirp'))
stenlan marked this conversation as resolved.
Show resolved Hide resolved

module.exports = function (client, options) {
module.exports = async function (client, options) {
stenlan marked this conversation as resolved.
Show resolved Hide resolved
const yggdrasilClient = yggdrasil({ agent: options.agent, host: options.authServer || 'https://authserver.mojang.com' })
const clientToken = options.clientToken || (options.session && options.session.clientToken) || UUID.v4().toString()
const clientToken = (options.profilesFolder && (await getLP()).clientToken) || options.clientToken || (options.session && options.session.clientToken) || UUID.v4().toString().replace("-", "")
const skipValidation = false || options.skipValidation
options.accessToken = null
options.haveCredentials = options.password != null || (clientToken != null && options.session != null)
options.haveCredentials = options.password != null || (clientToken != null && options.session != null) || options.profilesFolder != null

async function getLP () { // get launcher profile
stenlan marked this conversation as resolved.
Show resolved Hide resolved
let data
try {
data = JSON.parse(await fs.readFile(options.profilesFolder + '/launcher_profiles.json', 'utf8'))
} catch (err) {
await mkdirp(options.profilesFolder)
await fs.writeFile(options.profilesFolder + '/launcher_profiles.json', '')
stenlan marked this conversation as resolved.
Show resolved Hide resolved
data = { authenticationDatabase: {} }
}
return data
stenlan marked this conversation as resolved.
Show resolved Hide resolved
}

if (options.haveCredentials) {
// make a request to get the case-correct username before connecting.
const cb = function (err, session) {
const cb = async function (err, session) {
stenlan marked this conversation as resolved.
Show resolved Hide resolved
if (options.profilesFolder) {
let auths = await getLP()
let lowerUsername = options.username.toLowerCase()
let profile = Object.keys(auths.authenticationDatabase).find(key =>
auths.authenticationDatabase[key].username.toLowerCase() == lowerUsername
|| Object.values(auths.authenticationDatabase[key].profiles)[0].displayName.toLowerCase() == lowerUsername
stenlan marked this conversation as resolved.
Show resolved Hide resolved
)
if (err) {
if (profile) { // profile is invalid, remove
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we sure that the profile is invalid here ? We should check the error since a lot of kind of error can happen here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have thought about this, and it should be fine like this IMO. The minecraft launcher itself does the same thing I think, if your login doesn't succeed for whatever reason, it invalidates it

delete auth.authenticationDatabase[profile]
}
} else { // successful login
if (!profile) {
profile = UUID.v4().toString() // create new profile
}
if (!auths.clientToken) {
auths.clientToken = clientToken
}

if (clientToken == auths.clientToken) { // only do something when we can save a new clienttoken or they match
let oldProfileObj = auths.authenticationDatabase[profile]
let newProfileObj = {
accessToken: session.accessToken,
profiles: {},
properties: oldProfileObj ? (oldProfileObj.properties || []) : [],
username: options.username
}
newProfileObj.profiles[session.selectedProfile.id] = {
displayName: session.selectedProfile.name
}
auths.authenticationDatabase[profile] = newProfileObj
}
}
await fs.writeFile(options.profilesFolder + '/launcher_profiles.json', JSON.stringify(auths, null, 2))
}

if (err) {
client.emit('error', err)
} else {
Expand All @@ -22,6 +73,34 @@ module.exports = function (client, options) {
}
}

if (!options.session && options.profilesFolder) {
let auths = await getLP()

let lowerUsername = options.username.toLowerCase()
let profile = Object.keys(auths.authenticationDatabase).find(key =>
auths.authenticationDatabase[key].username.toLowerCase() == lowerUsername
|| Object.values(auths.authenticationDatabase[key].profiles)[0].displayName.toLowerCase() == lowerUsername
)

if (profile) {
let newUsername = auths.authenticationDatabase[profile].username
let uuid = Object.keys(auths.authenticationDatabase[profile].profiles)[0]
let displayName = auths.authenticationDatabase[profile].profiles[uuid].displayName
let newProfile = {
name: displayName,
id: uuid
};

options.session = {
accessToken: auths.authenticationDatabase[profile].accessToken,
clientToken: auths.clientToken,
selectedProfile: newProfile,
availableProfiles: [newProfile]
}
options.username = newUsername
stenlan marked this conversation as resolved.
Show resolved Hide resolved
}
}

if (options.session) {
if (!skipValidation) {
yggdrasilClient.validate(options.session.accessToken, function (err) {
Expand All @@ -33,7 +112,8 @@ module.exports = function (client, options) {
yggdrasilClient.auth({
user: options.username,
pass: options.password,
token: clientToken
token: clientToken,
requestUser: true
}, cb)
} else {
cb(err, data)
Expand All @@ -52,6 +132,7 @@ module.exports = function (client, options) {
token: clientToken
}, cb)
}

} else {
// assume the server is in offline mode and just go for it.
client.username = options.username
Expand Down