pimi-launcher-core/components/authenticator.js

180 lines
4 KiB
JavaScript
Raw Normal View History

2023-06-18 15:07:15 +03:00
const { v3 } = require("uuid");
2023-06-21 17:30:27 +03:00
const axios = require("axios");
2023-06-18 15:07:15 +03:00
let uuid;
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) => {
2023-06-18 15:07:15 +03:00
getUUID(username);
2020-05-29 05:39:13 +03:00
if (!password) {
const user = {
access_token: uuid,
client_token: client_token || uuid,
uuid,
2020-05-29 05:39:13 +03:00
name: username,
2023-06-18 15:07:15 +03:00
user_properties: "{}",
};
2020-05-29 05:39:13 +03:00
2023-06-18 15:07:15 +03:00
return resolve(user);
2020-05-29 05:39:13 +03:00
}
const requestObject = {
2023-06-18 15:07:15 +03:00
url: api_url + "/authenticate",
2020-05-29 05:39:13 +03:00
json: {
agent: {
2023-06-18 15:07:15 +03:00
name: "Minecraft",
version: 1,
2020-05-29 05:39:13 +03:00
},
username,
password,
clientToken: uuid,
2023-06-18 15:07:15 +03:00
requestUser: true,
},
};
2020-05-29 05:39:13 +03:00
2023-06-21 17:30:27 +03:00
axios
.post(requestObject.url, requestObject.json)
.then((response) => {
const body = response.data;
const userProfile = {
access_token: body.accessToken,
client_token: body.clientToken,
uuid: body.selectedProfile.id,
name: body.selectedProfile.name,
selected_profile: body.selectedProfile,
user_properties: parsePropts(body.user.properties),
};
resolve(userProfile);
})
.catch((error) => {
return reject(error);
});
2023-06-18 15:07:15 +03:00
});
};
2018-10-30 01:13:58 +03:00
module.exports.validate = function (accessToken, clientToken) {
2020-05-29 05:39:13 +03:00
return new Promise((resolve, reject) => {
const requestObject = {
2023-06-18 15:07:15 +03:00
url: api_url + "/validate",
2020-05-29 05:39:13 +03:00
json: {
accessToken,
2023-06-18 15:07:15 +03:00
clientToken,
},
};
2020-05-29 05:39:13 +03:00
2023-06-21 17:30:27 +03:00
axios
.post(requestObject.url, requestObject.json)
.then((response) => {
if (!response.data) resolve(true);
else reject(response.data);
})
.catch((error) => {
reject(error);
});
2023-06-18 15:07:15 +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 = {
2023-06-18 15:07:15 +03:00
url: api_url + "/refresh",
2020-05-29 05:39:13 +03:00
json: {
accessToken,
clientToken,
2023-06-18 15:07:15 +03:00
requestUser: true,
},
};
2020-05-29 05:39:13 +03:00
2023-06-21 17:30:27 +03:00
axios
.post(requestObject.url, requestObject.json)
.then((response) => {
const body = response.data;
const userProfile = {
access_token: body.accessToken,
client_token: getUUID(body.selectedProfile.name),
uuid: body.selectedProfile.id,
name: body.selectedProfile.name,
user_properties: parsePropts(body.user.properties),
};
return resolve(userProfile);
})
.catch((error) => {
reject(error);
});
2023-06-18 15:07:15 +03:00
});
};
2019-05-23 01:28:48 +03:00
module.exports.invalidate = function (accessToken, clientToken) {
2020-05-29 05:39:13 +03:00
return new Promise((resolve, reject) => {
const requestObject = {
2023-06-18 15:07:15 +03:00
url: api_url + "/invalidate",
2020-05-29 05:39:13 +03:00
json: {
accessToken,
2023-06-18 15:07:15 +03:00
clientToken,
},
};
2020-05-29 05:39:13 +03:00
2023-06-21 17:30:27 +03:00
axios
.post(requestObject.url, requestObject.json)
.then((response) => {
if (!response.data) return resolve(true);
else return reject(response.data);
})
.catch((error) => {
return reject(error);
});
2023-06-18 15:07:15 +03:00
});
};
2019-05-23 01:28:48 +03:00
module.exports.signOut = function (username, password) {
2020-05-29 05:39:13 +03:00
return new Promise((resolve, reject) => {
const requestObject = {
2023-06-18 15:07:15 +03:00
url: api_url + "/signout",
2020-05-29 05:39:13 +03:00
json: {
username,
2023-06-18 15:07:15 +03:00
password,
},
};
2020-05-29 05:39:13 +03:00
2023-06-21 17:30:27 +03:00
axios
.post(requestObject.url, requestObject.json)
.then((response) => {
if (!response.data) return resolve(true);
else return reject(response.data);
})
.catch((error) => {
return reject(error);
});
2023-06-18 15:07:15 +03:00
});
};
2020-05-29 05:39:13 +03:00
module.exports.changeApiUrl = function (url) {
2023-06-18 15:07:15 +03:00
api_url = url;
};
2021-05-05 20:48:52 +03:00
2023-06-18 15:07:15 +03:00
function parsePropts(array) {
2021-05-05 20:48:52 +03:00
if (array) {
2023-06-18 15:07:15 +03:00
const newObj = {};
2021-05-05 20:48:52 +03:00
for (const entry of array) {
if (newObj[entry.name]) {
2023-06-18 15:07:15 +03:00
newObj[entry.name].push(entry.value);
2021-05-05 20:48:52 +03:00
} else {
2023-06-18 15:07:15 +03:00
newObj[entry.name] = [entry.value];
2021-05-05 20:48:52 +03:00
}
}
2023-06-18 15:07:15 +03:00
return JSON.stringify(newObj);
2021-05-05 20:48:52 +03:00
} else {
2023-06-18 15:07:15 +03:00
return "{}";
2021-05-05 20:48:52 +03:00
}
}
2023-06-18 15:07:15 +03:00
function getUUID(value) {
if (!uuid) {
2023-06-18 15:07:15 +03:00
uuid = v3(value, v3.DNS);
}
2023-06-18 15:07:15 +03:00
return uuid;
}