pimi-launcher-core/components/authenticator.js

143 lines
4.2 KiB
JavaScript
Raw Normal View History

2018-10-29 18:13:58 -04:00
const request = require('request');
const uuid = require('uuid/v1');
let api_url = "https://authserver.mojang.com";
2018-10-29 18:13:58 -04:00
module.exports.getAuth = function (username, password) {
return new Promise((resolve, reject) => {
if (!password) {
const user = {
access_token: uuid(),
client_token: uuid(),
uuid: uuid(),
name: username,
user_properties: JSON.stringify({})
};
return resolve(user);
}
2018-10-29 18:13:58 -04:00
const requestObject = {
url: api_url + "/authenticate",
json: {
agent: {
name: "Minecraft",
version: 1
},
username: username,
password: password,
2019-08-12 12:34:28 -04:00
clientToken: uuid(),
2018-10-29 18:13:58 -04:00
requestUser: true
}
};
request.post(requestObject, function (error, response, body) {
if (error) return reject(error);
if (!body || !body.selectedProfile) {
return reject("Validation error: " + response.statusMessage);
2018-10-29 18:13:58 -04:00
}
const userProfile = {
access_token: body.accessToken,
2019-08-12 12:34:28 -04:00
client_token: body.clientToken,
2018-10-29 18:13:58 -04:00
uuid: body.selectedProfile.id,
name: body.selectedProfile.name,
selected_profile: body.selectedProfile,
user_properties: JSON.stringify(body.user.properties || {})
2018-10-29 18:13:58 -04:00
};
resolve(userProfile);
});
});
};
2018-10-29 18:13:58 -04:00
2019-08-12 12:34:28 -04:00
module.exports.validate = function (access_token, client_token) {
return new Promise((resolve, reject) => {
const requestObject = {
url: api_url + "/validate",
json: {
2019-08-12 12:34:28 -04:00
"accessToken": access_token,
"clientToken": client_token
}
};
request.post(requestObject, async function (error, response, body) {
if (error) return reject(error);
if (!body) resolve(true);
else reject(body);
});
});
};
module.exports.refreshAuth = function (accessToken, clientToken, selectedProfile) {
return new Promise((resolve, reject) => {
const requestObject = {
url: api_url + "/refresh",
json: {
"accessToken": accessToken,
"clientToken": clientToken,
"selectedProfile": selectedProfile,
"requestUser": true
}
};
request.post(requestObject, function (error, response, body) {
if (error) return reject(error);
if (!body || !body.selectedProfile) {
return reject("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 || {})
};
resolve(userProfile);
});
});
2019-05-22 18:28:48 -04:00
};
module.exports.invalidate = function (accessToken, clientToken) {
return new Promise((resolve, reject) => {
2019-05-22 18:28:48 -04:00
const requestObject = {
url: api_url + "/invalidate",
json: {
"accessToken": accessToken,
"clientToken": clientToken
}
};
request.post(requestObject, function (error, response, body) {
if (error) return reject(error);
2019-05-22 18:28:48 -04:00
if (!body) resolve(true);
else reject(body);
2019-05-22 18:28:48 -04:00
});
});
};
module.exports.signOut = function (username, password) {
return new Promise((resolve, reject) => {
2019-05-22 18:28:48 -04:00
const requestObject = {
url: api_url + "/signout",
2019-05-22 18:28:48 -04:00
json: {
"username": username,
"password": password
}
};
request.post(requestObject, function (error, response, body) {
if (error) return reject(error);
2019-05-22 18:28:48 -04:00
if (!body) resolve(true);
else reject(body);
2019-05-22 18:28:48 -04:00
});
});
};
module.exports.changeApiUrl = function(url) {
api_url = url;
}