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