2013-11-12 00:29:59 +05:30
|
|
|
/* Copyright 2013 MultiMC Contributors
|
|
|
|
*
|
|
|
|
* Authors: Orochimarufan <orochimarufan.x3@gmail.com>
|
|
|
|
*
|
|
|
|
* 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 "MojangAccount.h"
|
2013-12-05 07:09:52 +05:30
|
|
|
#include "flows/RefreshTask.h"
|
|
|
|
#include "flows/AuthenticateTask.h"
|
2013-11-12 00:29:59 +05:30
|
|
|
|
|
|
|
#include <QUuid>
|
2013-11-21 06:01:15 +05:30
|
|
|
#include <QJsonObject>
|
|
|
|
#include <QJsonArray>
|
2013-12-05 07:09:52 +05:30
|
|
|
#include <QRegExp>
|
2013-11-12 00:29:59 +05:30
|
|
|
|
2013-11-20 00:23:30 +05:30
|
|
|
#include <logger/QsLog.h>
|
|
|
|
|
2013-12-01 06:30:42 +05:30
|
|
|
MojangAccountPtr MojangAccount::loadFromJson(const QJsonObject &object)
|
2013-11-20 00:23:30 +05:30
|
|
|
{
|
|
|
|
// The JSON object must at least have a username for it to be valid.
|
|
|
|
if (!object.value("username").isString())
|
|
|
|
{
|
|
|
|
QLOG_ERROR() << "Can't load Mojang account info from JSON object. Username field is missing or of the wrong type.";
|
|
|
|
return nullptr;
|
|
|
|
}
|
2013-12-01 06:30:42 +05:30
|
|
|
|
2013-11-20 00:23:30 +05:30
|
|
|
QString username = object.value("username").toString("");
|
|
|
|
QString clientToken = object.value("clientToken").toString("");
|
|
|
|
QString accessToken = object.value("accessToken").toString("");
|
|
|
|
|
2013-11-21 06:01:15 +05:30
|
|
|
QJsonArray profileArray = object.value("profiles").toArray();
|
|
|
|
if (profileArray.size() < 1)
|
|
|
|
{
|
|
|
|
QLOG_ERROR() << "Can't load Mojang account with username \"" << username << "\". No profiles found.";
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2013-12-05 07:09:52 +05:30
|
|
|
QList<AccountProfile> profiles;
|
2013-11-21 06:01:15 +05:30
|
|
|
for (QJsonValue profileVal : profileArray)
|
|
|
|
{
|
|
|
|
QJsonObject profileObject = profileVal.toObject();
|
|
|
|
QString id = profileObject.value("id").toString("");
|
|
|
|
QString name = profileObject.value("name").toString("");
|
|
|
|
if (id.isEmpty() || name.isEmpty())
|
|
|
|
{
|
|
|
|
QLOG_WARN() << "Unable to load a profile because it was missing an ID or a name.";
|
|
|
|
continue;
|
|
|
|
}
|
2013-12-05 07:09:52 +05:30
|
|
|
profiles.append({id, name});
|
2013-11-21 06:01:15 +05:30
|
|
|
}
|
|
|
|
|
2013-12-05 07:09:52 +05:30
|
|
|
MojangAccountPtr account(new MojangAccount());
|
|
|
|
account->m_username = username;
|
|
|
|
account->m_clientToken = clientToken;
|
|
|
|
account->m_accessToken = accessToken;
|
|
|
|
account->m_profiles = profiles;
|
2013-11-20 00:23:30 +05:30
|
|
|
|
2013-11-21 06:01:15 +05:30
|
|
|
// Get the currently selected profile.
|
|
|
|
QString currentProfile = object.value("activeProfile").toString("");
|
|
|
|
if (!currentProfile.isEmpty())
|
2013-12-05 07:09:52 +05:30
|
|
|
account->setCurrentProfile(currentProfile);
|
|
|
|
|
|
|
|
return account;
|
|
|
|
}
|
2013-11-21 06:01:15 +05:30
|
|
|
|
2013-12-05 07:09:52 +05:30
|
|
|
MojangAccountPtr MojangAccount::createFromUsername(const QString& username)
|
|
|
|
{
|
|
|
|
MojangAccountPtr account(new MojangAccount());
|
|
|
|
account->m_clientToken = QUuid::createUuid().toString().remove(QRegExp("[{}-]"));
|
|
|
|
account->m_username = username;
|
2013-11-21 06:01:15 +05:30
|
|
|
return account;
|
2013-11-20 00:23:30 +05:30
|
|
|
}
|
|
|
|
|
2013-12-05 07:09:52 +05:30
|
|
|
QJsonObject MojangAccount::saveToJson() const
|
2013-11-20 00:23:30 +05:30
|
|
|
{
|
|
|
|
QJsonObject json;
|
2013-12-05 07:09:52 +05:30
|
|
|
json.insert("username", m_username);
|
|
|
|
json.insert("clientToken", m_clientToken);
|
|
|
|
json.insert("accessToken", m_accessToken);
|
2013-11-21 06:01:15 +05:30
|
|
|
|
|
|
|
QJsonArray profileArray;
|
|
|
|
for (AccountProfile profile : m_profiles)
|
|
|
|
{
|
|
|
|
QJsonObject profileObj;
|
2013-12-05 07:09:52 +05:30
|
|
|
profileObj.insert("id", profile.id);
|
|
|
|
profileObj.insert("name", profile.name);
|
2013-11-21 06:01:15 +05:30
|
|
|
profileArray.append(profileObj);
|
|
|
|
}
|
|
|
|
json.insert("profiles", profileArray);
|
|
|
|
|
2013-12-05 07:09:52 +05:30
|
|
|
if (m_currentProfile != -1)
|
|
|
|
json.insert("activeProfile", currentProfile()->id);
|
2013-11-21 06:01:15 +05:30
|
|
|
|
2013-11-20 00:23:30 +05:30
|
|
|
return json;
|
|
|
|
}
|
|
|
|
|
2013-12-05 07:09:52 +05:30
|
|
|
bool MojangAccount::setCurrentProfile(const QString &profileId)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < m_profiles.length(); i++)
|
|
|
|
{
|
|
|
|
if (m_profiles[i].id == profileId)
|
|
|
|
{
|
|
|
|
m_currentProfile = i;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2013-11-15 02:02:43 +05:30
|
|
|
|
2013-12-05 07:09:52 +05:30
|
|
|
const AccountProfile* MojangAccount::currentProfile() const
|
2013-11-15 02:02:43 +05:30
|
|
|
{
|
2013-12-05 07:09:52 +05:30
|
|
|
if(m_currentProfile == -1)
|
|
|
|
return nullptr;
|
|
|
|
return &m_profiles[m_currentProfile];
|
2013-11-15 02:02:43 +05:30
|
|
|
}
|
|
|
|
|
2013-12-05 07:09:52 +05:30
|
|
|
AccountStatus MojangAccount::accountStatus() const
|
2013-11-15 02:02:43 +05:30
|
|
|
{
|
2013-12-05 07:09:52 +05:30
|
|
|
if(m_accessToken.isEmpty())
|
|
|
|
return NotVerified;
|
|
|
|
if(!m_online)
|
|
|
|
return Verified;
|
|
|
|
return Online;
|
2013-11-15 02:02:43 +05:30
|
|
|
}
|
|
|
|
|
2013-12-08 22:04:45 +05:30
|
|
|
std::shared_ptr<Task> MojangAccount::login(QString password)
|
2013-11-15 02:02:43 +05:30
|
|
|
{
|
2013-12-05 07:09:52 +05:30
|
|
|
if(m_currentTask)
|
2013-12-08 22:04:45 +05:30
|
|
|
return m_currentTask;
|
2013-12-05 07:09:52 +05:30
|
|
|
if(password.isEmpty())
|
|
|
|
{
|
2013-12-08 22:04:45 +05:30
|
|
|
m_currentTask.reset(new RefreshTask(this));
|
2013-12-05 07:09:52 +05:30
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-08 22:04:45 +05:30
|
|
|
m_currentTask.reset(new AuthenticateTask(this, password));
|
2013-12-05 07:09:52 +05:30
|
|
|
}
|
|
|
|
connect(m_currentTask.get(), SIGNAL(succeeded()), SLOT(authSucceeded()));
|
|
|
|
connect(m_currentTask.get(), SIGNAL(failed(QString)), SLOT(authFailed(QString)));
|
2013-12-08 22:04:45 +05:30
|
|
|
return m_currentTask;
|
2013-11-15 02:02:43 +05:30
|
|
|
}
|
|
|
|
|
2013-12-05 07:09:52 +05:30
|
|
|
void MojangAccount::authSucceeded()
|
2013-11-15 02:02:43 +05:30
|
|
|
{
|
2013-12-05 07:09:52 +05:30
|
|
|
m_online = true;
|
|
|
|
m_currentTask.reset();
|
|
|
|
emit changed();
|
2013-11-15 02:02:43 +05:30
|
|
|
}
|
|
|
|
|
2013-12-05 07:09:52 +05:30
|
|
|
void MojangAccount::authFailed(QString reason)
|
2013-12-01 06:30:42 +05:30
|
|
|
{
|
2013-12-08 22:04:45 +05:30
|
|
|
// This is emitted when the yggdrasil tasks time out or are cancelled.
|
|
|
|
// -> we treat the error as no-op
|
|
|
|
if(reason != "Yggdrasil task cancelled.")
|
|
|
|
{
|
|
|
|
m_online = false;
|
|
|
|
m_accessToken = QString();
|
|
|
|
emit changed();
|
|
|
|
}
|
2013-12-05 07:09:52 +05:30
|
|
|
m_currentTask.reset();
|
2013-12-01 06:30:42 +05:30
|
|
|
}
|