2018-10-29 18:13:58 -04:00
|
|
|
const request = require('request');
|
|
|
|
const uuid = require('uuid/v1');
|
|
|
|
const api_url = "https://authserver.mojang.com";
|
|
|
|
|
|
|
|
|
2018-11-29 21:41:07 -05:00
|
|
|
function getAuth(username, password) {
|
2018-10-29 18:13:58 -04:00
|
|
|
return new Promise(resolve => {
|
2018-11-29 21:41:07 -05:00
|
|
|
if(!password) {
|
|
|
|
const user = {
|
|
|
|
access_token: uuid(),
|
|
|
|
client_token: uuid(),
|
|
|
|
uuid: uuid(),
|
|
|
|
name: username,
|
2019-02-08 16:59:27 -05:00
|
|
|
user_properties: JSON.stringify({})
|
2018-11-29 21:41:07 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
resolve(user);
|
2019-01-12 20:51:24 -05:00
|
|
|
return;
|
2018-11-29 21:41:07 -05:00
|
|
|
}
|
|
|
|
|
2018-10-29 18:13:58 -04:00
|
|
|
const requestObject = {
|
|
|
|
url: api_url + "/authenticate",
|
|
|
|
json: {
|
|
|
|
agent: {
|
|
|
|
name: "Minecraft",
|
|
|
|
version: 1
|
|
|
|
},
|
|
|
|
username: username,
|
|
|
|
password: password,
|
|
|
|
requestUser: true
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
request.post(requestObject, function(error, response, body) {
|
|
|
|
if (error) resolve(error);
|
|
|
|
if(!body.selectedProfile) {
|
|
|
|
throw new Error("Validation error: " + response.statusMessage);
|
|
|
|
}
|
|
|
|
|
|
|
|
const userProfile = {
|
|
|
|
access_token: body.accessToken,
|
|
|
|
client_token: uuid(),
|
|
|
|
uuid: body.selectedProfile.id,
|
|
|
|
name: body.selectedProfile.name,
|
2018-11-30 19:33:08 -05:00
|
|
|
selected_profile: body.selectedProfile,
|
2019-03-10 20:18:49 -04:00
|
|
|
user_properties: JSON.stringify(body.user.properties || {})
|
2018-10-29 18:13:58 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
resolve(userProfile);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-11-30 19:33:08 -05:00
|
|
|
function validate(access_token) {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
const requestObject = {
|
|
|
|
url: api_url + "/validate",
|
|
|
|
json: {
|
|
|
|
"accessToken": access_token
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
request.post(requestObject, async function(error, response, body) {
|
|
|
|
if (error) resolve(error);
|
|
|
|
|
|
|
|
if(!body) resolve(true); else resolve(false);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function refreshAuth(accessToken, clientToken, selectedProfile) {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
const requestObject = {
|
|
|
|
url: api_url + "/refresh",
|
|
|
|
json: {
|
|
|
|
"accessToken": accessToken,
|
|
|
|
"clientToken": clientToken,
|
|
|
|
"selectedProfile": selectedProfile,
|
|
|
|
"requestUser": true
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
request.post(requestObject, function(error, response, body) {
|
|
|
|
if (error) resolve(error);
|
|
|
|
console.log(body);
|
|
|
|
if(!body.selectedProfile) {
|
|
|
|
throw new Error("Validation error: " + response.statusMessage);
|
|
|
|
}
|
|
|
|
|
|
|
|
const userProfile = {
|
|
|
|
access_token: body.accessToken,
|
|
|
|
client_token: uuid(),
|
|
|
|
uuid: body.selectedProfile.id,
|
|
|
|
name: body.selectedProfile.name,
|
2019-03-10 21:29:42 -04:00
|
|
|
user_properties: JSON.stringify(body.user.properties || {})
|
2018-11-30 19:33:08 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
resolve(userProfile);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {getAuth, validate, refreshAuth};
|