2013-11-14 04:29:50 +05:30
|
|
|
|
2014-11-03 00:38:26 +05:30
|
|
|
/* Copyright 2013-2014 MultiMC Contributors
|
2013-11-14 04:29:50 +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.
|
|
|
|
*/
|
|
|
|
|
2013-12-01 06:30:42 +05:30
|
|
|
#include <logic/auth/flows/AuthenticateTask.h>
|
2013-11-14 04:29:50 +05:30
|
|
|
|
|
|
|
#include <logic/auth/MojangAccount.h>
|
2013-11-15 02:02:43 +05:30
|
|
|
|
2013-11-14 04:29:50 +05:30
|
|
|
#include <QJsonDocument>
|
2013-11-15 02:02:43 +05:30
|
|
|
#include <QJsonObject>
|
|
|
|
#include <QJsonArray>
|
2013-11-14 04:29:50 +05:30
|
|
|
#include <QVariant>
|
|
|
|
|
2013-11-15 02:02:43 +05:30
|
|
|
#include "logger/QsLog.h"
|
|
|
|
|
2013-12-05 07:09:52 +05:30
|
|
|
AuthenticateTask::AuthenticateTask(MojangAccount * account, const QString &password,
|
2013-12-01 06:30:42 +05:30
|
|
|
QObject *parent)
|
|
|
|
: YggdrasilTask(account, parent), m_password(password)
|
2013-11-14 04:29:50 +05:30
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
QJsonObject AuthenticateTask::getRequestContent() const
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* {
|
|
|
|
* "agent": { // optional
|
|
|
|
* "name": "Minecraft", // So far this is the only encountered value
|
2013-12-01 06:30:42 +05:30
|
|
|
* "version": 1 // This number might be increased
|
2013-11-14 04:29:50 +05:30
|
|
|
* // 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
|
2013-12-01 06:30:42 +05:30
|
|
|
* "requestUser": true/false // request the user structure
|
2013-11-14 04:29:50 +05:30
|
|
|
* }
|
|
|
|
*/
|
|
|
|
QJsonObject req;
|
|
|
|
|
|
|
|
{
|
|
|
|
QJsonObject agent;
|
2013-12-01 06:30:42 +05:30
|
|
|
// C++ makes string literals void* for some stupid reason, so we have to tell it
|
|
|
|
// QString... Thanks Obama.
|
2013-11-14 04:29:50 +05:30
|
|
|
agent.insert("name", QString("Minecraft"));
|
|
|
|
agent.insert("version", 1);
|
|
|
|
req.insert("agent", agent);
|
|
|
|
}
|
|
|
|
|
2013-12-05 07:09:52 +05:30
|
|
|
req.insert("username", m_account->username());
|
2013-11-14 04:29:50 +05:30
|
|
|
req.insert("password", m_password);
|
2013-12-01 06:30:42 +05:30
|
|
|
req.insert("requestUser", true);
|
2013-11-15 02:02:43 +05:30
|
|
|
|
|
|
|
// If we already have a client token, give it to the server.
|
|
|
|
// Otherwise, let the server give us one.
|
2013-12-05 07:09:52 +05:30
|
|
|
if (!m_account->m_clientToken.isEmpty())
|
|
|
|
req.insert("clientToken", m_account->m_clientToken);
|
2013-11-14 04:29:50 +05:30
|
|
|
|
|
|
|
return req;
|
|
|
|
}
|
|
|
|
|
2014-04-07 00:01:02 +05:30
|
|
|
void AuthenticateTask::processResponse(QJsonObject responseData)
|
2013-11-14 04:29:50 +05:30
|
|
|
{
|
2013-12-01 06:30:42 +05:30
|
|
|
// Read the response data. We need to get the client token, access token, and the selected
|
|
|
|
// profile.
|
2013-11-15 02:02:43 +05:30
|
|
|
QLOG_DEBUG() << "Processing authentication response.";
|
2013-12-13 07:36:10 +05:30
|
|
|
// QLOG_DEBUG() << responseData;
|
2013-12-01 06:30:42 +05:30
|
|
|
// If we already have a client token, make sure the one the server gave us matches our
|
|
|
|
// existing one.
|
2013-11-15 02:02:43 +05:30
|
|
|
QLOG_DEBUG() << "Getting client token.";
|
|
|
|
QString clientToken = responseData.value("clientToken").toString("");
|
|
|
|
if (clientToken.isEmpty())
|
|
|
|
{
|
|
|
|
// Fail if the server gave us an empty client token
|
2014-04-07 00:01:02 +05:30
|
|
|
changeState(STATE_FAILED_HARD, tr("Authentication server didn't send a client token."));
|
|
|
|
return;
|
2013-11-15 02:02:43 +05:30
|
|
|
}
|
2013-12-05 07:09:52 +05:30
|
|
|
if (!m_account->m_clientToken.isEmpty() && clientToken != m_account->m_clientToken)
|
2013-11-15 02:02:43 +05:30
|
|
|
{
|
2014-04-07 00:01:02 +05:30
|
|
|
changeState(STATE_FAILED_HARD, tr("Authentication server attempted to change the client token. This isn't supported."));
|
|
|
|
return;
|
2013-11-15 02:02:43 +05:30
|
|
|
}
|
|
|
|
// Set the client token.
|
2013-12-05 07:09:52 +05:30
|
|
|
m_account->m_clientToken = clientToken;
|
2013-11-15 02:02:43 +05:30
|
|
|
|
|
|
|
// Now, we set the access token.
|
|
|
|
QLOG_DEBUG() << "Getting access token.";
|
|
|
|
QString accessToken = responseData.value("accessToken").toString("");
|
|
|
|
if (accessToken.isEmpty())
|
|
|
|
{
|
|
|
|
// Fail if the server didn't give us an access token.
|
2014-04-07 00:01:02 +05:30
|
|
|
changeState(STATE_FAILED_HARD, tr("Authentication server didn't send an access token."));
|
|
|
|
return;
|
2013-11-15 02:02:43 +05:30
|
|
|
}
|
|
|
|
// Set the access token.
|
2013-12-05 07:09:52 +05:30
|
|
|
m_account->m_accessToken = accessToken;
|
2013-11-15 02:02:43 +05:30
|
|
|
|
|
|
|
// Now we load the list of available profiles.
|
2013-12-01 06:30:42 +05:30
|
|
|
// Mojang hasn't yet implemented the profile system,
|
|
|
|
// but we might as well support what's there so we
|
2013-11-15 02:02:43 +05:30
|
|
|
// don't have trouble implementing it later.
|
|
|
|
QLOG_DEBUG() << "Loading profile list.";
|
|
|
|
QJsonArray availableProfiles = responseData.value("availableProfiles").toArray();
|
2013-12-05 07:09:52 +05:30
|
|
|
QList<AccountProfile> loadedProfiles;
|
2013-11-15 02:02:43 +05:30
|
|
|
for (auto iter : availableProfiles)
|
|
|
|
{
|
|
|
|
QJsonObject profile = iter.toObject();
|
|
|
|
// Profiles are easy, we just need their ID and name.
|
|
|
|
QString id = profile.value("id").toString("");
|
|
|
|
QString name = profile.value("name").toString("");
|
2013-12-13 07:17:59 +05:30
|
|
|
bool legacy = profile.value("legacy").toBool(false);
|
2013-11-15 02:02:43 +05:30
|
|
|
|
|
|
|
if (id.isEmpty() || name.isEmpty())
|
|
|
|
{
|
2013-12-01 06:30:42 +05:30
|
|
|
// This should never happen, but we might as well
|
2013-11-15 02:02:43 +05:30
|
|
|
// warn about it if it does so we can debug it easily.
|
|
|
|
// You never know when Mojang might do something truly derpy.
|
2013-12-01 06:30:42 +05:30
|
|
|
QLOG_WARN() << "Found entry in available profiles list with missing ID or name "
|
|
|
|
"field. Ignoring it.";
|
2013-11-15 02:02:43 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// Now, add a new AccountProfile entry to the list.
|
2013-12-13 07:17:59 +05:30
|
|
|
loadedProfiles.append({id, name, legacy});
|
2013-11-15 02:02:43 +05:30
|
|
|
}
|
|
|
|
// Put the list of profiles we loaded into the MojangAccount object.
|
2013-12-05 07:09:52 +05:30
|
|
|
m_account->m_profiles = loadedProfiles;
|
2013-11-15 02:02:43 +05:30
|
|
|
|
|
|
|
// Finally, we set the current profile to the correct value. This is pretty simple.
|
2013-12-01 06:30:42 +05:30
|
|
|
// We do need to make sure that the current profile that the server gave us
|
2013-11-15 02:02:43 +05:30
|
|
|
// is actually in the available profiles list.
|
|
|
|
// If it isn't, we'll just fail horribly (*shouldn't* ever happen, but you never know).
|
|
|
|
QLOG_DEBUG() << "Setting current profile.";
|
|
|
|
QJsonObject currentProfile = responseData.value("selectedProfile").toObject();
|
|
|
|
QString currentProfileId = currentProfile.value("id").toString("");
|
|
|
|
if (currentProfileId.isEmpty())
|
|
|
|
{
|
2014-04-07 00:01:02 +05:30
|
|
|
changeState(STATE_FAILED_HARD, tr("Authentication server didn't specify a currently selected profile. The account exists, but likely isn't premium."));
|
|
|
|
return;
|
2013-11-15 02:02:43 +05:30
|
|
|
}
|
2013-12-05 07:09:52 +05:30
|
|
|
if (!m_account->setCurrentProfile(currentProfileId))
|
2013-11-15 02:02:43 +05:30
|
|
|
{
|
2014-04-07 00:01:02 +05:30
|
|
|
changeState(STATE_FAILED_HARD, tr("Authentication server specified a selected profile that wasn't in the available profiles list."));
|
|
|
|
return;
|
2013-11-15 02:02:43 +05:30
|
|
|
}
|
2013-11-27 06:20:38 +05:30
|
|
|
|
|
|
|
// this is what the vanilla launcher passes to the userProperties launch param
|
2013-12-01 06:30:42 +05:30
|
|
|
if (responseData.contains("user"))
|
2013-11-27 06:20:38 +05:30
|
|
|
{
|
2013-12-13 07:17:59 +05:30
|
|
|
User u;
|
2013-11-27 06:20:38 +05:30
|
|
|
auto obj = responseData.value("user").toObject();
|
2013-12-13 07:17:59 +05:30
|
|
|
u.id = obj.value("id").toString();
|
2013-11-27 06:20:38 +05:30
|
|
|
auto propArray = obj.value("properties").toArray();
|
2013-12-01 06:30:42 +05:30
|
|
|
for (auto prop : propArray)
|
2013-11-27 06:20:38 +05:30
|
|
|
{
|
|
|
|
auto propTuple = prop.toObject();
|
|
|
|
auto name = propTuple.value("name").toString();
|
|
|
|
auto value = propTuple.value("value").toString();
|
2013-12-13 07:17:59 +05:30
|
|
|
u.properties.insert(name, value);
|
2013-11-27 06:20:38 +05:30
|
|
|
}
|
2013-12-13 07:17:59 +05:30
|
|
|
m_account->m_user = u;
|
2013-11-27 06:20:38 +05:30
|
|
|
}
|
2013-12-01 06:30:42 +05:30
|
|
|
|
|
|
|
// We've made it through the minefield of possible errors. Return true to indicate that
|
|
|
|
// we've succeeded.
|
2013-11-15 02:02:43 +05:30
|
|
|
QLOG_DEBUG() << "Finished reading authentication response.";
|
2014-04-07 00:01:02 +05:30
|
|
|
changeState(STATE_SUCCEEDED);
|
2013-11-14 04:29:50 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
QString AuthenticateTask::getEndpoint() const
|
|
|
|
{
|
|
|
|
return "authenticate";
|
|
|
|
}
|
|
|
|
|
2014-04-07 00:01:02 +05:30
|
|
|
QString AuthenticateTask::getStateMessage() const
|
2013-11-15 02:02:43 +05:30
|
|
|
{
|
2014-04-07 00:01:02 +05:30
|
|
|
switch (m_state)
|
2013-11-15 02:02:43 +05:30
|
|
|
{
|
|
|
|
case STATE_SENDING_REQUEST:
|
2013-12-23 21:16:01 +05:30
|
|
|
return tr("Authenticating: Sending request...");
|
2013-11-15 02:02:43 +05:30
|
|
|
case STATE_PROCESSING_RESPONSE:
|
2013-12-23 21:16:01 +05:30
|
|
|
return tr("Authenticating: Processing response...");
|
2013-11-15 02:02:43 +05:30
|
|
|
default:
|
2014-04-07 00:01:02 +05:30
|
|
|
return YggdrasilTask::getStateMessage();
|
2013-11-15 02:02:43 +05:30
|
|
|
}
|
|
|
|
}
|