2021-12-04 00:18:05 +00:00
|
|
|
#include "LauncherLoginStep.h"
|
|
|
|
|
|
|
|
#include <QNetworkRequest>
|
|
|
|
|
2022-12-27 16:04:42 +00:00
|
|
|
#include "Logging.h"
|
|
|
|
#include "minecraft/auth/AccountTask.h"
|
2021-12-04 00:18:05 +00:00
|
|
|
#include "minecraft/auth/AuthRequest.h"
|
|
|
|
#include "minecraft/auth/Parsers.h"
|
2022-07-06 20:24:14 +01:00
|
|
|
#include "net/NetUtils.h"
|
2021-12-04 00:18:05 +00:00
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
LauncherLoginStep::LauncherLoginStep(AccountData* data) : AuthStep(data) {}
|
2021-12-04 00:18:05 +00:00
|
|
|
|
|
|
|
LauncherLoginStep::~LauncherLoginStep() noexcept = default;
|
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
QString LauncherLoginStep::describe()
|
|
|
|
{
|
2021-12-04 00:18:05 +00:00
|
|
|
return tr("Accessing Mojang services.");
|
|
|
|
}
|
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
void LauncherLoginStep::perform()
|
|
|
|
{
|
2021-12-04 00:18:05 +00:00
|
|
|
auto requestURL = "https://api.minecraftservices.com/launcher/login";
|
|
|
|
auto uhs = m_data->mojangservicesToken.extra["uhs"].toString();
|
|
|
|
auto xToken = m_data->mojangservicesToken.token;
|
|
|
|
|
|
|
|
QString mc_auth_template = R"XXX(
|
|
|
|
{
|
|
|
|
"xtoken": "XBL3.0 x=%1;%2",
|
|
|
|
"platform": "PC_LAUNCHER"
|
|
|
|
}
|
|
|
|
)XXX";
|
|
|
|
auto requestBody = mc_auth_template.arg(uhs, xToken);
|
|
|
|
|
|
|
|
QNetworkRequest request = QNetworkRequest(QUrl(requestURL));
|
|
|
|
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
|
|
|
|
request.setRawHeader("Accept", "application/json");
|
2023-08-02 17:35:35 +01:00
|
|
|
AuthRequest* requestor = new AuthRequest(this);
|
2021-12-04 00:18:05 +00:00
|
|
|
connect(requestor, &AuthRequest::finished, this, &LauncherLoginStep::onRequestDone);
|
|
|
|
requestor->post(request, requestBody.toUtf8());
|
|
|
|
qDebug() << "Getting Minecraft access token...";
|
|
|
|
}
|
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
void LauncherLoginStep::rehydrate()
|
|
|
|
{
|
2021-12-04 00:18:05 +00:00
|
|
|
// TODO: check the token validity
|
|
|
|
}
|
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
void LauncherLoginStep::onRequestDone(QNetworkReply::NetworkError error, QByteArray data, QList<QNetworkReply::RawHeaderPair> headers)
|
|
|
|
{
|
|
|
|
auto requestor = qobject_cast<AuthRequest*>(QObject::sender());
|
2021-12-04 00:18:05 +00:00
|
|
|
requestor->deleteLater();
|
|
|
|
|
2022-12-27 16:04:42 +00:00
|
|
|
qCDebug(authCredentials()) << data;
|
2021-12-04 00:18:05 +00:00
|
|
|
if (error != QNetworkReply::NoError) {
|
|
|
|
qWarning() << "Reply error:" << error;
|
2022-12-27 16:04:42 +00:00
|
|
|
qCDebug(authCredentials()) << data;
|
2022-07-06 20:24:14 +01:00
|
|
|
if (Net::isApplicationError(error)) {
|
2023-08-02 17:35:35 +01:00
|
|
|
emit finished(AccountTaskState::STATE_FAILED_SOFT, tr("Failed to get Minecraft access token: %1").arg(requestor->errorString_));
|
|
|
|
} else {
|
|
|
|
emit finished(AccountTaskState::STATE_OFFLINE, tr("Failed to get Minecraft access token: %1").arg(requestor->errorString_));
|
2022-07-06 20:24:14 +01:00
|
|
|
}
|
2021-12-04 00:18:05 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
if (!Parsers::parseMojangResponse(data, m_data->yggdrasilToken)) {
|
2021-12-04 00:18:05 +00:00
|
|
|
qWarning() << "Could not parse login_with_xbox response...";
|
2022-12-27 16:04:42 +00:00
|
|
|
qCDebug(authCredentials()) << data;
|
2023-08-02 17:35:35 +01:00
|
|
|
emit finished(AccountTaskState::STATE_FAILED_SOFT, tr("Failed to parse the Minecraft access token response."));
|
2021-12-04 00:18:05 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
emit finished(AccountTaskState::STATE_WORKING, tr(""));
|
|
|
|
}
|