From 962639aa6d58d88fae32159ed4e2c70ec120991e Mon Sep 17 00:00:00 2001 From: Andrew Date: Mon, 11 Nov 2013 12:59:59 -0600 Subject: Added data structures for Mojang Account. --- logic/auth/MojangAccount.cpp | 55 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 logic/auth/MojangAccount.cpp (limited to 'logic/auth/MojangAccount.cpp') diff --git a/logic/auth/MojangAccount.cpp b/logic/auth/MojangAccount.cpp new file mode 100644 index 00000000..f796794e --- /dev/null +++ b/logic/auth/MojangAccount.cpp @@ -0,0 +1,55 @@ +/* Copyright 2013 MultiMC Contributors + * + * Authors: Orochimarufan + * + * 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" + +#include + +MojangAccount::MojangAccount(const QString& username, QObject* parent) : + QObject(parent) +{ + // Generate a client token. + m_clientToken = QUuid::createUuid().toString(); + + m_username = username; +} + +MojangAccount::MojangAccount(const QString& username, const QString& clientToken, + const QString& accessToken, QObject* parent) : + QObject(parent) +{ + m_username = username; + m_clientToken = clientToken; + m_accessToken = accessToken; +} + + +QString MojangAccount::username() const +{ + return m_username; +} + +QString MojangAccount::clientToken() const +{ + return m_clientToken; +} + +QString MojangAccount::accessToken() const +{ + return m_accessToken; +} + -- cgit v1.2.3 From ad8aeb0b2bdfd7586beab0be31bc36c64da31092 Mon Sep 17 00:00:00 2001 From: Andrew Date: Thu, 14 Nov 2013 14:32:43 -0600 Subject: Implement auth task's response processing. The authenticate task can now successfully log a user in. --- logic/auth/MojangAccount.cpp | 74 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) (limited to 'logic/auth/MojangAccount.cpp') diff --git a/logic/auth/MojangAccount.cpp b/logic/auth/MojangAccount.cpp index f796794e..8856047e 100644 --- a/logic/auth/MojangAccount.cpp +++ b/logic/auth/MojangAccount.cpp @@ -26,6 +26,8 @@ MojangAccount::MojangAccount(const QString& username, QObject* parent) : m_clientToken = QUuid::createUuid().toString(); m_username = username; + + m_currentProfile = -1; } MojangAccount::MojangAccount(const QString& username, const QString& clientToken, @@ -35,6 +37,8 @@ MojangAccount::MojangAccount(const QString& username, const QString& clientToken m_username = username; m_clientToken = clientToken; m_accessToken = accessToken; + + m_currentProfile = -1; } @@ -48,8 +52,78 @@ 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; +} + + +const QList MojangAccount::profiles() const +{ + return m_profiles; +} + +const AccountProfile* MojangAccount::currentProfile() const +{ + if (m_currentProfile < 0) + return nullptr; + else + return &m_profiles.at(m_currentProfile); +} + +bool MojangAccount::setProfile(const QString& profileId) +{ + const QList& 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); +} + + +AccountProfile::AccountProfile(const QString& id, const QString& name) +{ + m_id = id; + m_name = name; +} + +AccountProfile::AccountProfile(const AccountProfile& other) +{ + m_id = other.m_id; + m_name = other.m_name; +} + +QString AccountProfile::id() const +{ + return m_id; +} + +QString AccountProfile::name() const +{ + return m_name; +} + + -- cgit v1.2.3 From cdca53013990ac85967394529476712e6695bbf9 Mon Sep 17 00:00:00 2001 From: Andrew Date: Mon, 18 Nov 2013 12:05:35 -0600 Subject: Implement account list and account list dialog --- logic/auth/MojangAccount.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'logic/auth/MojangAccount.cpp') diff --git a/logic/auth/MojangAccount.cpp b/logic/auth/MojangAccount.cpp index 8856047e..936046fb 100644 --- a/logic/auth/MojangAccount.cpp +++ b/logic/auth/MojangAccount.cpp @@ -41,6 +41,16 @@ MojangAccount::MojangAccount(const QString& username, const QString& clientToken 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 { -- cgit v1.2.3 From 928e0d0b151a4690a5849c2ec4a44d97338937c5 Mon Sep 17 00:00:00 2001 From: Andrew Date: Tue, 19 Nov 2013 12:53:30 -0600 Subject: Implement saving account list to file Currently it only saves when accounts are added or removed. We'll have to fix this, but we need signals for when account objects change first. --- logic/auth/MojangAccount.cpp | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'logic/auth/MojangAccount.cpp') diff --git a/logic/auth/MojangAccount.cpp b/logic/auth/MojangAccount.cpp index 936046fb..80b88082 100644 --- a/logic/auth/MojangAccount.cpp +++ b/logic/auth/MojangAccount.cpp @@ -19,6 +19,8 @@ #include +#include + MojangAccount::MojangAccount(const QString& username, QObject* parent) : QObject(parent) { @@ -113,6 +115,34 @@ void MojangAccount::loadProfiles(const ProfileList& 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. + 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; + } + + QString username = object.value("username").toString(""); + QString clientToken = object.value("clientToken").toString(""); + QString accessToken = object.value("accessToken").toString(""); + + // TODO: Load profiles? + + return MojangAccountPtr(new MojangAccount(username, clientToken, accessToken)); +} + +QJsonObject MojangAccount::saveToJson() +{ + QJsonObject json; + json.insert("username", username()); + json.insert("clientToken", clientToken()); + json.insert("accessToken", accessToken()); + // TODO: Save profiles? + return json; +} + AccountProfile::AccountProfile(const QString& id, const QString& name) { -- cgit v1.2.3 From abf8408911c057d8aafe90790f5d2f5de0e1d97c Mon Sep 17 00:00:00 2001 From: Andrew Date: Wed, 20 Nov 2013 18:31:15 -0600 Subject: Nuke and pave the old login system Also, account list now saves profile lists. --- logic/auth/MojangAccount.cpp | 55 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 51 insertions(+), 4 deletions(-) (limited to 'logic/auth/MojangAccount.cpp') diff --git a/logic/auth/MojangAccount.cpp b/logic/auth/MojangAccount.cpp index 80b88082..4875e5f7 100644 --- a/logic/auth/MojangAccount.cpp +++ b/logic/auth/MojangAccount.cpp @@ -18,6 +18,8 @@ #include "MojangAccount.h" #include +#include +#include #include @@ -89,7 +91,12 @@ const QList MojangAccount::profiles() const const AccountProfile* MojangAccount::currentProfile() const { if (m_currentProfile < 0) - return nullptr; + { + if (m_profiles.length() > 0) + return &m_profiles.at(0); + else + return nullptr; + } else return &m_profiles.at(m_currentProfile); } @@ -128,9 +135,36 @@ MojangAccountPtr MojangAccount::loadFromJson(const QJsonObject& object) QString clientToken = object.value("clientToken").toString(""); QString accessToken = object.value("accessToken").toString(""); - // TODO: Load profiles? + 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; + } + + ProfileList profiles; + 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; + } + profiles.append(AccountProfile(id, name)); + } + + MojangAccountPtr account(new MojangAccount(username, clientToken, accessToken)); + account->loadProfiles(profiles); - return MojangAccountPtr(new MojangAccount(username, clientToken, accessToken)); + // Get the currently selected profile. + QString currentProfile = object.value("activeProfile").toString(""); + if (!currentProfile.isEmpty()) + account->setProfile(currentProfile); + + return account; } QJsonObject MojangAccount::saveToJson() @@ -139,7 +173,20 @@ QJsonObject MojangAccount::saveToJson() json.insert("username", username()); json.insert("clientToken", clientToken()); json.insert("accessToken", accessToken()); - // TODO: Save profiles? + + QJsonArray profileArray; + for (AccountProfile profile : m_profiles) + { + QJsonObject profileObj; + 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()); + return json; } -- cgit v1.2.3