2015-02-02 22:25:30 +00:00
|
|
|
/* Copyright 2013-2015 MultiMC Contributors
|
2013-11-11 18:59:59 +00:00
|
|
|
*
|
|
|
|
* 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 01:39:52 +00:00
|
|
|
#include "flows/RefreshTask.h"
|
|
|
|
#include "flows/AuthenticateTask.h"
|
2013-11-11 18:59:59 +00:00
|
|
|
|
|
|
|
#include <QUuid>
|
2013-11-21 00:31:15 +00:00
|
|
|
#include <QJsonObject>
|
|
|
|
#include <QJsonArray>
|
2013-12-05 01:39:52 +00:00
|
|
|
#include <QRegExp>
|
2013-12-13 01:47:59 +00:00
|
|
|
#include <QStringList>
|
2014-01-27 02:00:49 +00:00
|
|
|
#include <QJsonDocument>
|
2013-11-11 18:59:59 +00:00
|
|
|
|
2013-11-19 18:53:30 +00:00
|
|
|
#include <logger/QsLog.h>
|
|
|
|
|
2013-12-01 01:00:42 +00:00
|
|
|
MojangAccountPtr MojangAccount::loadFromJson(const QJsonObject &object)
|
2013-11-19 18:53:30 +00:00
|
|
|
{
|
|
|
|
// The JSON object must at least have a username for it to be valid.
|
|
|
|
if (!object.value("username").isString())
|
|
|
|
{
|
2013-12-22 18:47:58 +00:00
|
|
|
QLOG_ERROR() << "Can't load Mojang account info from JSON object. Username field is "
|
|
|
|
"missing or of the wrong type.";
|
2013-11-19 18:53:30 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
2013-12-01 01:00:42 +00:00
|
|
|
|
2013-11-19 18:53:30 +00:00
|
|
|
QString username = object.value("username").toString("");
|
|
|
|
QString clientToken = object.value("clientToken").toString("");
|
|
|
|
QString accessToken = object.value("accessToken").toString("");
|
|
|
|
|
2013-11-21 00:31:15 +00:00
|
|
|
QJsonArray profileArray = object.value("profiles").toArray();
|
|
|
|
if (profileArray.size() < 1)
|
|
|
|
{
|
2013-12-22 18:47:58 +00:00
|
|
|
QLOG_ERROR() << "Can't load Mojang account with username \"" << username
|
|
|
|
<< "\". No profiles found.";
|
2013-11-21 00:31:15 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2013-12-05 01:39:52 +00:00
|
|
|
QList<AccountProfile> profiles;
|
2013-11-21 00:31:15 +00:00
|
|
|
for (QJsonValue profileVal : profileArray)
|
|
|
|
{
|
|
|
|
QJsonObject profileObject = profileVal.toObject();
|
|
|
|
QString id = profileObject.value("id").toString("");
|
|
|
|
QString name = profileObject.value("name").toString("");
|
2013-12-13 01:47:59 +00:00
|
|
|
bool legacy = profileObject.value("legacy").toBool(false);
|
2013-11-21 00:31:15 +00:00
|
|
|
if (id.isEmpty() || name.isEmpty())
|
|
|
|
{
|
|
|
|
QLOG_WARN() << "Unable to load a profile because it was missing an ID or a name.";
|
|
|
|
continue;
|
|
|
|
}
|
2013-12-13 01:47:59 +00:00
|
|
|
profiles.append({id, name, legacy});
|
2013-11-21 00:31:15 +00:00
|
|
|
}
|
|
|
|
|
2013-12-05 01:39:52 +00:00
|
|
|
MojangAccountPtr account(new MojangAccount());
|
2013-12-22 18:47:58 +00:00
|
|
|
if (object.value("user").isObject())
|
2013-12-13 01:47:59 +00:00
|
|
|
{
|
|
|
|
User u;
|
|
|
|
QJsonObject userStructure = object.value("user").toObject();
|
|
|
|
u.id = userStructure.value("id").toString();
|
2013-12-14 00:18:54 +00:00
|
|
|
/*
|
2013-12-13 01:47:59 +00:00
|
|
|
QJsonObject propMap = userStructure.value("properties").toObject();
|
|
|
|
for(auto key: propMap.keys())
|
|
|
|
{
|
|
|
|
auto values = propMap.operator[](key).toArray();
|
|
|
|
for(auto value: values)
|
|
|
|
u.properties.insert(key, value.toString());
|
|
|
|
}
|
2013-12-14 00:18:54 +00:00
|
|
|
*/
|
2013-12-13 01:47:59 +00:00
|
|
|
account->m_user = u;
|
|
|
|
}
|
2013-12-05 01:39:52 +00:00
|
|
|
account->m_username = username;
|
|
|
|
account->m_clientToken = clientToken;
|
|
|
|
account->m_accessToken = accessToken;
|
|
|
|
account->m_profiles = profiles;
|
2013-11-19 18:53:30 +00:00
|
|
|
|
2013-11-21 00:31:15 +00:00
|
|
|
// Get the currently selected profile.
|
|
|
|
QString currentProfile = object.value("activeProfile").toString("");
|
|
|
|
if (!currentProfile.isEmpty())
|
2013-12-05 01:39:52 +00:00
|
|
|
account->setCurrentProfile(currentProfile);
|
|
|
|
|
|
|
|
return account;
|
|
|
|
}
|
2013-11-21 00:31:15 +00:00
|
|
|
|
2013-12-22 18:47:58 +00:00
|
|
|
MojangAccountPtr MojangAccount::createFromUsername(const QString &username)
|
2013-12-05 01:39:52 +00:00
|
|
|
{
|
|
|
|
MojangAccountPtr account(new MojangAccount());
|
|
|
|
account->m_clientToken = QUuid::createUuid().toString().remove(QRegExp("[{}-]"));
|
|
|
|
account->m_username = username;
|
2013-11-21 00:31:15 +00:00
|
|
|
return account;
|
2013-11-19 18:53:30 +00:00
|
|
|
}
|
|
|
|
|
2013-12-05 01:39:52 +00:00
|
|
|
QJsonObject MojangAccount::saveToJson() const
|
2013-11-19 18:53:30 +00:00
|
|
|
{
|
|
|
|
QJsonObject json;
|
2013-12-05 01:39:52 +00:00
|
|
|
json.insert("username", m_username);
|
|
|
|
json.insert("clientToken", m_clientToken);
|
|
|
|
json.insert("accessToken", m_accessToken);
|
2013-11-21 00:31:15 +00:00
|
|
|
|
|
|
|
QJsonArray profileArray;
|
|
|
|
for (AccountProfile profile : m_profiles)
|
|
|
|
{
|
|
|
|
QJsonObject profileObj;
|
2013-12-05 01:39:52 +00:00
|
|
|
profileObj.insert("id", profile.id);
|
|
|
|
profileObj.insert("name", profile.name);
|
2013-12-13 01:47:59 +00:00
|
|
|
profileObj.insert("legacy", profile.legacy);
|
2013-11-21 00:31:15 +00:00
|
|
|
profileArray.append(profileObj);
|
|
|
|
}
|
|
|
|
json.insert("profiles", profileArray);
|
|
|
|
|
2013-12-13 01:47:59 +00:00
|
|
|
QJsonObject userStructure;
|
|
|
|
{
|
|
|
|
userStructure.insert("id", m_user.id);
|
2013-12-14 00:18:54 +00:00
|
|
|
/*
|
2013-12-13 01:47:59 +00:00
|
|
|
QJsonObject userAttrs;
|
|
|
|
for(auto key: m_user.properties.keys())
|
|
|
|
{
|
|
|
|
auto array = QJsonArray::fromStringList(m_user.properties.values(key));
|
|
|
|
userAttrs.insert(key, array);
|
|
|
|
}
|
|
|
|
userStructure.insert("properties", userAttrs);
|
2013-12-14 00:18:54 +00:00
|
|
|
*/
|
2013-12-13 01:47:59 +00:00
|
|
|
}
|
|
|
|
json.insert("user", userStructure);
|
|
|
|
|
2013-12-05 01:39:52 +00:00
|
|
|
if (m_currentProfile != -1)
|
|
|
|
json.insert("activeProfile", currentProfile()->id);
|
2013-11-21 00:31:15 +00:00
|
|
|
|
2013-11-19 18:53:30 +00:00
|
|
|
return json;
|
|
|
|
}
|
|
|
|
|
2013-12-05 01:39:52 +00:00
|
|
|
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-14 20:32:43 +00:00
|
|
|
|
2013-12-22 18:47:58 +00:00
|
|
|
const AccountProfile *MojangAccount::currentProfile() const
|
2013-11-14 20:32:43 +00:00
|
|
|
{
|
2013-12-22 18:47:58 +00:00
|
|
|
if (m_currentProfile == -1)
|
2013-12-05 01:39:52 +00:00
|
|
|
return nullptr;
|
|
|
|
return &m_profiles[m_currentProfile];
|
2013-11-14 20:32:43 +00:00
|
|
|
}
|
|
|
|
|
2013-12-05 01:39:52 +00:00
|
|
|
AccountStatus MojangAccount::accountStatus() const
|
2013-11-14 20:32:43 +00:00
|
|
|
{
|
2013-12-22 18:47:58 +00:00
|
|
|
if (m_accessToken.isEmpty())
|
2013-12-05 01:39:52 +00:00
|
|
|
return NotVerified;
|
2014-01-27 02:00:49 +00:00
|
|
|
else
|
2013-12-05 01:39:52 +00:00
|
|
|
return Verified;
|
2013-11-14 20:32:43 +00:00
|
|
|
}
|
|
|
|
|
2014-01-27 02:00:49 +00:00
|
|
|
std::shared_ptr<YggdrasilTask> MojangAccount::login(AuthSessionPtr session,
|
|
|
|
QString password)
|
2013-11-14 20:32:43 +00:00
|
|
|
{
|
2014-01-27 02:00:49 +00:00
|
|
|
Q_ASSERT(m_currentTask.get() == nullptr);
|
|
|
|
|
|
|
|
// take care of the true offline status
|
|
|
|
if (accountStatus() == NotVerified && password.isEmpty())
|
|
|
|
{
|
|
|
|
if (session)
|
|
|
|
{
|
|
|
|
session->status = AuthSession::RequiresPassword;
|
|
|
|
fillSession(session);
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2013-12-22 18:47:58 +00:00
|
|
|
if (password.isEmpty())
|
2013-12-05 01:39:52 +00:00
|
|
|
{
|
2013-12-08 16:34:45 +00:00
|
|
|
m_currentTask.reset(new RefreshTask(this));
|
2013-12-05 01:39:52 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-08 16:34:45 +00:00
|
|
|
m_currentTask.reset(new AuthenticateTask(this, password));
|
2013-12-05 01:39:52 +00:00
|
|
|
}
|
2014-01-27 02:00:49 +00:00
|
|
|
m_currentTask->assignSession(session);
|
|
|
|
|
2013-12-05 01:39:52 +00:00
|
|
|
connect(m_currentTask.get(), SIGNAL(succeeded()), SLOT(authSucceeded()));
|
|
|
|
connect(m_currentTask.get(), SIGNAL(failed(QString)), SLOT(authFailed(QString)));
|
2013-12-08 16:34:45 +00:00
|
|
|
return m_currentTask;
|
2013-11-14 20:32:43 +00:00
|
|
|
}
|
|
|
|
|
2013-12-05 01:39:52 +00:00
|
|
|
void MojangAccount::authSucceeded()
|
2013-11-14 20:32:43 +00:00
|
|
|
{
|
2014-01-27 02:00:49 +00:00
|
|
|
auto session = m_currentTask->getAssignedSession();
|
|
|
|
if (session)
|
|
|
|
{
|
|
|
|
session->status =
|
|
|
|
session->wants_online ? AuthSession::PlayableOnline : AuthSession::PlayableOffline;
|
|
|
|
fillSession(session);
|
|
|
|
session->auth_server_online = true;
|
|
|
|
}
|
2013-12-05 01:39:52 +00:00
|
|
|
m_currentTask.reset();
|
|
|
|
emit changed();
|
2013-11-14 20:32:43 +00:00
|
|
|
}
|
|
|
|
|
2013-12-05 01:39:52 +00:00
|
|
|
void MojangAccount::authFailed(QString reason)
|
2013-12-01 01:00:42 +00:00
|
|
|
{
|
2014-01-27 02:00:49 +00:00
|
|
|
auto session = m_currentTask->getAssignedSession();
|
2013-12-08 16:34:45 +00:00
|
|
|
// This is emitted when the yggdrasil tasks time out or are cancelled.
|
|
|
|
// -> we treat the error as no-op
|
2014-04-06 18:31:02 +00:00
|
|
|
if (m_currentTask->state() == YggdrasilTask::STATE_FAILED_SOFT)
|
2014-01-18 21:11:33 +00:00
|
|
|
{
|
2014-01-27 02:00:49 +00:00
|
|
|
if (session)
|
|
|
|
{
|
|
|
|
session->status = accountStatus() == Verified ? AuthSession::PlayableOffline
|
|
|
|
: AuthSession::RequiresPassword;
|
|
|
|
session->auth_server_online = false;
|
|
|
|
fillSession(session);
|
|
|
|
}
|
2014-01-18 21:11:33 +00:00
|
|
|
}
|
|
|
|
else
|
2013-12-08 16:34:45 +00:00
|
|
|
{
|
|
|
|
m_accessToken = QString();
|
|
|
|
emit changed();
|
2014-01-27 02:00:49 +00:00
|
|
|
if (session)
|
|
|
|
{
|
|
|
|
session->status = AuthSession::RequiresPassword;
|
|
|
|
session->auth_server_online = true;
|
|
|
|
fillSession(session);
|
|
|
|
}
|
2013-12-08 16:34:45 +00:00
|
|
|
}
|
2013-12-05 01:39:52 +00:00
|
|
|
m_currentTask.reset();
|
2013-12-01 01:00:42 +00:00
|
|
|
}
|
2014-01-27 02:00:49 +00:00
|
|
|
|
|
|
|
void MojangAccount::fillSession(AuthSessionPtr session)
|
|
|
|
{
|
|
|
|
// the user name. you have to have an user name
|
|
|
|
session->username = m_username;
|
|
|
|
// volatile auth token
|
|
|
|
session->access_token = m_accessToken;
|
|
|
|
// the semi-permanent client token
|
|
|
|
session->client_token = m_clientToken;
|
|
|
|
if (currentProfile())
|
|
|
|
{
|
|
|
|
// profile name
|
|
|
|
session->player_name = currentProfile()->name;
|
|
|
|
// profile ID
|
|
|
|
session->uuid = currentProfile()->id;
|
|
|
|
// 'legacy' or 'mojang', depending on account type
|
|
|
|
session->user_type = currentProfile()->legacy ? "legacy" : "mojang";
|
|
|
|
if (!session->access_token.isEmpty())
|
|
|
|
{
|
|
|
|
session->session = "token:" + m_accessToken + ":" + m_profiles[m_currentProfile].id;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
session->session = "-";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
session->player_name = "Player";
|
|
|
|
session->session = "-";
|
|
|
|
}
|
|
|
|
session->u = user();
|
|
|
|
}
|