2020-05-29 05:39:13 +03:00
|
|
|
const request = require('request')
|
2022-03-02 05:52:06 +03:00
|
|
|
const { v3 } = require('uuid')
|
|
|
|
|
|
|
|
let uuid
|
2020-05-29 05:39:13 +03:00
|
|
|
let api_url = 'https://authserver.mojang.com'
|
2018-10-30 01:13:58 +03:00
|
|
|
|
2021-08-08 23:05:50 +03:00
|
|
|
module.exports.getAuth = function (username, password, client_token = null) {
|
2020-05-29 05:39:13 +03:00
|
|
|
return new Promise((resolve, reject) => {
|
2022-03-02 05:52:06 +03:00
|
|
|
getUUID(username)
|
2020-05-29 05:39:13 +03:00
|
|
|
if (!password) {
|
|
|
|
const user = {
|
2022-03-02 05:52:06 +03:00
|
|
|
access_token: uuid,
|
|
|
|
client_token: client_token || uuid,
|
|
|
|
uuid,
|
2020-05-29 05:39:13 +03:00
|
|
|
name: username,
|
2021-05-05 20:48:52 +03:00
|
|
|
user_properties: '{}'
|
2020-05-29 05:39:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return resolve(user)
|
|
|
|
}
|
|
|
|
|
|
|
|
const requestObject = {
|
|
|
|
url: api_url + '/authenticate',
|
|
|
|
json: {
|
|
|
|
agent: {
|
|
|
|
name: 'Minecraft',
|
|
|
|
version: 1
|
|
|
|
},
|
2022-03-02 05:52:06 +03:00
|
|
|
username,
|
|
|
|
password,
|
|
|
|
clientToken: uuid,
|
2020-05-29 05:39:13 +03:00
|
|
|
requestUser: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
request.post(requestObject, function (error, response, body) {
|
|
|
|
if (error) return reject(error)
|
|
|
|
if (!body || !body.selectedProfile) {
|
|
|
|
return reject(new Error('Validation error: ' + response.statusMessage))
|
|
|
|
}
|
|
|
|
|
|
|
|
const userProfile = {
|
|
|
|
access_token: body.accessToken,
|
|
|
|
client_token: body.clientToken,
|
|
|
|
uuid: body.selectedProfile.id,
|
|
|
|
name: body.selectedProfile.name,
|
|
|
|
selected_profile: body.selectedProfile,
|
2021-05-05 20:48:52 +03:00
|
|
|
user_properties: parsePropts(body.user.properties)
|
2020-05-29 05:39:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
resolve(userProfile)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2018-10-30 01:13:58 +03:00
|
|
|
|
2022-03-02 05:52:06 +03:00
|
|
|
module.exports.validate = function (accessToken, clientToken) {
|
2020-05-29 05:39:13 +03:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const requestObject = {
|
|
|
|
url: api_url + '/validate',
|
|
|
|
json: {
|
2022-03-02 05:52:06 +03:00
|
|
|
accessToken,
|
|
|
|
clientToken
|
2020-05-29 05:39:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
request.post(requestObject, async function (error, response, body) {
|
|
|
|
if (error) return reject(error)
|
|
|
|
|
|
|
|
if (!body) resolve(true)
|
|
|
|
else reject(body)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2018-12-01 03:33:08 +03:00
|
|
|
|
2021-08-08 23:05:50 +03:00
|
|
|
module.exports.refreshAuth = function (accessToken, clientToken) {
|
2020-05-29 05:39:13 +03:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const requestObject = {
|
|
|
|
url: api_url + '/refresh',
|
|
|
|
json: {
|
2022-03-02 05:52:06 +03:00
|
|
|
accessToken,
|
|
|
|
clientToken,
|
2020-05-29 05:39:13 +03:00
|
|
|
requestUser: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
request.post(requestObject, function (error, response, body) {
|
|
|
|
if (error) return reject(error)
|
|
|
|
if (!body || !body.selectedProfile) {
|
|
|
|
return reject(new Error('Validation error: ' + response.statusMessage))
|
|
|
|
}
|
|
|
|
|
|
|
|
const userProfile = {
|
|
|
|
access_token: body.accessToken,
|
2022-03-02 05:52:06 +03:00
|
|
|
client_token: getUUID(body.selectedProfile.name),
|
2020-05-29 05:39:13 +03:00
|
|
|
uuid: body.selectedProfile.id,
|
|
|
|
name: body.selectedProfile.name,
|
2021-05-05 20:48:52 +03:00
|
|
|
user_properties: parsePropts(body.user.properties)
|
2020-05-29 05:39:13 +03:00
|
|
|
}
|
|
|
|
|
2022-03-02 05:52:06 +03:00
|
|
|
return resolve(userProfile)
|
2020-05-29 05:39:13 +03:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2019-05-23 01:28:48 +03:00
|
|
|
|
2019-08-27 21:07:33 +03:00
|
|
|
module.exports.invalidate = function (accessToken, clientToken) {
|
2020-05-29 05:39:13 +03:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const requestObject = {
|
|
|
|
url: api_url + '/invalidate',
|
|
|
|
json: {
|
2022-03-02 05:52:06 +03:00
|
|
|
accessToken,
|
|
|
|
clientToken
|
2020-05-29 05:39:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
request.post(requestObject, function (error, response, body) {
|
|
|
|
if (error) return reject(error)
|
|
|
|
|
2022-03-02 05:52:06 +03:00
|
|
|
if (!body) return resolve(true)
|
|
|
|
else return reject(body)
|
2020-05-29 05:39:13 +03:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2019-05-23 01:28:48 +03:00
|
|
|
|
2019-08-27 21:07:33 +03:00
|
|
|
module.exports.signOut = function (username, password) {
|
2020-05-29 05:39:13 +03:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const requestObject = {
|
|
|
|
url: api_url + '/signout',
|
|
|
|
json: {
|
2022-03-02 05:52:06 +03:00
|
|
|
username,
|
|
|
|
password
|
2020-05-29 05:39:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
request.post(requestObject, function (error, response, body) {
|
|
|
|
if (error) return reject(error)
|
|
|
|
|
2022-03-02 05:52:06 +03:00
|
|
|
if (!body) return resolve(true)
|
|
|
|
else return reject(body)
|
2020-05-29 05:39:13 +03:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports.changeApiUrl = function (url) {
|
|
|
|
api_url = url
|
|
|
|
}
|
2021-05-05 20:48:52 +03:00
|
|
|
|
|
|
|
function parsePropts (array) {
|
|
|
|
if (array) {
|
|
|
|
const newObj = {}
|
|
|
|
for (const entry of array) {
|
|
|
|
if (newObj[entry.name]) {
|
|
|
|
newObj[entry.name].push(entry.value)
|
|
|
|
} else {
|
|
|
|
newObj[entry.name] = [entry.value]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return JSON.stringify(newObj)
|
|
|
|
} else {
|
|
|
|
return '{}'
|
|
|
|
}
|
|
|
|
}
|
2022-03-02 05:52:06 +03:00
|
|
|
|
|
|
|
function getUUID (value) {
|
|
|
|
if (!uuid) {
|
|
|
|
uuid = v3(value, v3.DNS)
|
|
|
|
}
|
|
|
|
return uuid
|
|
|
|
}
|