2021-01-18 07:28:54 +00:00
|
|
|
/* Copyright 2013-2021 MultiMC Contributors
|
2013-11-13 18:38:28 +00:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2021-07-26 20:44:11 +01:00
|
|
|
#include "Yggdrasil.h"
|
2021-12-04 00:18:05 +00:00
|
|
|
#include "AccountData.h"
|
2013-11-13 18:38:28 +00:00
|
|
|
|
2023-08-14 17:16:53 +01:00
|
|
|
#include <QByteArray>
|
2013-11-13 18:38:28 +00:00
|
|
|
#include <QJsonDocument>
|
2023-08-14 17:16:53 +01:00
|
|
|
#include <QJsonObject>
|
2013-11-13 18:38:28 +00:00
|
|
|
#include <QNetworkReply>
|
2023-08-14 17:16:53 +01:00
|
|
|
#include <QObject>
|
|
|
|
#include <QString>
|
2021-11-23 00:25:24 +00:00
|
|
|
|
2015-02-02 01:14:14 +00:00
|
|
|
#include <QDebug>
|
2015-01-31 15:59:03 +00:00
|
|
|
|
2021-11-21 22:21:12 +00:00
|
|
|
#include "Application.h"
|
2021-11-20 15:22:22 +00:00
|
|
|
|
2023-08-14 17:16:53 +01:00
|
|
|
Yggdrasil::Yggdrasil(AccountData* data, QObject* parent) : AccountTask(data, parent)
|
2013-11-13 18:38:28 +00:00
|
|
|
{
|
2021-12-04 00:18:05 +00:00
|
|
|
changeState(AccountTaskState::STATE_CREATED);
|
2013-11-13 18:38:28 +00:00
|
|
|
}
|
|
|
|
|
2023-08-14 17:16:53 +01:00
|
|
|
void Yggdrasil::sendRequest(QUrl endpoint, QByteArray content)
|
|
|
|
{
|
2021-12-04 00:18:05 +00:00
|
|
|
changeState(AccountTaskState::STATE_WORKING);
|
2018-07-15 13:51:05 +01:00
|
|
|
|
2021-07-26 20:44:11 +01:00
|
|
|
QNetworkRequest netRequest(endpoint);
|
2013-11-13 18:38:28 +00:00
|
|
|
netRequest.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
|
2021-11-21 22:21:12 +00:00
|
|
|
m_netReply = APPLICATION->network()->post(netRequest, content);
|
2021-07-26 20:44:11 +01:00
|
|
|
connect(m_netReply, &QNetworkReply::finished, this, &Yggdrasil::processReply);
|
|
|
|
connect(m_netReply, &QNetworkReply::uploadProgress, this, &Yggdrasil::refreshTimers);
|
|
|
|
connect(m_netReply, &QNetworkReply::downloadProgress, this, &Yggdrasil::refreshTimers);
|
|
|
|
connect(m_netReply, &QNetworkReply::sslErrors, this, &Yggdrasil::sslErrors);
|
2013-12-08 16:34:45 +00:00
|
|
|
timeout_keeper.setSingleShot(true);
|
|
|
|
timeout_keeper.start(timeout_max);
|
|
|
|
counter.setSingleShot(false);
|
|
|
|
counter.start(time_step);
|
|
|
|
progress(0, timeout_max);
|
2021-07-26 20:44:11 +01:00
|
|
|
connect(&timeout_keeper, &QTimer::timeout, this, &Yggdrasil::abortByTimeout);
|
|
|
|
connect(&counter, &QTimer::timeout, this, &Yggdrasil::heartbeat);
|
|
|
|
}
|
|
|
|
|
2023-08-14 17:16:53 +01:00
|
|
|
void Yggdrasil::executeTask() {}
|
2021-07-26 20:44:11 +01:00
|
|
|
|
2023-08-14 17:16:53 +01:00
|
|
|
void Yggdrasil::refresh()
|
|
|
|
{
|
2021-07-26 20:44:11 +01:00
|
|
|
start();
|
|
|
|
/*
|
|
|
|
* {
|
|
|
|
* "clientToken": "client identifier"
|
|
|
|
* "accessToken": "current access token to be refreshed"
|
|
|
|
* "selectedProfile": // specifying this causes errors
|
|
|
|
* {
|
|
|
|
* "id": "profile ID"
|
|
|
|
* "name": "profile name"
|
|
|
|
* }
|
|
|
|
* "requestUser": true/false // request the user structure
|
|
|
|
* }
|
|
|
|
*/
|
|
|
|
QJsonObject req;
|
|
|
|
req.insert("clientToken", m_data->clientToken());
|
|
|
|
req.insert("accessToken", m_data->accessToken());
|
|
|
|
/*
|
|
|
|
{
|
|
|
|
auto currentProfile = m_account->currentProfile();
|
|
|
|
QJsonObject profile;
|
|
|
|
profile.insert("id", currentProfile->id());
|
|
|
|
profile.insert("name", currentProfile->name());
|
|
|
|
req.insert("selectedProfile", profile);
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
req.insert("requestUser", false);
|
|
|
|
QJsonDocument doc(req);
|
|
|
|
|
2021-11-20 15:22:22 +00:00
|
|
|
QUrl reqUrl("https://authserver.mojang.com/refresh");
|
2021-07-26 20:44:11 +01:00
|
|
|
QByteArray requestData = doc.toJson();
|
|
|
|
|
|
|
|
sendRequest(reqUrl, requestData);
|
|
|
|
}
|
|
|
|
|
2023-08-14 17:16:53 +01:00
|
|
|
void Yggdrasil::login(QString password)
|
|
|
|
{
|
2021-07-26 20:44:11 +01:00
|
|
|
start();
|
|
|
|
/*
|
|
|
|
* {
|
|
|
|
* "agent": { // optional
|
|
|
|
* "name": "Minecraft", // So far this is the only encountered value
|
|
|
|
* "version": 1 // This number might be increased
|
|
|
|
* // by the vanilla client in the future
|
|
|
|
* },
|
|
|
|
* "username": "mojang account name", // Can be an email address or player name for
|
|
|
|
* // unmigrated accounts
|
|
|
|
* "password": "mojang account password",
|
|
|
|
* "clientToken": "client identifier", // optional
|
|
|
|
* "requestUser": true/false // request the user structure
|
|
|
|
* }
|
|
|
|
*/
|
|
|
|
QJsonObject req;
|
|
|
|
|
|
|
|
{
|
|
|
|
QJsonObject agent;
|
|
|
|
// C++ makes string literals void* for some stupid reason, so we have to tell it
|
|
|
|
// QString... Thanks Obama.
|
|
|
|
agent.insert("name", QString("Minecraft"));
|
|
|
|
agent.insert("version", 1);
|
|
|
|
req.insert("agent", agent);
|
|
|
|
}
|
|
|
|
|
|
|
|
req.insert("username", m_data->userName());
|
|
|
|
req.insert("password", password);
|
|
|
|
req.insert("requestUser", false);
|
|
|
|
|
|
|
|
// If we already have a client token, give it to the server.
|
|
|
|
// Otherwise, let the server give us one.
|
|
|
|
|
|
|
|
m_data->generateClientTokenIfMissing();
|
|
|
|
req.insert("clientToken", m_data->clientToken());
|
|
|
|
|
|
|
|
QJsonDocument doc(req);
|
|
|
|
|
2021-11-20 15:22:22 +00:00
|
|
|
QUrl reqUrl("https://authserver.mojang.com/authenticate");
|
2021-07-26 20:44:11 +01:00
|
|
|
QNetworkRequest netRequest(reqUrl);
|
|
|
|
QByteArray requestData = doc.toJson();
|
|
|
|
|
|
|
|
sendRequest(reqUrl, requestData);
|
2013-11-13 18:38:28 +00:00
|
|
|
}
|
|
|
|
|
2023-08-14 17:16:53 +01:00
|
|
|
void Yggdrasil::refreshTimers(qint64, qint64)
|
|
|
|
{
|
2013-12-08 16:34:45 +00:00
|
|
|
timeout_keeper.stop();
|
|
|
|
timeout_keeper.start(timeout_max);
|
|
|
|
progress(count = 0, timeout_max);
|
|
|
|
}
|
2021-11-20 15:22:22 +00:00
|
|
|
|
2023-08-14 17:16:53 +01:00
|
|
|
void Yggdrasil::heartbeat()
|
|
|
|
{
|
2013-12-08 16:34:45 +00:00
|
|
|
count += time_step;
|
|
|
|
progress(count, timeout_max);
|
|
|
|
}
|
2013-11-13 18:38:28 +00:00
|
|
|
|
2023-08-14 17:16:53 +01:00
|
|
|
bool Yggdrasil::abort()
|
|
|
|
{
|
2013-12-08 16:34:45 +00:00
|
|
|
progress(timeout_max, timeout_max);
|
2014-04-06 19:31:02 +01:00
|
|
|
// TODO: actually use this in a meaningful way
|
2021-07-26 20:44:11 +01:00
|
|
|
m_aborted = Yggdrasil::BY_USER;
|
2013-12-08 16:34:45 +00:00
|
|
|
m_netReply->abort();
|
2015-07-21 01:38:15 +01:00
|
|
|
return true;
|
2013-12-08 16:34:45 +00:00
|
|
|
}
|
|
|
|
|
2023-08-14 17:16:53 +01:00
|
|
|
void Yggdrasil::abortByTimeout()
|
|
|
|
{
|
2014-03-23 18:07:13 +00:00
|
|
|
progress(timeout_max, timeout_max);
|
2014-04-06 19:31:02 +01:00
|
|
|
// TODO: actually use this in a meaningful way
|
2021-07-26 20:44:11 +01:00
|
|
|
m_aborted = Yggdrasil::BY_TIMEOUT;
|
2014-03-23 18:07:13 +00:00
|
|
|
m_netReply->abort();
|
|
|
|
}
|
|
|
|
|
2023-08-14 17:16:53 +01:00
|
|
|
void Yggdrasil::sslErrors(QList<QSslError> errors)
|
|
|
|
{
|
2013-12-24 22:38:37 +00:00
|
|
|
int i = 1;
|
2021-11-20 15:22:22 +00:00
|
|
|
for (auto error : errors) {
|
2015-02-02 01:14:14 +00:00
|
|
|
qCritical() << "LOGIN SSL Error #" << i << " : " << error.errorString();
|
2013-12-24 22:38:37 +00:00
|
|
|
auto cert = error.certificate();
|
2015-02-02 01:14:14 +00:00
|
|
|
qCritical() << "Certificate in question:\n" << cert.toText();
|
2013-12-24 22:38:37 +00:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-14 17:16:53 +01:00
|
|
|
void Yggdrasil::processResponse(QJsonObject responseData)
|
|
|
|
{
|
2021-07-26 20:44:11 +01:00
|
|
|
// Read the response data. We need to get the client token, access token, and the selected
|
|
|
|
// profile.
|
|
|
|
qDebug() << "Processing authentication response.";
|
|
|
|
|
|
|
|
// qDebug() << responseData;
|
|
|
|
// If we already have a client token, make sure the one the server gave us matches our
|
|
|
|
// existing one.
|
|
|
|
QString clientToken = responseData.value("clientToken").toString("");
|
2021-11-20 15:22:22 +00:00
|
|
|
if (clientToken.isEmpty()) {
|
2021-07-26 20:44:11 +01:00
|
|
|
// Fail if the server gave us an empty client token
|
2021-12-04 00:18:05 +00:00
|
|
|
changeState(AccountTaskState::STATE_FAILED_HARD, tr("Authentication server didn't send a client token."));
|
2021-07-26 20:44:11 +01:00
|
|
|
return;
|
|
|
|
}
|
2023-08-14 17:16:53 +01:00
|
|
|
if (m_data->clientToken().isEmpty()) {
|
2021-07-26 20:44:11 +01:00
|
|
|
m_data->setClientToken(clientToken);
|
2023-08-14 17:16:53 +01:00
|
|
|
} else if (clientToken != m_data->clientToken()) {
|
|
|
|
changeState(AccountTaskState::STATE_FAILED_HARD,
|
|
|
|
tr("Authentication server attempted to change the client token. This isn't supported."));
|
2021-07-26 20:44:11 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now, we set the access token.
|
|
|
|
qDebug() << "Getting access token.";
|
|
|
|
QString accessToken = responseData.value("accessToken").toString("");
|
2021-11-20 15:22:22 +00:00
|
|
|
if (accessToken.isEmpty()) {
|
2021-07-26 20:44:11 +01:00
|
|
|
// Fail if the server didn't give us an access token.
|
2021-12-04 00:18:05 +00:00
|
|
|
changeState(AccountTaskState::STATE_FAILED_HARD, tr("Authentication server didn't send an access token."));
|
2021-07-26 20:44:11 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Set the access token.
|
|
|
|
m_data->yggdrasilToken.token = accessToken;
|
|
|
|
m_data->yggdrasilToken.validity = Katabasis::Validity::Certain;
|
2021-11-20 15:22:22 +00:00
|
|
|
m_data->yggdrasilToken.issueInstant = QDateTime::currentDateTimeUtc();
|
2021-07-26 20:44:11 +01:00
|
|
|
|
2022-04-21 21:01:55 +01:00
|
|
|
// Get UUID here since we need it for later
|
|
|
|
auto profile = responseData.value("selectedProfile");
|
|
|
|
if (!profile.isObject()) {
|
|
|
|
changeState(AccountTaskState::STATE_FAILED_HARD, tr("Authentication server didn't send a selected profile."));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto profileObj = profile.toObject();
|
|
|
|
for (auto i = profileObj.constBegin(); i != profileObj.constEnd(); ++i) {
|
|
|
|
if (i.key() == "name" && i.value().isString()) {
|
|
|
|
m_data->minecraftProfile.name = i->toString();
|
2023-08-14 17:16:53 +01:00
|
|
|
} else if (i.key() == "id" && i.value().isString()) {
|
2022-04-21 21:01:55 +01:00
|
|
|
m_data->minecraftProfile.id = i->toString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_data->minecraftProfile.id.isEmpty()) {
|
|
|
|
changeState(AccountTaskState::STATE_FAILED_HARD, tr("Authentication server didn't send a UUID in selected profile."));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-07-26 20:44:11 +01:00
|
|
|
// We've made it through the minefield of possible errors. Return true to indicate that
|
|
|
|
// we've succeeded.
|
|
|
|
qDebug() << "Finished reading authentication response.";
|
2021-12-04 00:18:05 +00:00
|
|
|
changeState(AccountTaskState::STATE_SUCCEEDED);
|
2021-07-26 20:44:11 +01:00
|
|
|
}
|
|
|
|
|
2023-08-14 17:16:53 +01:00
|
|
|
void Yggdrasil::processReply()
|
|
|
|
{
|
2021-12-04 00:18:05 +00:00
|
|
|
changeState(AccountTaskState::STATE_WORKING);
|
2018-07-15 13:51:05 +01:00
|
|
|
|
2023-08-14 17:16:53 +01:00
|
|
|
switch (m_netReply->error()) {
|
|
|
|
case QNetworkReply::NoError:
|
|
|
|
break;
|
|
|
|
case QNetworkReply::TimeoutError:
|
|
|
|
changeState(AccountTaskState::STATE_FAILED_SOFT, tr("Authentication operation timed out."));
|
|
|
|
return;
|
|
|
|
case QNetworkReply::OperationCanceledError:
|
|
|
|
changeState(AccountTaskState::STATE_FAILED_SOFT, tr("Authentication operation cancelled."));
|
|
|
|
return;
|
|
|
|
case QNetworkReply::SslHandshakeFailedError:
|
|
|
|
changeState(AccountTaskState::STATE_FAILED_SOFT,
|
|
|
|
tr("<b>SSL Handshake failed.</b><br/>There might be a few causes for it:<br/>"
|
|
|
|
"<ul>"
|
|
|
|
"<li>You use Windows and need to update your root certificates, please install any outstanding updates.</li>"
|
|
|
|
"<li>Some device on your network is interfering with SSL traffic. In that case, "
|
|
|
|
"you have bigger worries than Minecraft not starting.</li>"
|
|
|
|
"<li>Possibly something else. Check the log file for details</li>"
|
|
|
|
"</ul>"));
|
|
|
|
return;
|
|
|
|
// used for invalid credentials and similar errors. Fall through.
|
|
|
|
case QNetworkReply::ContentAccessDenied:
|
|
|
|
case QNetworkReply::ContentOperationNotPermittedError:
|
|
|
|
break;
|
|
|
|
case QNetworkReply::ContentGoneError: {
|
|
|
|
changeState(AccountTaskState::STATE_FAILED_GONE,
|
|
|
|
tr("The Mojang account no longer exists. It may have been migrated to a Microsoft account."));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
changeState(AccountTaskState::STATE_FAILED_SOFT, tr("Authentication operation failed due to a network error: %1 (%2)")
|
|
|
|
.arg(m_netReply->errorString())
|
|
|
|
.arg(m_netReply->error()));
|
|
|
|
return;
|
2013-11-29 02:45:52 +00:00
|
|
|
}
|
2018-07-15 13:51:05 +01:00
|
|
|
|
2013-12-08 16:34:45 +00:00
|
|
|
// Try to parse the response regardless of the response code.
|
|
|
|
// Sometimes the auth server will give more information and an error code.
|
|
|
|
QJsonParseError jsonError;
|
|
|
|
QByteArray replyData = m_netReply->readAll();
|
|
|
|
QJsonDocument doc = QJsonDocument::fromJson(replyData, &jsonError);
|
|
|
|
// Check the response code.
|
|
|
|
int responseCode = m_netReply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
2018-07-15 13:51:05 +01:00
|
|
|
|
2021-11-20 15:22:22 +00:00
|
|
|
if (responseCode == 200) {
|
2013-12-08 16:34:45 +00:00
|
|
|
// If the response code was 200, then there shouldn't be an error. Make sure
|
|
|
|
// anyways.
|
|
|
|
// Also, sometimes an empty reply indicates success. If there was no data received,
|
|
|
|
// pass an empty json object to the processResponse function.
|
2021-11-20 15:22:22 +00:00
|
|
|
if (jsonError.error == QJsonParseError::NoError || replyData.size() == 0) {
|
2014-04-06 19:31:02 +01:00
|
|
|
processResponse(replyData.size() > 0 ? doc.object() : QJsonObject());
|
|
|
|
return;
|
2023-08-14 17:16:53 +01:00
|
|
|
} else {
|
|
|
|
changeState(AccountTaskState::STATE_FAILED_SOFT,
|
|
|
|
tr("Failed to parse authentication server response JSON response: %1 at offset %2.")
|
|
|
|
.arg(jsonError.errorString())
|
|
|
|
.arg(jsonError.offset));
|
2015-02-02 01:14:14 +00:00
|
|
|
qCritical() << replyData;
|
2013-11-13 18:38:28 +00:00
|
|
|
}
|
2013-12-08 16:34:45 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-07-15 13:51:05 +01:00
|
|
|
|
2013-12-08 16:34:45 +00:00
|
|
|
// If the response code was not 200, then Yggdrasil may have given us information
|
|
|
|
// about the error.
|
|
|
|
// If we can parse the response, then get information from it. Otherwise just say
|
|
|
|
// there was an unknown error.
|
2021-11-20 15:22:22 +00:00
|
|
|
if (jsonError.error == QJsonParseError::NoError) {
|
2013-12-08 16:34:45 +00:00
|
|
|
// We were able to parse the server's response. Woo!
|
|
|
|
// Call processError. If a subclass has overridden it then they'll handle their
|
|
|
|
// stuff there.
|
2021-08-29 18:58:35 +01:00
|
|
|
qDebug() << "The request failed, but the server gave us an error message. Processing error.";
|
2014-04-06 19:31:02 +01:00
|
|
|
processError(doc.object());
|
2023-08-14 17:16:53 +01:00
|
|
|
} else {
|
2013-12-08 16:34:45 +00:00
|
|
|
// The server didn't say anything regarding the error. Give the user an unknown
|
|
|
|
// error.
|
2021-08-29 18:58:35 +01:00
|
|
|
qDebug() << "The request failed and the server gave no error message. Unknown error.";
|
|
|
|
changeState(
|
2021-12-04 00:18:05 +00:00
|
|
|
AccountTaskState::STATE_FAILED_SOFT,
|
2023-08-14 17:16:53 +01:00
|
|
|
tr("An unknown error occurred when trying to communicate with the authentication server: %1").arg(m_netReply->errorString()));
|
2013-11-13 18:38:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-14 17:16:53 +01:00
|
|
|
void Yggdrasil::processError(QJsonObject responseData)
|
|
|
|
{
|
2013-11-13 18:38:28 +00:00
|
|
|
QJsonValue errorVal = responseData.value("error");
|
2013-12-08 16:34:45 +00:00
|
|
|
QJsonValue errorMessageValue = responseData.value("errorMessage");
|
2013-11-13 18:38:28 +00:00
|
|
|
QJsonValue causeVal = responseData.value("cause");
|
2018-07-15 13:51:05 +01:00
|
|
|
|
2021-11-20 15:22:22 +00:00
|
|
|
if (errorVal.isString() && errorMessageValue.isString()) {
|
2023-08-14 17:16:53 +01:00
|
|
|
m_error = std::shared_ptr<Error>(new Error{ errorVal.toString(""), errorMessageValue.toString(""), causeVal.toString("") });
|
2021-12-04 00:18:05 +00:00
|
|
|
changeState(AccountTaskState::STATE_FAILED_HARD, m_error->m_errorMessageVerbose);
|
2023-08-14 17:16:53 +01:00
|
|
|
} else {
|
2013-11-13 18:38:28 +00:00
|
|
|
// Error is not in standard format. Don't set m_error and return unknown error.
|
2021-12-04 00:18:05 +00:00
|
|
|
changeState(AccountTaskState::STATE_FAILED_HARD, tr("An unknown Yggdrasil error occurred."));
|
2013-11-13 18:38:28 +00:00
|
|
|
}
|
|
|
|
}
|