2021-12-04 00:18:05 +00:00
|
|
|
#include "XboxAuthorizationStep.h"
|
|
|
|
|
|
|
|
#include <QJsonDocument>
|
2023-08-02 17:35:35 +01:00
|
|
|
#include <QJsonParseError>
|
|
|
|
#include <QNetworkRequest>
|
2021-12-04 00:18:05 +00:00
|
|
|
|
2022-12-27 16:04:42 +00:00
|
|
|
#include "Logging.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
|
|
|
XboxAuthorizationStep::XboxAuthorizationStep(AccountData* data, Katabasis::Token* token, QString relyingParty, QString authorizationKind)
|
|
|
|
: AuthStep(data), m_token(token), m_relyingParty(relyingParty), m_authorizationKind(authorizationKind)
|
|
|
|
{}
|
2021-12-04 00:18:05 +00:00
|
|
|
|
|
|
|
XboxAuthorizationStep::~XboxAuthorizationStep() noexcept = default;
|
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
QString XboxAuthorizationStep::describe()
|
|
|
|
{
|
2021-12-04 00:18:05 +00:00
|
|
|
return tr("Getting authorization to access %1 services.").arg(m_authorizationKind);
|
|
|
|
}
|
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
void XboxAuthorizationStep::rehydrate()
|
|
|
|
{
|
2021-12-04 00:18:05 +00:00
|
|
|
// FIXME: check if the tokens are good?
|
|
|
|
}
|
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
void XboxAuthorizationStep::perform()
|
|
|
|
{
|
2021-12-04 00:18:05 +00:00
|
|
|
QString xbox_auth_template = R"XXX(
|
|
|
|
{
|
|
|
|
"Properties": {
|
|
|
|
"SandboxId": "RETAIL",
|
|
|
|
"UserTokens": [
|
|
|
|
"%1"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
"RelyingParty": "%2",
|
|
|
|
"TokenType": "JWT"
|
|
|
|
}
|
|
|
|
)XXX";
|
|
|
|
auto xbox_auth_data = xbox_auth_template.arg(m_data->userToken.token, m_relyingParty);
|
2023-08-02 17:35:35 +01:00
|
|
|
// http://xboxlive.com
|
2021-12-04 00:18:05 +00:00
|
|
|
QNetworkRequest request = QNetworkRequest(QUrl("https://xsts.auth.xboxlive.com/xsts/authorize"));
|
|
|
|
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, &XboxAuthorizationStep::onRequestDone);
|
|
|
|
requestor->post(request, xbox_auth_data.toUtf8());
|
|
|
|
qDebug() << "Getting authorization token for " << m_relyingParty;
|
|
|
|
}
|
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
void XboxAuthorizationStep::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-07-06 20:24:14 +01:00
|
|
|
if (Net::isApplicationError(error)) {
|
2023-08-02 17:35:35 +01:00
|
|
|
if (!processSTSError(error, data, headers)) {
|
|
|
|
emit finished(AccountTaskState::STATE_FAILED_SOFT,
|
|
|
|
tr("Failed to get authorization for %1 services. Error %2.").arg(m_authorizationKind, error));
|
|
|
|
} else {
|
|
|
|
emit finished(AccountTaskState::STATE_FAILED_SOFT,
|
|
|
|
tr("Unknown STS error for %1 services: %2").arg(m_authorizationKind, requestor->errorString_));
|
2022-07-06 20:24:14 +01:00
|
|
|
}
|
2023-08-02 17:35:35 +01:00
|
|
|
} else {
|
|
|
|
emit finished(AccountTaskState::STATE_OFFLINE,
|
|
|
|
tr("Failed to get authorization for %1 services: %2").arg(m_authorizationKind, requestor->errorString_));
|
2021-12-04 00:18:05 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Katabasis::Token temp;
|
2023-08-02 17:35:35 +01:00
|
|
|
if (!Parsers::parseXTokenResponse(data, temp, m_authorizationKind)) {
|
|
|
|
emit finished(AccountTaskState::STATE_FAILED_SOFT,
|
|
|
|
tr("Could not parse authorization response for access to %1 services.").arg(m_authorizationKind));
|
2021-12-04 00:18:05 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
if (temp.extra["uhs"] != m_data->userToken.extra["uhs"]) {
|
|
|
|
emit finished(AccountTaskState::STATE_FAILED_SOFT,
|
|
|
|
tr("Server has changed %1 authorization user hash in the reply. Something is wrong.").arg(m_authorizationKind));
|
2021-12-04 00:18:05 +00:00
|
|
|
return;
|
|
|
|
}
|
2023-08-02 17:35:35 +01:00
|
|
|
auto& token = *m_token;
|
2021-12-04 00:18:05 +00:00
|
|
|
token = temp;
|
|
|
|
|
|
|
|
emit finished(AccountTaskState::STATE_WORKING, tr("Got authorization to access %1").arg(m_relyingParty));
|
|
|
|
}
|
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
bool XboxAuthorizationStep::processSTSError(QNetworkReply::NetworkError error, QByteArray data, QList<QNetworkReply::RawHeaderPair> headers)
|
|
|
|
{
|
|
|
|
if (error == QNetworkReply::AuthenticationRequiredError) {
|
2021-12-04 00:18:05 +00:00
|
|
|
QJsonParseError jsonError;
|
|
|
|
QJsonDocument doc = QJsonDocument::fromJson(data, &jsonError);
|
2023-08-02 17:35:35 +01:00
|
|
|
if (jsonError.error) {
|
2021-12-04 00:18:05 +00:00
|
|
|
qWarning() << "Cannot parse error XSTS response as JSON: " << jsonError.errorString();
|
2023-08-02 17:35:35 +01:00
|
|
|
emit finished(AccountTaskState::STATE_FAILED_SOFT,
|
|
|
|
tr("Cannot parse %1 authorization error response as JSON: %2").arg(m_authorizationKind, jsonError.errorString()));
|
2021-12-04 00:18:05 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t errorCode = -1;
|
|
|
|
auto obj = doc.object();
|
2023-08-02 17:35:35 +01:00
|
|
|
if (!Parsers::getNumber(obj.value("XErr"), errorCode)) {
|
|
|
|
emit finished(AccountTaskState::STATE_FAILED_SOFT,
|
|
|
|
tr("XErr element is missing from %1 authorization error response.").arg(m_authorizationKind));
|
2021-12-04 00:18:05 +00:00
|
|
|
return true;
|
|
|
|
}
|
2023-08-02 17:35:35 +01:00
|
|
|
switch (errorCode) {
|
|
|
|
case 2148916233: {
|
|
|
|
emit finished(AccountTaskState::STATE_FAILED_SOFT,
|
|
|
|
tr("This Microsoft account does not have an XBox Live profile. Buy the game on %1 first.")
|
|
|
|
.arg("<a href=\"https://www.minecraft.net/en-us/store/minecraft-java-edition\">minecraft.net</a>"));
|
2021-12-04 00:18:05 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
case 2148916235: {
|
|
|
|
// NOTE: this is the Grulovia error
|
2023-08-02 17:35:35 +01:00
|
|
|
emit finished(AccountTaskState::STATE_FAILED_SOFT, tr("XBox Live is not available in your country. You've been blocked."));
|
2021-12-04 00:18:05 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
case 2148916238: {
|
|
|
|
emit finished(
|
|
|
|
AccountTaskState::STATE_FAILED_SOFT,
|
|
|
|
tr("This Microsoft account is underaged and is not linked to a family.\n\nPlease set up your account according to %1.")
|
2023-08-02 17:35:35 +01:00
|
|
|
.arg("<a href=\"https://help.minecraft.net/hc/en-us/articles/4403181904525\">help.minecraft.net</a>"));
|
2021-12-04 00:18:05 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
default: {
|
2023-08-02 17:35:35 +01:00
|
|
|
emit finished(AccountTaskState::STATE_FAILED_SOFT,
|
|
|
|
tr("XSTS authentication ended with unrecognized error(s):\n\n%1").arg(errorCode));
|
2021-12-04 00:18:05 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|