2018-04-14 19:43:58 -07:00
|
|
|
/**
|
|
|
|
* Mojang
|
|
|
|
*
|
|
|
|
* This module serves as a minimal wrapper for Mojang's REST api.
|
|
|
|
*
|
|
|
|
* @module mojang
|
|
|
|
*/
|
|
|
|
// Requirements
|
2017-12-03 04:05:13 -08:00
|
|
|
const request = require('request')
|
2018-08-22 11:21:49 -07:00
|
|
|
const logger = require('./loggerutil')('%c[Mojang]', 'color: #a02d2a; font-weight: bold')
|
2017-12-03 04:05:13 -08:00
|
|
|
|
2018-04-14 19:43:58 -07:00
|
|
|
// Constants
|
2017-12-03 04:05:13 -08:00
|
|
|
const minecraftAgent = {
|
|
|
|
name: 'Minecraft',
|
|
|
|
version: 1
|
|
|
|
}
|
|
|
|
const authpath = 'https://authserver.mojang.com'
|
|
|
|
const statuses = [
|
|
|
|
{
|
2018-05-17 00:11:44 -07:00
|
|
|
service: 'sessionserver.mojang.com',
|
2017-12-03 04:05:13 -08:00
|
|
|
status: 'grey',
|
2018-05-17 00:11:44 -07:00
|
|
|
name: 'Multiplayer Session Service',
|
|
|
|
essential: true
|
2017-12-03 04:05:13 -08:00
|
|
|
},
|
|
|
|
{
|
2018-05-17 00:11:44 -07:00
|
|
|
service: 'authserver.mojang.com',
|
2017-12-03 04:05:13 -08:00
|
|
|
status: 'grey',
|
2018-05-17 00:11:44 -07:00
|
|
|
name: 'Authentication Service',
|
|
|
|
essential: true
|
2017-12-03 04:05:13 -08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
service: 'textures.minecraft.net',
|
|
|
|
status: 'grey',
|
2018-05-17 00:11:44 -07:00
|
|
|
name: 'Minecraft Skins',
|
|
|
|
essential: false
|
2017-12-03 04:05:13 -08:00
|
|
|
},
|
|
|
|
{
|
2018-05-17 00:11:44 -07:00
|
|
|
service: 'api.mojang.com',
|
2017-12-03 04:05:13 -08:00
|
|
|
status: 'grey',
|
2018-05-17 00:11:44 -07:00
|
|
|
name: 'Public API',
|
|
|
|
essential: false
|
2017-12-03 04:05:13 -08:00
|
|
|
},
|
|
|
|
{
|
2018-05-17 00:11:44 -07:00
|
|
|
service: 'minecraft.net',
|
2017-12-03 04:05:13 -08:00
|
|
|
status: 'grey',
|
2018-05-17 00:11:44 -07:00
|
|
|
name: 'Minecraft.net',
|
|
|
|
essential: false
|
2017-12-03 04:05:13 -08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
service: 'account.mojang.com',
|
|
|
|
status: 'grey',
|
2018-05-17 00:11:44 -07:00
|
|
|
name: 'Mojang Accounts Website',
|
|
|
|
essential: false
|
2017-12-03 04:05:13 -08:00
|
|
|
}
|
|
|
|
]
|
|
|
|
|
2018-04-14 19:43:58 -07:00
|
|
|
// Functions
|
|
|
|
|
2017-12-03 04:05:13 -08:00
|
|
|
/**
|
|
|
|
* Converts a Mojang status color to a hex value. Valid statuses
|
|
|
|
* are 'green', 'yellow', 'red', and 'grey'. Grey is a custom status
|
2018-03-31 10:46:52 -07:00
|
|
|
* to our project which represents an unknown status.
|
2017-12-03 04:05:13 -08:00
|
|
|
*
|
2018-03-28 13:42:10 -07:00
|
|
|
* @param {string} status A valid status code.
|
|
|
|
* @returns {string} The hex color of the status code.
|
2017-12-03 04:05:13 -08:00
|
|
|
*/
|
|
|
|
exports.statusToHex = function(status){
|
|
|
|
switch(status.toLowerCase()){
|
|
|
|
case 'green':
|
|
|
|
return '#a5c325'
|
|
|
|
case 'yellow':
|
|
|
|
return '#eac918'
|
|
|
|
case 'red':
|
|
|
|
return '#c32625'
|
|
|
|
case 'grey':
|
|
|
|
default:
|
|
|
|
return '#848484'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves the status of Mojang's services.
|
|
|
|
* The response is condensed into a single object. Each service is
|
|
|
|
* a key, where the value is an object containing a status and name
|
|
|
|
* property.
|
|
|
|
*
|
|
|
|
* @see http://wiki.vg/Mojang_API#API_Status
|
|
|
|
*/
|
|
|
|
exports.status = function(){
|
2018-05-07 15:15:59 -07:00
|
|
|
return new Promise((resolve, reject) => {
|
2017-12-03 04:05:13 -08:00
|
|
|
request.get('https://status.mojang.com/check',
|
2018-07-22 10:31:15 -07:00
|
|
|
{
|
|
|
|
json: true,
|
|
|
|
timeout: 2500
|
|
|
|
},
|
|
|
|
function(error, response, body){
|
2018-04-14 15:42:45 -07:00
|
|
|
|
2018-07-22 10:31:15 -07:00
|
|
|
if(error || response.statusCode !== 200){
|
2018-08-22 11:21:49 -07:00
|
|
|
logger.warn('Unable to retrieve Mojang status.')
|
|
|
|
logger.debug('Error while retrieving Mojang statuses:', error)
|
2018-07-22 10:31:15 -07:00
|
|
|
//reject(error || response.statusCode)
|
|
|
|
for(let i=0; i<statuses.length; i++){
|
|
|
|
statuses[i].status = 'grey'
|
|
|
|
}
|
|
|
|
resolve(statuses)
|
|
|
|
} else {
|
|
|
|
for(let i=0; i<body.length; i++){
|
|
|
|
const key = Object.keys(body[i])[0]
|
|
|
|
inner:
|
|
|
|
for(let j=0; j<statuses.length; j++){
|
|
|
|
if(statuses[j].service === key) {
|
|
|
|
statuses[j].status = body[i][key]
|
|
|
|
break inner
|
|
|
|
}
|
2017-12-03 04:05:13 -08:00
|
|
|
}
|
|
|
|
}
|
2018-07-22 10:31:15 -07:00
|
|
|
resolve(statuses)
|
2017-12-03 04:05:13 -08:00
|
|
|
}
|
2018-07-22 10:31:15 -07:00
|
|
|
})
|
2017-12-03 04:05:13 -08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Authenticate a user with their Mojang credentials.
|
|
|
|
*
|
2018-03-28 13:42:10 -07:00
|
|
|
* @param {string} username The user's username, this is often an email.
|
|
|
|
* @param {string} password The user's password.
|
|
|
|
* @param {string} clientToken The launcher's Client Token.
|
|
|
|
* @param {boolean} requestUser Optional. Adds user object to the reponse.
|
|
|
|
* @param {Object} agent Optional. Provided by default. Adds user info to the response.
|
2017-12-03 04:05:13 -08:00
|
|
|
*
|
|
|
|
* @see http://wiki.vg/Authentication#Authenticate
|
|
|
|
*/
|
|
|
|
exports.authenticate = function(username, password, clientToken, requestUser = true, agent = minecraftAgent){
|
2018-05-07 15:15:59 -07:00
|
|
|
return new Promise((resolve, reject) => {
|
2017-12-03 04:05:13 -08:00
|
|
|
request.post(authpath + '/authenticate',
|
2018-07-22 10:31:15 -07:00
|
|
|
{
|
|
|
|
json: true,
|
|
|
|
body: {
|
|
|
|
agent,
|
|
|
|
username,
|
|
|
|
password,
|
|
|
|
clientToken,
|
|
|
|
requestUser
|
|
|
|
}
|
|
|
|
},
|
|
|
|
function(error, response, body){
|
|
|
|
if(error){
|
2018-08-22 11:21:49 -07:00
|
|
|
logger.error('Error during authentication.', error)
|
2018-07-22 10:31:15 -07:00
|
|
|
reject(error)
|
2018-04-14 15:42:45 -07:00
|
|
|
} else {
|
2018-07-22 10:31:15 -07:00
|
|
|
if(response.statusCode === 200){
|
|
|
|
resolve(body)
|
|
|
|
} else {
|
|
|
|
reject(body || {code: 'ENOTFOUND'})
|
|
|
|
}
|
2018-04-14 15:42:45 -07:00
|
|
|
}
|
2018-07-22 10:31:15 -07:00
|
|
|
})
|
2017-12-03 04:05:13 -08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validate an access token. This should always be done before launching.
|
|
|
|
* The client token should match the one used to create the access token.
|
|
|
|
*
|
2018-03-28 13:42:10 -07:00
|
|
|
* @param {string} accessToken The access token to validate.
|
|
|
|
* @param {string} clientToken The launcher's client token.
|
2017-12-03 04:05:13 -08:00
|
|
|
*
|
|
|
|
* @see http://wiki.vg/Authentication#Validate
|
|
|
|
*/
|
|
|
|
exports.validate = function(accessToken, clientToken){
|
2018-05-07 15:15:59 -07:00
|
|
|
return new Promise((resolve, reject) => {
|
2017-12-03 04:05:13 -08:00
|
|
|
request.post(authpath + '/validate',
|
2018-07-22 10:31:15 -07:00
|
|
|
{
|
|
|
|
json: true,
|
|
|
|
body: {
|
|
|
|
accessToken,
|
|
|
|
clientToken
|
|
|
|
}
|
|
|
|
},
|
|
|
|
function(error, response, body){
|
|
|
|
if(error){
|
2018-08-22 11:21:49 -07:00
|
|
|
logger.error('Error during validation.', error)
|
2018-07-22 10:31:15 -07:00
|
|
|
reject(error)
|
2018-04-14 15:42:45 -07:00
|
|
|
} else {
|
2018-07-22 10:31:15 -07:00
|
|
|
if(response.statusCode === 403){
|
|
|
|
resolve(false)
|
|
|
|
} else {
|
2018-04-14 15:42:45 -07:00
|
|
|
// 204 if valid
|
2018-07-22 10:31:15 -07:00
|
|
|
resolve(true)
|
|
|
|
}
|
2018-04-14 15:42:45 -07:00
|
|
|
}
|
2018-07-22 10:31:15 -07:00
|
|
|
})
|
2017-12-03 04:05:13 -08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Invalidates an access token. The clientToken must match the
|
|
|
|
* token used to create the provided accessToken.
|
|
|
|
*
|
2018-03-28 13:42:10 -07:00
|
|
|
* @param {string} accessToken The access token to invalidate.
|
|
|
|
* @param {string} clientToken The launcher's client token.
|
2017-12-03 04:05:13 -08:00
|
|
|
*
|
|
|
|
* @see http://wiki.vg/Authentication#Invalidate
|
|
|
|
*/
|
|
|
|
exports.invalidate = function(accessToken, clientToken){
|
2018-05-07 15:15:59 -07:00
|
|
|
return new Promise((resolve, reject) => {
|
2017-12-03 04:05:13 -08:00
|
|
|
request.post(authpath + '/invalidate',
|
2018-07-22 10:31:15 -07:00
|
|
|
{
|
|
|
|
json: true,
|
|
|
|
body: {
|
|
|
|
accessToken,
|
|
|
|
clientToken
|
|
|
|
}
|
|
|
|
},
|
|
|
|
function(error, response, body){
|
|
|
|
if(error){
|
2018-08-22 11:21:49 -07:00
|
|
|
logger.error('Error during invalidation.', error)
|
2018-07-22 10:31:15 -07:00
|
|
|
reject(error)
|
2018-04-14 15:42:45 -07:00
|
|
|
} else {
|
2018-07-22 10:31:15 -07:00
|
|
|
if(response.statusCode === 204){
|
|
|
|
resolve()
|
|
|
|
} else {
|
|
|
|
reject(body)
|
|
|
|
}
|
2018-04-14 15:42:45 -07:00
|
|
|
}
|
2018-07-22 10:31:15 -07:00
|
|
|
})
|
2017-12-03 04:05:13 -08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Refresh a user's authentication. This should be used to keep a user logged
|
|
|
|
* in without asking them for their credentials again. A new access token will
|
|
|
|
* be generated using a recent invalid access token. See Wiki for more info.
|
|
|
|
*
|
2018-03-28 13:42:10 -07:00
|
|
|
* @param {string} accessToken The old access token.
|
|
|
|
* @param {string} clientToken The launcher's client token.
|
|
|
|
* @param {boolean} requestUser Optional. Adds user object to the reponse.
|
2017-12-03 04:05:13 -08:00
|
|
|
*
|
|
|
|
* @see http://wiki.vg/Authentication#Refresh
|
|
|
|
*/
|
|
|
|
exports.refresh = function(accessToken, clientToken, requestUser = true){
|
2018-05-07 15:15:59 -07:00
|
|
|
return new Promise((resolve, reject) => {
|
2017-12-03 04:05:13 -08:00
|
|
|
request.post(authpath + '/refresh',
|
2018-07-22 10:31:15 -07:00
|
|
|
{
|
|
|
|
json: true,
|
|
|
|
body: {
|
|
|
|
accessToken,
|
|
|
|
clientToken,
|
|
|
|
requestUser
|
|
|
|
}
|
|
|
|
},
|
|
|
|
function(error, response, body){
|
|
|
|
if(error){
|
2018-08-22 11:21:49 -07:00
|
|
|
logger.error('Error during refresh.', error)
|
2018-07-22 10:31:15 -07:00
|
|
|
reject(error)
|
2018-04-14 15:42:45 -07:00
|
|
|
} else {
|
2018-07-22 10:31:15 -07:00
|
|
|
if(response.statusCode === 200){
|
|
|
|
resolve(body)
|
|
|
|
} else {
|
|
|
|
reject(body)
|
|
|
|
}
|
2018-04-14 15:42:45 -07:00
|
|
|
}
|
2018-07-22 10:31:15 -07:00
|
|
|
})
|
2017-12-03 04:05:13 -08:00
|
|
|
})
|
|
|
|
}
|