2013-11-14 00:08:28 +05:30
|
|
|
/* Copyright 2013 MultiMC Contributors
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <logic/auth/YggdrasilTask.h>
|
|
|
|
|
|
|
|
#include <QObject>
|
|
|
|
#include <QString>
|
|
|
|
#include <QJsonObject>
|
|
|
|
#include <QJsonDocument>
|
|
|
|
#include <QNetworkReply>
|
2013-11-15 02:02:43 +05:30
|
|
|
#include <QByteArray>
|
2013-11-14 00:08:28 +05:30
|
|
|
|
|
|
|
#include <MultiMC.h>
|
|
|
|
#include <logic/auth/MojangAccount.h>
|
|
|
|
|
2013-12-05 07:09:52 +05:30
|
|
|
YggdrasilTask::YggdrasilTask(MojangAccount *account, QObject *parent)
|
|
|
|
: Task(parent), m_account(account)
|
2013-11-14 00:08:28 +05:30
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void YggdrasilTask::executeTask()
|
|
|
|
{
|
|
|
|
setStatus(getStateMessage(STATE_SENDING_REQUEST));
|
|
|
|
|
|
|
|
// Get the content of the request we're going to send to the server.
|
|
|
|
QJsonDocument doc(getRequestContent());
|
|
|
|
|
|
|
|
auto worker = MMC->qnam();
|
|
|
|
QUrl reqUrl("https://authserver.mojang.com/" + getEndpoint());
|
|
|
|
QNetworkRequest netRequest(reqUrl);
|
|
|
|
netRequest.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
|
2013-12-01 06:30:42 +05:30
|
|
|
|
|
|
|
QByteArray requestData = doc.toJson();
|
|
|
|
m_netReply = worker->post(netRequest, requestData);
|
2013-12-08 22:04:45 +05:30
|
|
|
connect(m_netReply, &QNetworkReply::finished, this, &YggdrasilTask::processReply);
|
|
|
|
connect(m_netReply, &QNetworkReply::uploadProgress, this, &YggdrasilTask::refreshTimers);
|
|
|
|
connect(m_netReply, &QNetworkReply::downloadProgress, this, &YggdrasilTask::refreshTimers);
|
|
|
|
timeout_keeper.setSingleShot(true);
|
|
|
|
timeout_keeper.start(timeout_max);
|
|
|
|
counter.setSingleShot(false);
|
|
|
|
counter.start(time_step);
|
|
|
|
progress(0, timeout_max);
|
|
|
|
connect(&timeout_keeper, &QTimer::timeout, this, &YggdrasilTask::abort);
|
|
|
|
connect(&counter, &QTimer::timeout, this, &YggdrasilTask::heartbeat);
|
2013-11-14 00:08:28 +05:30
|
|
|
}
|
|
|
|
|
2013-12-08 22:04:45 +05:30
|
|
|
void YggdrasilTask::refreshTimers(qint64, qint64)
|
2013-11-14 00:08:28 +05:30
|
|
|
{
|
2013-12-08 22:04:45 +05:30
|
|
|
timeout_keeper.stop();
|
|
|
|
timeout_keeper.start(timeout_max);
|
|
|
|
progress(count = 0, timeout_max);
|
|
|
|
}
|
|
|
|
void YggdrasilTask::heartbeat()
|
|
|
|
{
|
|
|
|
count += time_step;
|
|
|
|
progress(count, timeout_max);
|
|
|
|
}
|
2013-11-14 00:08:28 +05:30
|
|
|
|
2013-12-08 22:04:45 +05:30
|
|
|
void YggdrasilTask::abort()
|
|
|
|
{
|
|
|
|
progress(timeout_max, timeout_max);
|
|
|
|
m_netReply->abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
void YggdrasilTask::processReply()
|
|
|
|
{
|
|
|
|
setStatus(getStateMessage(STATE_PROCESSING_RESPONSE));
|
2013-11-14 00:08:28 +05:30
|
|
|
|
2013-12-09 02:36:04 +05:30
|
|
|
// any network errors lead to offline mode right now
|
|
|
|
if (m_netReply->error() >= QNetworkReply::ConnectionRefusedError &&
|
|
|
|
m_netReply->error() <= QNetworkReply::UnknownNetworkError)
|
2013-11-14 00:08:28 +05:30
|
|
|
{
|
2013-12-08 22:04:45 +05:30
|
|
|
// WARNING/FIXME: the value here is used in MojangAccount to detect the cancel/timeout
|
2013-11-29 08:15:52 +05:30
|
|
|
emitFailed("Yggdrasil task cancelled.");
|
|
|
|
return;
|
|
|
|
}
|
2013-12-08 22:04:45 +05:30
|
|
|
|
|
|
|
// 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();
|
|
|
|
|
|
|
|
if (responseCode == 200)
|
2013-11-29 08:15:52 +05:30
|
|
|
{
|
2013-12-08 22:04:45 +05:30
|
|
|
// 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.
|
|
|
|
if (jsonError.error == QJsonParseError::NoError || replyData.size() == 0)
|
2013-11-29 08:15:52 +05:30
|
|
|
{
|
2013-12-08 22:04:45 +05:30
|
|
|
if (processResponse(replyData.size() > 0 ? doc.object() : QJsonObject()))
|
2013-11-14 00:08:28 +05:30
|
|
|
{
|
2013-12-08 22:04:45 +05:30
|
|
|
emitSucceeded();
|
|
|
|
return;
|
2013-11-29 08:15:52 +05:30
|
|
|
}
|
2013-12-08 22:04:45 +05:30
|
|
|
|
|
|
|
// errors happened anyway?
|
|
|
|
emitFailed(m_error ? m_error->m_errorMessageVerbose
|
|
|
|
: tr("An unknown error occurred when processing the response "
|
|
|
|
"from the authentication server."));
|
2013-11-29 08:15:52 +05:30
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-08 22:04:45 +05:30
|
|
|
emitFailed(tr("Failed to parse Yggdrasil JSON response: %1 at offset %2.")
|
|
|
|
.arg(jsonError.errorString())
|
|
|
|
.arg(jsonError.offset));
|
2013-11-14 00:08:28 +05:30
|
|
|
}
|
2013-12-08 22:04:45 +05:30
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
if (jsonError.error == QJsonParseError::NoError)
|
|
|
|
{
|
|
|
|
// 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.
|
|
|
|
QLOG_DEBUG() << "The request failed, but the server gave us an error message. "
|
|
|
|
"Processing error.";
|
|
|
|
emitFailed(processError(doc.object()));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// The server didn't say anything regarding the error. Give the user an unknown
|
|
|
|
// error.
|
|
|
|
QLOG_DEBUG() << "The request failed and the server gave no error message. "
|
|
|
|
"Unknown error.";
|
|
|
|
emitFailed(tr("An unknown error occurred when trying to communicate with the "
|
|
|
|
"authentication server: %1").arg(m_netReply->errorString()));
|
2013-11-14 00:08:28 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QString YggdrasilTask::processError(QJsonObject responseData)
|
|
|
|
{
|
|
|
|
QJsonValue errorVal = responseData.value("error");
|
2013-12-08 22:04:45 +05:30
|
|
|
QJsonValue errorMessageValue = responseData.value("errorMessage");
|
2013-11-14 00:08:28 +05:30
|
|
|
QJsonValue causeVal = responseData.value("cause");
|
|
|
|
|
2013-12-08 22:04:45 +05:30
|
|
|
if (errorVal.isString() && errorMessageValue.isString())
|
2013-11-14 00:08:28 +05:30
|
|
|
{
|
2013-12-08 22:04:45 +05:30
|
|
|
m_error = std::shared_ptr<Error>(new Error{
|
|
|
|
errorVal.toString(""), errorMessageValue.toString(""), causeVal.toString("")});
|
|
|
|
return m_error->m_errorMessageVerbose;
|
2013-11-14 00:08:28 +05:30
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Error is not in standard format. Don't set m_error and return unknown error.
|
|
|
|
return tr("An unknown Yggdrasil error occurred.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-15 02:02:43 +05:30
|
|
|
QString YggdrasilTask::getStateMessage(const YggdrasilTask::State state) const
|
2013-11-14 00:08:28 +05:30
|
|
|
{
|
|
|
|
switch (state)
|
|
|
|
{
|
|
|
|
case STATE_SENDING_REQUEST:
|
|
|
|
return tr("Sending request to auth servers.");
|
|
|
|
case STATE_PROCESSING_RESPONSE:
|
|
|
|
return tr("Processing response from servers.");
|
|
|
|
default:
|
|
|
|
return tr("Processing. Please wait.");
|
|
|
|
}
|
|
|
|
}
|