summaryrefslogtreecommitdiffstats
path: root/logic/auth/MojangAccount.cpp
diff options
context:
space:
mode:
authorPetr Mrázek <peterix@gmail.com>2013-12-05 02:39:52 +0100
committerPetr Mrázek <peterix@gmail.com>2013-12-05 02:39:52 +0100
commitf028aa76bc5d28b7fc4d1ea4e194895690e9944e (patch)
tree5157fb0c4675c7f6f14c3947ab685f3b87d45610 /logic/auth/MojangAccount.cpp
parent613699b3626aea750093ab7eaaeccaa28c0e87c6 (diff)
downloadMultiMC-f028aa76bc5d28b7fc4d1ea4e194895690e9944e.tar
MultiMC-f028aa76bc5d28b7fc4d1ea4e194895690e9944e.tar.gz
MultiMC-f028aa76bc5d28b7fc4d1ea4e194895690e9944e.tar.lz
MultiMC-f028aa76bc5d28b7fc4d1ea4e194895690e9944e.tar.xz
MultiMC-f028aa76bc5d28b7fc4d1ea4e194895690e9944e.zip
Offline mode support, part 1
Refactor MojangAccount so it exposes a less generic interface and supports login. Hide the ugly details. Yggdrasil tasks are now only used from MojangAccount.
Diffstat (limited to 'logic/auth/MojangAccount.cpp')
-rw-r--r--logic/auth/MojangAccount.cpp196
1 files changed, 72 insertions, 124 deletions
diff --git a/logic/auth/MojangAccount.cpp b/logic/auth/MojangAccount.cpp
index 4a61cf19..9ab71077 100644
--- a/logic/auth/MojangAccount.cpp
+++ b/logic/auth/MojangAccount.cpp
@@ -16,113 +16,16 @@
*/
#include "MojangAccount.h"
+#include "flows/RefreshTask.h"
+#include "flows/AuthenticateTask.h"
#include <QUuid>
#include <QJsonObject>
#include <QJsonArray>
+#include <QRegExp>
#include <logger/QsLog.h>
-MojangAccount::MojangAccount(const QString &username, QObject *parent) : QObject(parent)
-{
- // Generate a client token.
- m_clientToken = QUuid::createUuid().toString();
-
- m_username = username;
-
- m_currentProfile = -1;
-}
-
-MojangAccount::MojangAccount(const QString &username, const QString &clientToken,
- const QString &accessToken, QObject *parent)
- : QObject(parent)
-{
- m_username = username;
- m_clientToken = clientToken;
- m_accessToken = accessToken;
-
- m_currentProfile = -1;
-}
-
-MojangAccount::MojangAccount(const MojangAccount &other, QObject *parent)
-{
- m_username = other.username();
- m_clientToken = other.clientToken();
- m_accessToken = other.accessToken();
-
- m_profiles = other.m_profiles;
- m_currentProfile = other.m_currentProfile;
-}
-
-QString MojangAccount::username() const
-{
- return m_username;
-}
-
-QString MojangAccount::clientToken() const
-{
- return m_clientToken;
-}
-
-void MojangAccount::setClientToken(const QString &clientToken)
-{
- m_clientToken = clientToken;
-}
-
-QString MojangAccount::accessToken() const
-{
- return m_accessToken;
-}
-
-void MojangAccount::setAccessToken(const QString &accessToken)
-{
- m_accessToken = accessToken;
-}
-
-QString MojangAccount::sessionId() const
-{
- return "token:" + m_accessToken + ":" + currentProfile()->id();
-}
-
-const QList<AccountProfile> MojangAccount::profiles() const
-{
- return m_profiles;
-}
-
-const AccountProfile *MojangAccount::currentProfile() const
-{
- if (m_currentProfile < 0)
- {
- if (m_profiles.length() > 0)
- return &m_profiles.at(0);
- else
- return nullptr;
- }
- else
- return &m_profiles.at(m_currentProfile);
-}
-
-bool MojangAccount::setProfile(const QString &profileId)
-{
- const QList<AccountProfile> &profiles = this->profiles();
- for (int i = 0; i < profiles.length(); i++)
- {
- if (profiles.at(i).id() == profileId)
- {
- m_currentProfile = i;
- return true;
- }
- }
- return false;
-}
-
-void MojangAccount::loadProfiles(const ProfileList &profiles)
-{
- m_profiles.clear();
- for (auto profile : profiles)
- m_profiles.append(profile);
-}
-
MojangAccountPtr MojangAccount::loadFromJson(const QJsonObject &object)
{
// The JSON object must at least have a username for it to be valid.
@@ -143,7 +46,7 @@ MojangAccountPtr MojangAccount::loadFromJson(const QJsonObject &object)
return nullptr;
}
- ProfileList profiles;
+ QList<AccountProfile> profiles;
for (QJsonValue profileVal : profileArray)
{
QJsonObject profileObject = profileVal.toObject();
@@ -154,67 +57,112 @@ MojangAccountPtr MojangAccount::loadFromJson(const QJsonObject &object)
QLOG_WARN() << "Unable to load a profile because it was missing an ID or a name.";
continue;
}
- profiles.append(AccountProfile(id, name));
+ profiles.append({id, name});
}
- MojangAccountPtr account(new MojangAccount(username, clientToken, accessToken));
- account->loadProfiles(profiles);
+ MojangAccountPtr account(new MojangAccount());
+ account->m_username = username;
+ account->m_clientToken = clientToken;
+ account->m_accessToken = accessToken;
+ account->m_profiles = profiles;
// Get the currently selected profile.
QString currentProfile = object.value("activeProfile").toString("");
if (!currentProfile.isEmpty())
- account->setProfile(currentProfile);
+ account->setCurrentProfile(currentProfile);
+
+ return account;
+}
+MojangAccountPtr MojangAccount::createFromUsername(const QString& username)
+{
+ MojangAccountPtr account(new MojangAccount());
+ account->m_clientToken = QUuid::createUuid().toString().remove(QRegExp("[{}-]"));
+ account->m_username = username;
return account;
}
-QJsonObject MojangAccount::saveToJson()
+QJsonObject MojangAccount::saveToJson() const
{
QJsonObject json;
- json.insert("username", username());
- json.insert("clientToken", clientToken());
- json.insert("accessToken", accessToken());
+ json.insert("username", m_username);
+ json.insert("clientToken", m_clientToken);
+ json.insert("accessToken", m_accessToken);
QJsonArray profileArray;
for (AccountProfile profile : m_profiles)
{
QJsonObject profileObj;
- profileObj.insert("id", profile.id());
- profileObj.insert("name", profile.name());
+ profileObj.insert("id", profile.id);
+ profileObj.insert("name", profile.name);
profileArray.append(profileObj);
}
json.insert("profiles", profileArray);
- if (currentProfile() != nullptr)
- json.insert("activeProfile", currentProfile()->id());
+ if (m_currentProfile != -1)
+ json.insert("activeProfile", currentProfile()->id);
return json;
}
+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;
+}
-AccountProfile::AccountProfile(const QString& id, const QString& name)
+const AccountProfile* MojangAccount::currentProfile() const
{
- m_id = id;
- m_name = name;
+ if(m_currentProfile == -1)
+ return nullptr;
+ return &m_profiles[m_currentProfile];
}
-AccountProfile::AccountProfile(const AccountProfile &other)
+AccountStatus MojangAccount::accountStatus() const
{
- m_id = other.m_id;
- m_name = other.m_name;
+ if(m_accessToken.isEmpty())
+ return NotVerified;
+ if(!m_online)
+ return Verified;
+ return Online;
}
-QString AccountProfile::id() const
+bool MojangAccount::login(QString password)
{
- return m_id;
+ if(m_currentTask)
+ return false;
+ if(password.isEmpty())
+ {
+ m_currentTask.reset(new RefreshTask(this, this));
+ }
+ else
+ {
+ m_currentTask.reset(new AuthenticateTask(this, password, this));
+ }
+ connect(m_currentTask.get(), SIGNAL(succeeded()), SLOT(authSucceeded()));
+ connect(m_currentTask.get(), SIGNAL(failed(QString)), SLOT(authFailed(QString)));
+ m_currentTask->start();
+ return true;
}
-QString AccountProfile::name() const
+void MojangAccount::authSucceeded()
{
- return m_name;
+ m_online = true;
+ m_currentTask.reset();
+ emit changed();
}
-void MojangAccount::propagateChange()
+void MojangAccount::authFailed(QString reason)
{
+ m_online = false;
+ m_accessToken = QString();
+ m_currentTask.reset();
emit changed();
}