2015-02-03 03:55:30 +05:30
|
|
|
/* Copyright 2013-2015 MultiMC Contributors
|
2013-12-01 06:30:42 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <logic/auth/flows/RefreshTask.h>
|
|
|
|
|
|
|
|
#include <logic/auth/MojangAccount.h>
|
|
|
|
|
|
|
|
#include <QJsonDocument>
|
|
|
|
#include <QJsonObject>
|
|
|
|
#include <QJsonArray>
|
|
|
|
#include <QVariant>
|
|
|
|
|
|
|
|
#include "logger/QsLog.h"
|
|
|
|
|
2014-01-27 07:30:49 +05:30
|
|
|
RefreshTask::RefreshTask(MojangAccount *account) : YggdrasilTask(account)
|
2013-12-01 06:30:42 +05:30
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
QJsonObject RefreshTask::getRequestContent() const
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* {
|
|
|
|
* "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;
|
2013-12-05 07:09:52 +05:30
|
|
|
req.insert("clientToken", m_account->m_clientToken);
|
|
|
|
req.insert("accessToken", m_account->m_accessToken);
|
2013-12-01 06:30:42 +05:30
|
|
|
/*
|
|
|
|
{
|
2013-12-05 07:09:52 +05:30
|
|
|
auto currentProfile = m_account->currentProfile();
|
2013-12-01 06:30:42 +05:30
|
|
|
QJsonObject profile;
|
|
|
|
profile.insert("id", currentProfile->id());
|
|
|
|
profile.insert("name", currentProfile->name());
|
|
|
|
req.insert("selectedProfile", profile);
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
req.insert("requestUser", true);
|
|
|
|
|
|
|
|
return req;
|
|
|
|
}
|
|
|
|
|
2014-04-07 00:01:02 +05:30
|
|
|
void RefreshTask::processResponse(QJsonObject responseData)
|
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.
|
|
|
|
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.
|
|
|
|
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-12-01 06:30:42 +05:30
|
|
|
}
|
2013-12-05 07:09:52 +05:30
|
|
|
if (!m_account->m_clientToken.isEmpty() && clientToken != m_account->m_clientToken)
|
2013-12-01 06:30:42 +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-12-01 06:30:42 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// Now, we set the access token.
|
|
|
|
QLOG_DEBUG() << "Getting new 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-12-01 06:30:42 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// we validate that the server responded right. (our current profile = returned current
|
|
|
|
// profile)
|
|
|
|
QJsonObject currentProfile = responseData.value("selectedProfile").toObject();
|
|
|
|
QString currentProfileId = currentProfile.value("id").toString("");
|
2013-12-05 07:09:52 +05:30
|
|
|
if (m_account->currentProfile()->id != currentProfileId)
|
2013-12-01 06:30:42 +05:30
|
|
|
{
|
2014-04-07 00:01:02 +05:30
|
|
|
changeState(STATE_FAILED_HARD, tr("Authentication server didn't specify the same prefile as expected."));
|
|
|
|
return;
|
2013-12-01 06:30:42 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// this is what the vanilla launcher passes to the userProperties launch param
|
|
|
|
if (responseData.contains("user"))
|
|
|
|
{
|
2013-12-14 05:48:54 +05:30
|
|
|
User u;
|
2013-12-01 06:30:42 +05:30
|
|
|
auto obj = responseData.value("user").toObject();
|
2013-12-14 05:48:54 +05:30
|
|
|
u.id = obj.value("id").toString();
|
2013-12-01 06:30:42 +05:30
|
|
|
auto propArray = obj.value("properties").toArray();
|
|
|
|
for (auto prop : propArray)
|
|
|
|
{
|
|
|
|
auto propTuple = prop.toObject();
|
|
|
|
auto name = propTuple.value("name").toString();
|
|
|
|
auto value = propTuple.value("value").toString();
|
2013-12-14 05:48:54 +05:30
|
|
|
u.properties.insert(name, value);
|
2013-12-01 06:30:42 +05:30
|
|
|
}
|
2013-12-14 05:48:54 +05:30
|
|
|
m_account->m_user = u;
|
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.
|
|
|
|
QLOG_DEBUG() << "Finished reading refresh response.";
|
|
|
|
// Reset the access token.
|
2013-12-05 07:09:52 +05:30
|
|
|
m_account->m_accessToken = accessToken;
|
2014-04-07 00:01:02 +05:30
|
|
|
changeState(STATE_SUCCEEDED);
|
2013-12-01 06:30:42 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
QString RefreshTask::getEndpoint() const
|
|
|
|
{
|
|
|
|
return "refresh";
|
|
|
|
}
|
|
|
|
|
2014-04-07 00:01:02 +05:30
|
|
|
QString RefreshTask::getStateMessage() const
|
2013-12-01 06:30:42 +05:30
|
|
|
{
|
2014-04-07 00:01:02 +05:30
|
|
|
switch (m_state)
|
2013-12-01 06:30:42 +05:30
|
|
|
{
|
|
|
|
case STATE_SENDING_REQUEST:
|
2013-12-23 21:16:01 +05:30
|
|
|
return tr("Refreshing login token...");
|
2013-12-01 06:30:42 +05:30
|
|
|
case STATE_PROCESSING_RESPONSE:
|
2013-12-23 21:16:01 +05:30
|
|
|
return tr("Refreshing login token: Processing response...");
|
2013-12-01 06:30:42 +05:30
|
|
|
default:
|
2014-04-07 00:01:02 +05:30
|
|
|
return YggdrasilTask::getStateMessage();
|
2013-12-01 06:30:42 +05:30
|
|
|
}
|
|
|
|
}
|