2015-02-03 03:55:30 +05:30
|
|
|
/* Copyright 2013-2015 MultiMC Contributors
|
2013-11-14 00:08:28 +05:30
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2015-02-09 06:21:14 +05:30
|
|
|
#include <auth/YggdrasilTask.h>
|
2013-11-14 00:08:28 +05:30
|
|
|
|
|
|
|
#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
|
|
|
|
2015-02-09 06:21:14 +05:30
|
|
|
#include <Env.h>
|
|
|
|
#include <auth/MojangAccount.h>
|
|
|
|
#include <net/URLConstants.h>
|
2013-11-14 00:08:28 +05:30
|
|
|
|
2015-02-02 06:44:14 +05:30
|
|
|
#include <QDebug>
|
2015-01-31 21:29:03 +05:30
|
|
|
|
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
|
|
|
{
|
2014-04-07 00:01:02 +05:30
|
|
|
changeState(STATE_CREATED);
|
2013-11-14 00:08:28 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void YggdrasilTask::executeTask()
|
|
|
|
{
|
2014-04-07 00:01:02 +05:30
|
|
|
changeState(STATE_SENDING_REQUEST);
|
2013-11-14 00:08:28 +05:30
|
|
|
|
|
|
|
// Get the content of the request we're going to send to the server.
|
|
|
|
QJsonDocument doc(getRequestContent());
|
|
|
|
|
2015-01-31 21:29:03 +05:30
|
|
|
auto worker = ENV.qnam();
|
2013-12-13 20:28:11 +05:30
|
|
|
QUrl reqUrl("https://" + URLConstants::AUTH_BASE + getEndpoint());
|
2013-11-14 00:08:28 +05:30
|
|
|
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);
|
2013-12-25 04:08:37 +05:30
|
|
|
connect(m_netReply, &QNetworkReply::sslErrors, this, &YggdrasilTask::sslErrors);
|
2013-12-08 22:04:45 +05:30
|
|
|
timeout_keeper.setSingleShot(true);
|
|
|
|
timeout_keeper.start(timeout_max);
|
|
|
|
counter.setSingleShot(false);
|
|
|
|
counter.start(time_step);
|
|
|
|
progress(0, timeout_max);
|
2014-03-23 23:37:13 +05:30
|
|
|
connect(&timeout_keeper, &QTimer::timeout, this, &YggdrasilTask::abortByTimeout);
|
2013-12-08 22:04:45 +05:30
|
|
|
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
|
|
|
|
2015-07-21 06:08:15 +05:30
|
|
|
bool YggdrasilTask::abort()
|
2013-12-08 22:04:45 +05:30
|
|
|
{
|
|
|
|
progress(timeout_max, timeout_max);
|
2014-04-07 00:01:02 +05:30
|
|
|
// TODO: actually use this in a meaningful way
|
|
|
|
m_aborted = YggdrasilTask::BY_USER;
|
2013-12-08 22:04:45 +05:30
|
|
|
m_netReply->abort();
|
2015-07-21 06:08:15 +05:30
|
|
|
return true;
|
2013-12-08 22:04:45 +05:30
|
|
|
}
|
|
|
|
|
2014-03-23 23:37:13 +05:30
|
|
|
void YggdrasilTask::abortByTimeout()
|
|
|
|
{
|
|
|
|
progress(timeout_max, timeout_max);
|
2014-04-07 00:01:02 +05:30
|
|
|
// TODO: actually use this in a meaningful way
|
|
|
|
m_aborted = YggdrasilTask::BY_TIMEOUT;
|
2014-03-23 23:37:13 +05:30
|
|
|
m_netReply->abort();
|
|
|
|
}
|
|
|
|
|
2013-12-25 04:08:37 +05:30
|
|
|
void YggdrasilTask::sslErrors(QList<QSslError> errors)
|
|
|
|
{
|
|
|
|
int i = 1;
|
2013-12-25 05:57:38 +05:30
|
|
|
for (auto error : errors)
|
2013-12-25 04:08:37 +05:30
|
|
|
{
|
2015-02-02 06:44:14 +05:30
|
|
|
qCritical() << "LOGIN SSL Error #" << i << " : " << error.errorString();
|
2013-12-25 04:08:37 +05:30
|
|
|
auto cert = error.certificate();
|
2015-02-02 06:44:14 +05:30
|
|
|
qCritical() << "Certificate in question:\n" << cert.toText();
|
2013-12-25 04:08:37 +05:30
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-08 22:04:45 +05:30
|
|
|
void YggdrasilTask::processReply()
|
|
|
|
{
|
2014-04-07 00:01:02 +05:30
|
|
|
changeState(STATE_PROCESSING_RESPONSE);
|
2013-11-14 00:08:28 +05:30
|
|
|
|
2014-04-07 00:01:02 +05:30
|
|
|
switch (m_netReply->error())
|
2013-12-25 04:08:37 +05:30
|
|
|
{
|
2014-04-07 00:01:02 +05:30
|
|
|
case QNetworkReply::NoError:
|
|
|
|
break;
|
|
|
|
case QNetworkReply::TimeoutError:
|
|
|
|
changeState(STATE_FAILED_SOFT, tr("Authentication operation timed out."));
|
|
|
|
return;
|
|
|
|
case QNetworkReply::OperationCanceledError:
|
|
|
|
changeState(STATE_FAILED_SOFT, tr("Authentication operation cancelled."));
|
|
|
|
return;
|
|
|
|
case QNetworkReply::SslHandshakeFailedError:
|
|
|
|
changeState(
|
|
|
|
STATE_FAILED_SOFT,
|
2013-12-25 05:57:38 +05:30
|
|
|
tr("<b>SSL Handshake failed.</b><br/>There might be a few causes for it:<br/>"
|
|
|
|
"<ul>"
|
|
|
|
"<li>You use Windows XP and need to <a "
|
|
|
|
"href=\"http://www.microsoft.com/en-us/download/details.aspx?id=38918\">update "
|
|
|
|
"your root certificates</a></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 MultiMC log file for details</li>"
|
|
|
|
"</ul>"));
|
2013-12-25 04:08:37 +05:30
|
|
|
return;
|
2014-04-07 00:01:02 +05:30
|
|
|
// used for invalid credentials and similar errors. Fall through.
|
|
|
|
case QNetworkReply::ContentOperationNotPermittedError:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
changeState(STATE_FAILED_SOFT,
|
|
|
|
tr("Authentication operation failed due to a network error: %1 (%2)")
|
|
|
|
.arg(m_netReply->errorString()).arg(m_netReply->error()));
|
2013-11-29 08:15:52 +05:30
|
|
|
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
|
|
|
{
|
2014-04-07 00:01:02 +05:30
|
|
|
processResponse(replyData.size() > 0 ? doc.object() : QJsonObject());
|
|
|
|
return;
|
2013-11-29 08:15:52 +05:30
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-04-07 00:01:02 +05:30
|
|
|
changeState(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 06:44:14 +05:30
|
|
|
qCritical() << replyData;
|
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.
|
2015-02-02 06:44:14 +05:30
|
|
|
qDebug() << "The request failed, but the server gave us an error message. "
|
2013-12-08 22:04:45 +05:30
|
|
|
"Processing error.";
|
2014-04-07 00:01:02 +05:30
|
|
|
processError(doc.object());
|
2013-12-08 22:04:45 +05:30
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// The server didn't say anything regarding the error. Give the user an unknown
|
|
|
|
// error.
|
2015-02-02 06:44:14 +05:30
|
|
|
qDebug()
|
2014-04-07 00:01:02 +05:30
|
|
|
<< "The request failed and the server gave no error message. Unknown error.";
|
|
|
|
changeState(STATE_FAILED_SOFT,
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-07 00:01:02 +05:30
|
|
|
void YggdrasilTask::processError(QJsonObject responseData)
|
2013-11-14 00:08:28 +05:30
|
|
|
{
|
|
|
|
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("")});
|
2014-04-07 00:01:02 +05:30
|
|
|
changeState(STATE_FAILED_HARD, 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.
|
2014-04-07 00:01:02 +05:30
|
|
|
changeState(STATE_FAILED_HARD, tr("An unknown Yggdrasil error occurred."));
|
2013-11-14 00:08:28 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-07 00:01:02 +05:30
|
|
|
QString YggdrasilTask::getStateMessage() const
|
2013-11-14 00:08:28 +05:30
|
|
|
{
|
2014-04-07 00:01:02 +05:30
|
|
|
switch (m_state)
|
2013-11-14 00:08:28 +05:30
|
|
|
{
|
2014-04-07 00:01:02 +05:30
|
|
|
case STATE_CREATED:
|
|
|
|
return "Waiting...";
|
2013-11-14 00:08:28 +05:30
|
|
|
case STATE_SENDING_REQUEST:
|
2013-12-23 21:16:01 +05:30
|
|
|
return tr("Sending request to auth servers...");
|
2013-11-14 00:08:28 +05:30
|
|
|
case STATE_PROCESSING_RESPONSE:
|
2013-12-23 21:16:01 +05:30
|
|
|
return tr("Processing response from servers...");
|
2014-04-07 00:01:02 +05:30
|
|
|
case STATE_SUCCEEDED:
|
|
|
|
return tr("Authentication task succeeded.");
|
|
|
|
case STATE_FAILED_SOFT:
|
|
|
|
return tr("Failed to contact the authentication server.");
|
|
|
|
case STATE_FAILED_HARD:
|
|
|
|
return tr("Failed to authenticate.");
|
2013-11-14 00:08:28 +05:30
|
|
|
default:
|
2014-04-07 00:01:02 +05:30
|
|
|
return tr("...");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void YggdrasilTask::changeState(YggdrasilTask::State newState, QString reason)
|
|
|
|
{
|
|
|
|
m_state = newState;
|
|
|
|
setStatus(getStateMessage());
|
|
|
|
if (newState == STATE_SUCCEEDED)
|
|
|
|
{
|
|
|
|
emitSucceeded();
|
|
|
|
}
|
|
|
|
else if (newState == STATE_FAILED_HARD || newState == STATE_FAILED_SOFT)
|
|
|
|
{
|
|
|
|
emitFailed(reason);
|
2013-11-14 00:08:28 +05:30
|
|
|
}
|
|
|
|
}
|
2014-04-07 00:01:02 +05:30
|
|
|
|
|
|
|
YggdrasilTask::State YggdrasilTask::state()
|
|
|
|
{
|
|
|
|
return m_state;
|
|
|
|
}
|