From f27a6c39ea796f946893ced1d9f80441ad9aa18c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Mr=C3=A1zek?= Date: Sun, 1 Dec 2013 02:00:42 +0100 Subject: [PATCH] Fix login and startup logging issues Auth uses the refresh endpoint instead of validate. This means less password entering. Console will now only autoscroll when already scrolled all the way down. Better conformance with the Yggdrasil auth protocol (not complete yet, but Mojang launcher isn't complete either). Fix bug that prevented saving the account data (uninitialized variable). Accounts can now trigger account list saving, this is used for the refresh endpoint. --- CMakeLists.txt | 15 +- gui/ConsoleWindow.cpp | 21 ++- gui/ConsoleWindow.h | 21 +-- gui/MainWindow.cpp | 32 ++-- gui/dialogs/AccountListDialog.cpp | 17 +-- gui/dialogs/AccountListDialog.h | 6 +- gui/dialogs/AccountSelectDialog.cpp | 8 +- gui/dialogs/AccountSelectDialog.h | 10 +- gui/dialogs/EditAccountDialog.cpp | 10 +- gui/dialogs/EditAccountDialog.h | 11 +- logic/MinecraftProcess.cpp | 30 +++- logic/MinecraftProcess.h | 14 +- logic/auth/MojangAccount.cpp | 36 ++--- logic/auth/MojangAccount.h | 55 ++++--- logic/auth/YggdrasilTask.cpp | 23 ++- logic/auth/YggdrasilTask.h | 22 +-- logic/auth/{ => flows}/AuthenticateTask.cpp | 88 ++++------- logic/auth/{ => flows}/AuthenticateTask.h | 12 +- logic/auth/flows/InvalidateTask.cpp | 0 logic/auth/flows/InvalidateTask.h | 0 logic/auth/flows/RefreshTask.cpp | 156 ++++++++++++++++++++ logic/auth/flows/RefreshTask.h | 43 ++++++ logic/auth/{ => flows}/ValidateTask.cpp | 8 +- logic/auth/{ => flows}/ValidateTask.h | 9 +- logic/lists/MojangAccountList.cpp | 33 +++-- logic/lists/MojangAccountList.h | 30 ++-- test.cpp | 60 -------- 27 files changed, 471 insertions(+), 299 deletions(-) rename logic/auth/{ => flows}/AuthenticateTask.cpp (82%) rename logic/auth/{ => flows}/AuthenticateTask.h (87%) create mode 100644 logic/auth/flows/InvalidateTask.cpp create mode 100644 logic/auth/flows/InvalidateTask.h create mode 100644 logic/auth/flows/RefreshTask.cpp create mode 100644 logic/auth/flows/RefreshTask.h rename logic/auth/{ => flows}/ValidateTask.cpp (91%) rename logic/auth/{ => flows}/ValidateTask.h (93%) delete mode 100644 test.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index e8292ce6..a33e4736 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -276,11 +276,14 @@ logic/auth/MojangAccount.h logic/auth/MojangAccount.cpp logic/auth/YggdrasilTask.h logic/auth/YggdrasilTask.cpp -logic/auth/AuthenticateTask.h -logic/auth/AuthenticateTask.cpp -logic/auth/ValidateTask.h -logic/auth/ValidateTask.cpp - +logic/auth/flows/AuthenticateTask.h +logic/auth/flows/AuthenticateTask.cpp +logic/auth/flows/RefreshTask.cpp +logic/auth/flows/RefreshTask.cpp +logic/auth/flows/ValidateTask.h +logic/auth/flows/ValidateTask.cpp +logic/auth/flows/InvalidateTask.h +logic/auth/flows/InvalidateTask.cpp # legacy instances logic/LegacyInstance.h @@ -592,5 +595,3 @@ endif (UPDATE_TRANSLATIONS) add_custom_target (translations DEPENDS ${QM_FILES}) install(FILES ${QM_FILES} DESTINATION ${CMAKE_INSTALL_PREFIX}/translations) - - diff --git a/gui/ConsoleWindow.cpp b/gui/ConsoleWindow.cpp index d8a1b69d..d0210df6 100644 --- a/gui/ConsoleWindow.cpp +++ b/gui/ConsoleWindow.cpp @@ -58,13 +58,23 @@ void ConsoleWindow::writeColor(QString text, const char *color) ui->text->appendHtml(QString("%2").arg(color).arg(text)); else ui->text->appendPlainText(text); - // scroll down - QScrollBar *bar = ui->text->verticalScrollBar(); - bar->setValue(bar->maximum()); } void ConsoleWindow::write(QString data, MessageLevel::Enum mode) { + QScrollBar *bar = ui->text->verticalScrollBar(); + int max_bar = bar->maximum(); + int val_bar = bar->value(); + if(m_scroll_active) + { + if(m_last_scroll_value > val_bar) + m_scroll_active = false; + } + else + { + m_scroll_active = val_bar == max_bar; + } + if (data.endsWith('\n')) data = data.left(data.length() - 1); QStringList paragraphs = data.split('\n'); @@ -93,6 +103,11 @@ void ConsoleWindow::write(QString data, MessageLevel::Enum mode) else while (iter.hasNext()) writeColor(iter.next()); + if(m_scroll_active) + { + bar->setValue(bar->maximum()); + } + m_last_scroll_value = bar->value(); } void ConsoleWindow::clear() diff --git a/gui/ConsoleWindow.h b/gui/ConsoleWindow.h index e0a47bc6..2d948484 100644 --- a/gui/ConsoleWindow.h +++ b/gui/ConsoleWindow.h @@ -38,6 +38,16 @@ public: */ void setMayClose(bool mayclose); +private: + /** + * @brief write a colored paragraph + * @param data the string + * @param color the css color name + * this will only insert a single paragraph. + * \n are ignored. a real \n is always appended. + */ + void writeColor(QString data, const char *color = nullptr); + signals: void isClosing(); @@ -51,15 +61,6 @@ slots: */ void write(QString data, MessageLevel::Enum level = MessageLevel::MultiMC); - /** - * @brief write a colored paragraph - * @param data the string - * @param color the css color name - * this will only insert a single paragraph. - * \n are ignored. a real \n is always appended. - */ - void writeColor(QString data, const char *color = nullptr); - /** * @brief clear the text widget */ @@ -82,4 +83,6 @@ private: Ui::ConsoleWindow *ui = nullptr; MinecraftProcess *proc = nullptr; bool m_mayclose = true; + int m_last_scroll_value = 0; + bool m_scroll_active = true; }; diff --git a/gui/MainWindow.cpp b/gui/MainWindow.cpp index a3394a82..854091c6 100644 --- a/gui/MainWindow.cpp +++ b/gui/MainWindow.cpp @@ -69,8 +69,9 @@ #include "logic/lists/IconList.h" #include "logic/lists/JavaVersionList.h" -#include "logic/auth/AuthenticateTask.h" -#include "logic/auth/ValidateTask.h" +#include "logic/auth/flows/AuthenticateTask.h" +#include "logic/auth/flows/RefreshTask.h" +#include "logic/auth/flows/ValidateTask.h" #include "logic/BaseInstance.h" #include "logic/InstanceFactory.h" @@ -276,7 +277,6 @@ MainWindow::~MainWindow() delete assets_downloader; } - void MainWindow::repopulateAccountsMenu() { accountMenu->clear(); @@ -791,16 +791,16 @@ void MainWindow::doLaunchInst(BaseInstance* instance, MojangAccountPtr account) { // We'll need to validate the access token to make sure the account is still logged in. ProgressDialog progDialog(this); - ValidateTask validateTask(account, &progDialog); - progDialog.exec(&validateTask); - - if (validateTask.successful()) + RefreshTask refreshtask(account, &progDialog); + progDialog.exec(&refreshtask); + + if (refreshtask.successful()) { prepareLaunch(m_selectedInstance, account); } else { - YggdrasilTask::Error* error = validateTask.getError(); + YggdrasilTask::Error *error = refreshtask.getError(); if (error != nullptr) { @@ -812,17 +812,20 @@ void MainWindow::doLaunchInst(BaseInstance* instance, MojangAccountPtr account) } else { - CustomMessageBox::selectable(this, tr("Access Token Validation Error"), + CustomMessageBox::selectable( + this, tr("Access Token Validation Error"), tr("There was an error when trying to validate your access token.\n" - "Details: %s").arg(error->getDisplayMessage()), + "Details: %s").arg(error->getDisplayMessage()), QMessageBox::Warning, QMessageBox::Ok)->exec(); } } else { - CustomMessageBox::selectable(this, tr("Access Token Validation Error"), + CustomMessageBox::selectable( + this, tr("Access Token Validation Error"), tr("There was an unknown error when trying to validate your access token." - "The authentication server might be down, or you might not be connected to the Internet."), + "The authentication server might be down, or you might not be connected to " + "the Internet."), QMessageBox::Warning, QMessageBox::Ok)->exec(); } } @@ -879,10 +882,7 @@ void MainWindow::launchInstance(BaseInstance *instance, MojangAccountPtr account console = new ConsoleWindow(proc); connect(console, SIGNAL(isClosing()), this, SLOT(instanceEnded())); - // I think this will work... - QString username = account->username(); - QString session_id = account->accessToken(); - proc->setLogin(username, session_id); + proc->setLogin(account); proc->launch(); } diff --git a/gui/dialogs/AccountListDialog.cpp b/gui/dialogs/AccountListDialog.cpp index dfac4989..f5268b61 100644 --- a/gui/dialogs/AccountListDialog.cpp +++ b/gui/dialogs/AccountListDialog.cpp @@ -20,7 +20,7 @@ #include -#include +#include #include #include @@ -29,9 +29,8 @@ #include -AccountListDialog::AccountListDialog(QWidget *parent) : - QDialog(parent), - ui(new Ui::AccountListDialog) +AccountListDialog::AccountListDialog(QWidget *parent) + : QDialog(parent), ui(new Ui::AccountListDialog) { ui->setupUi(this); @@ -44,8 +43,8 @@ AccountListDialog::AccountListDialog(QWidget *parent) : connect(selectionModel, &QItemSelectionModel::selectionChanged, [this] (const QItemSelection& sel, const QItemSelection& dsel) { updateButtonStates(); }); - connect(m_accounts.get(), SIGNAL(listChanged), SLOT(listChanged)); - connect(m_accounts.get(), SIGNAL(activeAccountChanged), SLOT(listChanged)); + connect(m_accounts.get(), SIGNAL(listChanged()), SLOT(listChanged())); + connect(m_accounts.get(), SIGNAL(activeAccountChanged()), SLOT(listChanged())); updateButtonStates(); } @@ -60,7 +59,6 @@ void AccountListDialog::listChanged() updateButtonStates(); } - void AccountListDialog::on_addAccountBtn_clicked() { addAccount(tr("Please enter your Mojang or Minecraft account username and password to add your account.")); @@ -84,7 +82,7 @@ void AccountListDialog::on_setDefaultBtn_clicked() QModelIndex selected = selection.first(); MojangAccountPtr account = selected.data(MojangAccountList::PointerRole).value(); m_accounts->setActiveAccount(account->username()); - } + } } void AccountListDialog::on_noDefaultBtn_clicked() @@ -104,7 +102,7 @@ void AccountListDialog::updateButtonStates() ui->rmAccountBtn->setEnabled(selection.size() > 0); ui->setDefaultBtn->setEnabled(selection.size() > 0); - + ui->noDefaultBtn->setDown(m_accounts->activeAccount().get() == nullptr); } @@ -146,4 +144,3 @@ void AccountListDialog::addAccount(const QString& errMsg) } } } - diff --git a/gui/dialogs/AccountListDialog.h b/gui/dialogs/AccountListDialog.h index 634d944a..84ff8e0e 100644 --- a/gui/dialogs/AccountListDialog.h +++ b/gui/dialogs/AccountListDialog.h @@ -21,7 +21,8 @@ #include "logic/lists/MojangAccountList.h" -namespace Ui { +namespace Ui +{ class AccountListDialog; } @@ -29,7 +30,7 @@ class AuthenticateTask; class AccountListDialog : public QDialog { -Q_OBJECT + Q_OBJECT public: explicit AccountListDialog(QWidget *parent = 0); ~AccountListDialog(); @@ -62,4 +63,3 @@ slots: private: Ui::AccountListDialog *ui; }; - diff --git a/gui/dialogs/AccountSelectDialog.cpp b/gui/dialogs/AccountSelectDialog.cpp index db1bd192..b8fa9e42 100644 --- a/gui/dialogs/AccountSelectDialog.cpp +++ b/gui/dialogs/AccountSelectDialog.cpp @@ -20,15 +20,14 @@ #include -#include +#include #include #include -AccountSelectDialog::AccountSelectDialog(const QString& message, int flags, QWidget *parent) : - QDialog(parent), - ui(new Ui::AccountSelectDialog) +AccountSelectDialog::AccountSelectDialog(const QString &message, int flags, QWidget *parent) + : QDialog(parent), ui(new Ui::AccountSelectDialog) { ui->setupUi(this); @@ -85,4 +84,3 @@ void AccountSelectDialog::on_buttonBox_rejected() { close(); } - diff --git a/gui/dialogs/AccountSelectDialog.h b/gui/dialogs/AccountSelectDialog.h index 41af4f7c..a539e7e9 100644 --- a/gui/dialogs/AccountSelectDialog.h +++ b/gui/dialogs/AccountSelectDialog.h @@ -21,17 +21,18 @@ #include "logic/lists/MojangAccountList.h" -namespace Ui { +namespace Ui +{ class AccountSelectDialog; } class AccountSelectDialog : public QDialog { -Q_OBJECT + Q_OBJECT public: enum Flags { - NoFlags=0, + NoFlags = 0, /*! * Shows a check box on the dialog that allows the user to specify that the account @@ -75,7 +76,7 @@ public: public slots: void on_buttonBox_accepted(); - + void on_buttonBox_rejected(); protected: @@ -87,4 +88,3 @@ protected: private: Ui::AccountSelectDialog *ui; }; - diff --git a/gui/dialogs/EditAccountDialog.cpp b/gui/dialogs/EditAccountDialog.cpp index d28336f7..dd3f0523 100644 --- a/gui/dialogs/EditAccountDialog.cpp +++ b/gui/dialogs/EditAccountDialog.cpp @@ -16,11 +16,10 @@ #include "EditAccountDialog.h" #include "ui_EditAccountDialog.h" -EditAccountDialog::EditAccountDialog(const QString& text, QWidget *parent, int flags) : - QDialog(parent), - ui(new Ui::EditAccountDialog) +EditAccountDialog::EditAccountDialog(const QString &text, QWidget *parent, int flags) + : QDialog(parent), ui(new Ui::EditAccountDialog) { - ui->setupUi(this); + ui->setupUi(this); ui->label->setText(text); ui->label->setVisible(!text.isEmpty()); @@ -31,7 +30,7 @@ EditAccountDialog::EditAccountDialog(const QString& text, QWidget *parent, int f EditAccountDialog::~EditAccountDialog() { - delete ui; + delete ui; } QString EditAccountDialog::username() const @@ -43,4 +42,3 @@ QString EditAccountDialog::password() const { return ui->passTextBox->text(); } - diff --git a/gui/dialogs/EditAccountDialog.h b/gui/dialogs/EditAccountDialog.h index 847c3be5..be3a88d8 100644 --- a/gui/dialogs/EditAccountDialog.h +++ b/gui/dialogs/EditAccountDialog.h @@ -17,16 +17,18 @@ #include -namespace Ui { +namespace Ui +{ class EditAccountDialog; } class EditAccountDialog : public QDialog { -Q_OBJECT + Q_OBJECT public: - explicit EditAccountDialog(const QString& text="", QWidget *parent = 0, int flags=UsernameField | PasswordField); + explicit EditAccountDialog(const QString &text = "", QWidget *parent = 0, + int flags = UsernameField | PasswordField); ~EditAccountDialog(); /*! @@ -41,7 +43,7 @@ public: enum Flags { - NoFlags=0, + NoFlags = 0, //! Specifies that the dialog should have a username field. UsernameField, @@ -53,4 +55,3 @@ public: private: Ui::EditAccountDialog *ui; }; - diff --git a/logic/MinecraftProcess.cpp b/logic/MinecraftProcess.cpp index e4a26054..aedd4027 100644 --- a/logic/MinecraftProcess.cpp +++ b/logic/MinecraftProcess.cpp @@ -71,6 +71,26 @@ void MinecraftProcess::setWorkdir(QString path) m_prepostlaunchprocess.setWorkingDirectory(mcDir.absolutePath()); } +QString MinecraftProcess::censorPrivateInfo(QString in) +{ + if(!m_account) + return in; + else + { + QString sessionId = m_account->sessionId(); + QString accessToken = m_account->accessToken(); + QString clientToken = m_account->clientToken(); + QString profileId = m_account->currentProfile()->id(); + QString profileName = m_account->currentProfile()->name(); + in.replace(sessionId, ""); + in.replace(accessToken, ""); + in.replace(clientToken, ""); + in.replace(clientToken, ""); + in.replace(clientToken, ""); + return in; + } +} + // console window void MinecraftProcess::on_stdErr() { @@ -83,8 +103,7 @@ void MinecraftProcess::on_stdErr() for (int i = 0; i < lines.size() - 1; i++) { QString &line = lines[i]; - emit log(line /*.replace(username, "").replace(sessionID, "")*/, - getLevel(line, MessageLevel::Error)); + emit log(censorPrivateInfo(line), getLevel(line, MessageLevel::Error)); } if (!complete) m_err_leftover = lines.last(); @@ -101,8 +120,7 @@ void MinecraftProcess::on_stdOut() for (int i = 0; i < lines.size() - 1; i++) { QString &line = lines[i]; - emit log(line.replace(username, "").replace(sessionID, ""), - getLevel(line, MessageLevel::Message)); + emit log(censorPrivateInfo(line), getLevel(line, MessageLevel::Message)); } if (!complete) m_out_leftover = lines.last(); @@ -173,8 +191,8 @@ void MinecraftProcess::launch() emit log(QString("Minecraft folder is: '%1'").arg(workingDirectory())); QString JavaPath = m_instance->settings().get("JavaPath").toString(); emit log(QString("Java path: '%1'").arg(JavaPath)); - emit log(QString("Arguments: '%1'").arg( - m_args.join("' '").replace(username, "").replace(sessionID, ""))); + QString allArgs = m_args.join("' '"); + emit log(QString("Arguments: '%1'").arg(censorPrivateInfo(allArgs))); start(JavaPath, m_args); if (!waitForStarted()) { diff --git a/logic/MinecraftProcess.h b/logic/MinecraftProcess.h index e38d2f83..bd0151cc 100644 --- a/logic/MinecraftProcess.h +++ b/logic/MinecraftProcess.h @@ -69,10 +69,9 @@ public: void killMinecraft(); - inline void setLogin(QString user, QString sid) + inline void setLogin(MojangAccountPtr account) { - username = user; - sessionID = sid; + m_account = account; } signals: @@ -104,11 +103,13 @@ signals: void log(QString text, MessageLevel::Enum level = MessageLevel::MultiMC); protected: - BaseInstance *m_instance; + BaseInstance *m_instance = nullptr; QStringList m_args; QString m_err_leftover; QString m_out_leftover; QProcess m_prepostlaunchprocess; + bool killed = false; + MojangAccountPtr m_account; protected slots: @@ -117,8 +118,7 @@ slots: void on_stdOut(); private: - bool killed; + QString censorPrivateInfo(QString in); MessageLevel::Enum getLevel(const QString &message, MessageLevel::Enum defaultLevel); - QString sessionID; - QString username; + }; diff --git a/logic/auth/MojangAccount.cpp b/logic/auth/MojangAccount.cpp index 4f3839bc..4a61cf19 100644 --- a/logic/auth/MojangAccount.cpp +++ b/logic/auth/MojangAccount.cpp @@ -23,8 +23,7 @@ #include -MojangAccount::MojangAccount(const QString& username, QObject* parent) : - QObject(parent) +MojangAccount::MojangAccount(const QString &username, QObject *parent) : QObject(parent) { // Generate a client token. m_clientToken = QUuid::createUuid().toString(); @@ -34,9 +33,9 @@ MojangAccount::MojangAccount(const QString& username, QObject* parent) : m_currentProfile = -1; } -MojangAccount::MojangAccount(const QString& username, const QString& clientToken, - const QString& accessToken, QObject* parent) : - QObject(parent) +MojangAccount::MojangAccount(const QString &username, const QString &clientToken, + const QString &accessToken, QObject *parent) + : QObject(parent) { m_username = username; m_clientToken = clientToken; @@ -45,7 +44,7 @@ MojangAccount::MojangAccount(const QString& username, const QString& clientToken m_currentProfile = -1; } -MojangAccount::MojangAccount(const MojangAccount& other, QObject* parent) +MojangAccount::MojangAccount(const MojangAccount &other, QObject *parent) { m_username = other.username(); m_clientToken = other.clientToken(); @@ -55,7 +54,6 @@ MojangAccount::MojangAccount(const MojangAccount& other, QObject* parent) m_currentProfile = other.m_currentProfile; } - QString MojangAccount::username() const { return m_username; @@ -66,18 +64,17 @@ QString MojangAccount::clientToken() const return m_clientToken; } -void MojangAccount::setClientToken(const QString& clientToken) +void MojangAccount::setClientToken(const QString &clientToken) { m_clientToken = clientToken; } - QString MojangAccount::accessToken() const { return m_accessToken; } -void MojangAccount::setAccessToken(const QString& accessToken) +void MojangAccount::setAccessToken(const QString &accessToken) { m_accessToken = accessToken; } @@ -92,7 +89,7 @@ const QList MojangAccount::profiles() const return m_profiles; } -const AccountProfile* MojangAccount::currentProfile() const +const AccountProfile *MojangAccount::currentProfile() const { if (m_currentProfile < 0) { @@ -105,9 +102,9 @@ const AccountProfile* MojangAccount::currentProfile() const return &m_profiles.at(m_currentProfile); } -bool MojangAccount::setProfile(const QString& profileId) +bool MojangAccount::setProfile(const QString &profileId) { - const QList& profiles = this->profiles(); + const QList &profiles = this->profiles(); for (int i = 0; i < profiles.length(); i++) { if (profiles.at(i).id() == profileId) @@ -119,14 +116,14 @@ bool MojangAccount::setProfile(const QString& profileId) return false; } -void MojangAccount::loadProfiles(const ProfileList& profiles) +void MojangAccount::loadProfiles(const ProfileList &profiles) { m_profiles.clear(); for (auto profile : profiles) m_profiles.append(profile); } -MojangAccountPtr MojangAccount::loadFromJson(const QJsonObject& object) +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()) @@ -134,7 +131,7 @@ MojangAccountPtr MojangAccount::loadFromJson(const QJsonObject& object) 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(""); @@ -201,7 +198,7 @@ AccountProfile::AccountProfile(const QString& id, const QString& name) m_name = name; } -AccountProfile::AccountProfile(const AccountProfile& other) +AccountProfile::AccountProfile(const AccountProfile &other) { m_id = other.m_id; m_name = other.m_name; @@ -217,4 +214,7 @@ QString AccountProfile::name() const return m_name; } - +void MojangAccount::propagateChange() +{ + emit changed(); +} diff --git a/logic/auth/MojangAccount.h b/logic/auth/MojangAccount.h index 062b8aa2..25a85790 100644 --- a/logic/auth/MojangAccount.h +++ b/logic/auth/MojangAccount.h @@ -19,6 +19,7 @@ #include #include #include +#include #include @@ -27,7 +28,6 @@ class MojangAccount; typedef std::shared_ptr MojangAccountPtr; Q_DECLARE_METATYPE(MojangAccountPtr) - /** * Class that represents a profile within someone's Mojang account. * @@ -38,56 +38,69 @@ Q_DECLARE_METATYPE(MojangAccountPtr) class AccountProfile { public: - AccountProfile(const QString& id, const QString& name); - AccountProfile(const AccountProfile& other); + AccountProfile(const QString &id, const QString &name); + AccountProfile(const AccountProfile &other); QString id() const; QString name() const; + protected: QString m_id; QString m_name; }; - typedef QList ProfileList; +struct User +{ + QString id; + // pair of key:value + // we don't know if the keys:value mapping is 1:1, so a list is used. + QList> properties; +}; /** * Object that stores information about a certain Mojang account. * - * Said information may include things such as that account's username, client token, and access + * Said information may include things such as that account's username, client token, and access * token if the user chose to stay logged in. */ class MojangAccount : public QObject { -Q_OBJECT + Q_OBJECT public: /** * Constructs a new MojangAccount with the given username. * The client token will be generated automatically and the access token will be blank. */ - explicit MojangAccount(const QString& username, QObject* parent = 0); + explicit MojangAccount(const QString &username, QObject *parent = 0); /** * Constructs a new MojangAccount with the given username, client token, and access token. */ - explicit MojangAccount(const QString& username, const QString& clientToken, const QString& accessToken, QObject* parent = 0); + explicit MojangAccount(const QString &username, const QString &clientToken, + const QString &accessToken, QObject *parent = 0); /** * Constructs a new MojangAccount matching the given account. */ - MojangAccount(const MojangAccount& other, QObject* parent); + MojangAccount(const MojangAccount &other, QObject *parent); /** * Loads a MojangAccount from the given JSON object. */ - static MojangAccountPtr loadFromJson(const QJsonObject& json); + static MojangAccountPtr loadFromJson(const QJsonObject &json); /** * Saves a MojangAccount to a JSON object and returns it. */ QJsonObject saveToJson(); + /** + * Update the account on disk and lists (it changed, for whatever reason) + * This is called by various Yggdrasil tasks. + */ + void propagateChange(); /** * This MojangAccount's username. May be an email address if the account is migrated. @@ -103,7 +116,7 @@ public: /** * Sets the MojangAccount's client token to the given value. */ - void setClientToken(const QString& token); + void setClientToken(const QString &token); /** * This MojangAccount's access token. @@ -114,7 +127,7 @@ public: /** * Changes this MojangAccount's access token to the given value. */ - void setAccessToken(const QString& token); + void setAccessToken(const QString &token); /** * Get full session ID @@ -130,25 +143,31 @@ public: * Returns a pointer to the currently selected profile. * If no profile is selected, returns the first profile in the profile list or nullptr if there are none. */ - const AccountProfile* currentProfile() const; + const AccountProfile *currentProfile() const; /** * Sets the currently selected profile to the profile with the given ID string. * If profileId is not in the list of available profiles, the function will simply return false. */ - bool setProfile(const QString& profileId); + bool setProfile(const QString &profileId); /** * Clears the current account profile list and replaces it with the given profile list. */ - void loadProfiles(const ProfileList& profiles); + void loadProfiles(const ProfileList &profiles); +signals: + /** + * This isgnal is emitted whrn the account changes + */ + void changed(); protected: QString m_username; QString m_clientToken; - QString m_accessToken; // Blank if not logged in. - int m_currentProfile; // Index of the selected profile within the list of available profiles. -1 if nothing is selected. + QString m_accessToken; // Blank if not logged in. + int m_currentProfile; // Index of the selected profile within the list of available + // profiles. -1 if nothing is selected. ProfileList m_profiles; // List of available profiles. + User m_user; // the user structure, whatever it is. }; - diff --git a/logic/auth/YggdrasilTask.cpp b/logic/auth/YggdrasilTask.cpp index 31c8fbab..797e84cd 100644 --- a/logic/auth/YggdrasilTask.cpp +++ b/logic/auth/YggdrasilTask.cpp @@ -25,13 +25,12 @@ #include #include -YggdrasilTask::YggdrasilTask(MojangAccountPtr account, QObject* parent) : Task(parent) +YggdrasilTask::YggdrasilTask(MojangAccountPtr account, QObject *parent) : Task(parent) { m_error = nullptr; m_account = account; } - YggdrasilTask::~YggdrasilTask() { if (m_error) @@ -46,17 +45,18 @@ void YggdrasilTask::executeTask() QJsonDocument doc(getRequestContent()); auto worker = MMC->qnam(); - connect(worker.get(), SIGNAL(finished(QNetworkReply*)), this, - SLOT(processReply(QNetworkReply*))); + connect(worker.get(), SIGNAL(finished(QNetworkReply *)), this, + SLOT(processReply(QNetworkReply *))); QUrl reqUrl("https://authserver.mojang.com/" + getEndpoint()); QNetworkRequest netRequest(reqUrl); netRequest.setHeader(QNetworkRequest::ContentTypeHeader, "application/json"); - - m_netReply = worker->post(netRequest, doc.toJson()); + + QByteArray requestData = doc.toJson(); + m_netReply = worker->post(netRequest, requestData); } -void YggdrasilTask::processReply(QNetworkReply* reply) +void YggdrasilTask::processReply(QNetworkReply *reply) { setStatus(getStateMessage(STATE_PROCESSING_RESPONSE)); @@ -76,7 +76,6 @@ void YggdrasilTask::processReply(QNetworkReply* reply) QJsonParseError jsonError; QByteArray replyData = reply->readAll(); QJsonDocument doc = QJsonDocument::fromJson(replyData, &jsonError); - // Check the response code. int responseCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); @@ -85,15 +84,16 @@ void YggdrasilTask::processReply(QNetworkReply* reply) // If the response code was 200, then there shouldn't be an error. Make sure anyways. // Also, sometimes an empty reply indicates success. If there was no data received, // pass an empty json object to the processResponse function. - if (jsonError.error == QJsonParseError::NoError || replyData.size() == 0) + if (jsonError.error == QJsonParseError::NoError || replyData.size() == 0) { if (!processResponse(replyData.size() > 0 ? doc.object() : QJsonObject())) { - YggdrasilTask::Error* err = getError(); + YggdrasilTask::Error *err = getError(); if (err) emitFailed(err->getErrorMessage()); else - emitFailed(tr("An unknown error occurred when processing the response from the authentication server.")); + emitFailed(tr("An unknown error occurred when processing the response " + "from the authentication server.")); } else { @@ -166,4 +166,3 @@ MojangAccountPtr YggdrasilTask::getMojangAccount() const { return this->m_account; } - diff --git a/logic/auth/YggdrasilTask.h b/logic/auth/YggdrasilTask.h index 5a433aeb..62638c9d 100644 --- a/logic/auth/YggdrasilTask.h +++ b/logic/auth/YggdrasilTask.h @@ -24,15 +24,14 @@ class QNetworkReply; - /** * A Yggdrasil task is a task that performs an operation on a given mojang account. */ class YggdrasilTask : public Task { -Q_OBJECT + Q_OBJECT public: - explicit YggdrasilTask(MojangAccountPtr account, QObject* parent=0); + explicit YggdrasilTask(MojangAccountPtr account, QObject *parent = 0); ~YggdrasilTask(); /** @@ -49,7 +48,10 @@ public: QString getCause() const { return m_cause; } /// Gets the string to display in the GUI for describing this error. - QString getDisplayMessage() { return getErrorMessage(); } + QString getDisplayMessage() + { + return getErrorMessage(); + } protected: QString m_shortError; @@ -66,7 +68,7 @@ public: * Returns a pointer to a YggdrasilTask::Error object if an error has occurred. * If no error has occurred, returns a null pointer. */ - virtual Error* getError() const; + virtual Error *getError() const; protected: /** @@ -120,11 +122,11 @@ protected: MojangAccountPtr m_account; - QNetworkReply* m_netReply; + QNetworkReply *m_netReply; - Error* m_error; + Error *m_error; -protected slots: - void processReply(QNetworkReply* reply); +protected +slots: + void processReply(QNetworkReply *reply); }; - diff --git a/logic/auth/AuthenticateTask.cpp b/logic/auth/flows/AuthenticateTask.cpp similarity index 82% rename from logic/auth/AuthenticateTask.cpp rename to logic/auth/flows/AuthenticateTask.cpp index bf7a54f9..ec2004d6 100644 --- a/logic/auth/AuthenticateTask.cpp +++ b/logic/auth/flows/AuthenticateTask.cpp @@ -14,7 +14,7 @@ * limitations under the License. */ -#include +#include #include @@ -26,8 +26,9 @@ #include "logger/QsLog.h" -AuthenticateTask::AuthenticateTask(MojangAccountPtr account, const QString& password, QObject* parent) : - YggdrasilTask(account, parent), m_password(password) +AuthenticateTask::AuthenticateTask(MojangAccountPtr account, const QString &password, + QObject *parent) + : YggdrasilTask(account, parent), m_password(password) { } @@ -37,20 +38,22 @@ QJsonObject AuthenticateTask::getRequestContent() const * { * "agent": { // optional * "name": "Minecraft", // So far this is the only encountered value - * "version": 1 // This number might be increased + * "version": 1 // This number might be increased * // by the vanilla client in the future * }, * "username": "mojang account name", // Can be an email address or player name for // unmigrated accounts * "password": "mojang account password", * "clientToken": "client identifier" // optional + * "requestUser": true/false // request the user structure * } */ QJsonObject req; { QJsonObject agent; - // C++ makes string literals void* for some stupid reason, so we have to tell it QString... Thanks Obama. + // C++ makes string literals void* for some stupid reason, so we have to tell it + // QString... Thanks Obama. agent.insert("name", QString("Minecraft")); agent.insert("version", 1); req.insert("agent", agent); @@ -58,6 +61,7 @@ QJsonObject AuthenticateTask::getRequestContent() const req.insert("username", getMojangAccount()->username()); req.insert("password", m_password); + req.insert("requestUser", true); // If we already have a client token, give it to the server. // Otherwise, let the server give us one. @@ -69,10 +73,12 @@ QJsonObject AuthenticateTask::getRequestContent() const bool AuthenticateTask::processResponse(QJsonObject responseData) { - // Read the response data. We need to get the client token, access token, and the selected profile. + // Read the response data. We need to get the client token, access token, and the selected + // profile. QLOG_DEBUG() << "Processing authentication response."; - - // If we already have a client token, make sure the one the server gave us matches our existing one. + + // If we already have a client token, make sure the one the server gave us matches our + // existing one. QLOG_DEBUG() << "Getting client token."; QString clientToken = responseData.value("clientToken").toString(""); if (clientToken.isEmpty()) @@ -82,16 +88,17 @@ bool AuthenticateTask::processResponse(QJsonObject responseData) QLOG_ERROR() << "Server didn't send a client token."; return false; } - if (!getMojangAccount()->clientToken().isEmpty() && clientToken != getMojangAccount()->clientToken()) + if (!getMojangAccount()->clientToken().isEmpty() && + clientToken != getMojangAccount()->clientToken()) { - // The server changed our client token! Obey its wishes, but complain. That's what I do for my parents, so... - QLOG_WARN() << "Server changed our client token to '" << clientToken + // The server changed our client token! Obey its wishes, but complain. That's what I do + // for my parents, so... + QLOG_WARN() << "Server changed our client token to '" << clientToken << "'. This shouldn't happen, but it isn't really a big deal."; } // Set the client token. getMojangAccount()->setClientToken(clientToken); - // Now, we set the access token. QLOG_DEBUG() << "Getting access token."; QString accessToken = responseData.value("accessToken").toString(""); @@ -104,10 +111,9 @@ bool AuthenticateTask::processResponse(QJsonObject responseData) // Set the access token. getMojangAccount()->setAccessToken(accessToken); - // Now we load the list of available profiles. - // Mojang hasn't yet implemented the profile system, - // but we might as well support what's there so we + // Mojang hasn't yet implemented the profile system, + // but we might as well support what's there so we // don't have trouble implementing it later. QLOG_DEBUG() << "Loading profile list."; QJsonArray availableProfiles = responseData.value("availableProfiles").toArray(); @@ -121,10 +127,11 @@ bool AuthenticateTask::processResponse(QJsonObject responseData) if (id.isEmpty() || name.isEmpty()) { - // This should never happen, but we might as well + // This should never happen, but we might as well // warn about it if it does so we can debug it easily. // You never know when Mojang might do something truly derpy. - QLOG_WARN() << "Found entry in available profiles list with missing ID or name field. Ignoring it."; + QLOG_WARN() << "Found entry in available profiles list with missing ID or name " + "field. Ignoring it."; } // Now, add a new AccountProfile entry to the list. @@ -133,10 +140,8 @@ bool AuthenticateTask::processResponse(QJsonObject responseData) // Put the list of profiles we loaded into the MojangAccount object. getMojangAccount()->loadProfiles(loadedProfiles); - - // Finally, we set the current profile to the correct value. This is pretty simple. - // We do need to make sure that the current profile that the server gave us + // We do need to make sure that the current profile that the server gave us // is actually in the available profiles list. // If it isn't, we'll just fail horribly (*shouldn't* ever happen, but you never know). QLOG_DEBUG() << "Setting current profile."; @@ -151,51 +156,23 @@ bool AuthenticateTask::processResponse(QJsonObject responseData) if (!getMojangAccount()->setProfile(currentProfileId)) { // TODO: Set an error to display to the user. - QLOG_ERROR() << "Server specified a selected profile that wasn't in the available profiles list."; + QLOG_ERROR() << "Server specified a selected profile that wasn't in the available " + "profiles list."; return false; } - /* -public class User -{ - private String id; - private List properties; - - public String getId() - { - return this.id; - } - - public List getProperties() { - return this.properties; - } - public class Property { - private String name; - private String value; - - public Property() { } - public String getKey() { return this.name; } - - public String getValue() - { - return this.value; - } - } -} -*/ - // this is what the vanilla launcher passes to the userProperties launch param // doesn't seem to be used for anything so far? I don't get any of this data on my account // (peterixxx) // is it a good idea to log this? - if(responseData.contains("user")) + if (responseData.contains("user")) { auto obj = responseData.value("user").toObject(); auto userId = obj.value("id").toString(); auto propArray = obj.value("properties").toArray(); QLOG_DEBUG() << "User ID: " << userId; QLOG_DEBUG() << "User Properties: "; - for(auto prop: propArray) + for (auto prop : propArray) { auto propTuple = prop.toObject(); auto name = propTuple.value("name").toString(); @@ -203,8 +180,9 @@ public class User QLOG_DEBUG() << name << " : " << value; } } - - // We've made it through the minefield of possible errors. Return true to indicate that we've succeeded. + + // We've made it through the minefield of possible errors. Return true to indicate that + // we've succeeded. QLOG_DEBUG() << "Finished reading authentication response."; return true; } @@ -226,5 +204,3 @@ QString AuthenticateTask::getStateMessage(const YggdrasilTask::State state) cons return YggdrasilTask::getStateMessage(state); } } - - diff --git a/logic/auth/AuthenticateTask.h b/logic/auth/flows/AuthenticateTask.h similarity index 87% rename from logic/auth/AuthenticateTask.h rename to logic/auth/flows/AuthenticateTask.h index 54a6b79a..3b99caad 100644 --- a/logic/auth/AuthenticateTask.h +++ b/logic/auth/flows/AuthenticateTask.h @@ -22,25 +22,25 @@ #include /** - * The authenticate task takes a MojangAccount with no access token and password and attempts to authenticate with Mojang's servers. + * The authenticate task takes a MojangAccount with no access token and password and attempts to + * authenticate with Mojang's servers. * If successful, it will set the MojangAccount's access token. */ class AuthenticateTask : public YggdrasilTask { -Q_OBJECT + Q_OBJECT public: - AuthenticateTask(MojangAccountPtr account, const QString& password, QObject* parent=0); + AuthenticateTask(MojangAccountPtr account, const QString &password, QObject *parent = 0); protected: virtual QJsonObject getRequestContent() const; - + virtual QString getEndpoint() const; virtual bool processResponse(QJsonObject responseData); - + QString getStateMessage(const YggdrasilTask::State state) const; private: QString m_password; }; - diff --git a/logic/auth/flows/InvalidateTask.cpp b/logic/auth/flows/InvalidateTask.cpp new file mode 100644 index 00000000..e69de29b diff --git a/logic/auth/flows/InvalidateTask.h b/logic/auth/flows/InvalidateTask.h new file mode 100644 index 00000000..e69de29b diff --git a/logic/auth/flows/RefreshTask.cpp b/logic/auth/flows/RefreshTask.cpp new file mode 100644 index 00000000..b56ed9bc --- /dev/null +++ b/logic/auth/flows/RefreshTask.cpp @@ -0,0 +1,156 @@ +/* Copyright 2013 MultiMC Contributors + * + * 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 + +#include + +#include +#include +#include +#include +#include + +#include "logger/QsLog.h" + +RefreshTask::RefreshTask(MojangAccountPtr account, QObject *parent) + : YggdrasilTask(account, parent) +{ +} + +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 + * } + */ + auto account = getMojangAccount(); + QJsonObject req; + req.insert("clientToken", account->clientToken()); + req.insert("accessToken", account->accessToken()); + /* + { + auto currentProfile = account->currentProfile(); + QJsonObject profile; + profile.insert("id", currentProfile->id()); + profile.insert("name", currentProfile->name()); + req.insert("selectedProfile", profile); + } + */ + req.insert("requestUser", true); + + return req; +} + +bool RefreshTask::processResponse(QJsonObject responseData) +{ + auto account = getMojangAccount(); + + // Read the response data. We need to get the client token, access token, and the selected + // profile. + QLOG_DEBUG() << "Processing authentication response."; + + // 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 + // TODO: Set an error properly to display to the user. + QLOG_ERROR() << "Server didn't send a client token."; + return false; + } + if (!account->clientToken().isEmpty() && clientToken != account->clientToken()) + { + // The server changed our client token! Obey its wishes, but complain. That's what I do + // for my parents, so... + QLOG_ERROR() << "Server changed our client token to '" << clientToken + << "'. This shouldn't happen, but it isn't really a big deal."; + return false; + } + + // 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. + // TODO: Set an error properly to display to the user. + QLOG_ERROR() << "Server didn't send an access token."; + return false; + } + + // 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(""); + if (account->currentProfile()->id() != currentProfileId) + { + // TODO: Set an error to display to the user. + QLOG_ERROR() << "Server didn't specify the same selected profile as ours."; + return false; + } + + // this is what the vanilla launcher passes to the userProperties launch param + if (responseData.contains("user")) + { + auto obj = responseData.value("user").toObject(); + auto userId = obj.value("id").toString(); + auto propArray = obj.value("properties").toArray(); + QLOG_DEBUG() << "User ID: " << userId; + QLOG_DEBUG() << "User Properties: "; + for (auto prop : propArray) + { + auto propTuple = prop.toObject(); + auto name = propTuple.value("name").toString(); + auto value = propTuple.value("value").toString(); + QLOG_DEBUG() << name << " : " << value; + } + } + + // 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. + account->setAccessToken(accessToken); + account->propagateChange(); + return true; +} + +QString RefreshTask::getEndpoint() const +{ + return "refresh"; +} + +QString RefreshTask::getStateMessage(const YggdrasilTask::State state) const +{ + switch (state) + { + case STATE_SENDING_REQUEST: + return tr("Refreshing: Sending request."); + case STATE_PROCESSING_RESPONSE: + return tr("Refreshing: Processing response."); + default: + return YggdrasilTask::getStateMessage(state); + } +} diff --git a/logic/auth/flows/RefreshTask.h b/logic/auth/flows/RefreshTask.h new file mode 100644 index 00000000..2596f6c7 --- /dev/null +++ b/logic/auth/flows/RefreshTask.h @@ -0,0 +1,43 @@ +/* Copyright 2013 MultiMC Contributors + * + * 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. + */ + +#pragma once + +#include + +#include +#include +#include + +/** + * The authenticate task takes a MojangAccount with a possibly timed-out access token + * and attempts to authenticate with Mojang's servers. + * If successful, it will set the new access token. The token is considered validated. + */ +class RefreshTask : public YggdrasilTask +{ + Q_OBJECT +public: + RefreshTask(MojangAccountPtr account, QObject *parent = 0); + +protected: + virtual QJsonObject getRequestContent() const; + + virtual QString getEndpoint() const; + + virtual bool processResponse(QJsonObject responseData); + + QString getStateMessage(const YggdrasilTask::State state) const; +}; diff --git a/logic/auth/ValidateTask.cpp b/logic/auth/flows/ValidateTask.cpp similarity index 91% rename from logic/auth/ValidateTask.cpp rename to logic/auth/flows/ValidateTask.cpp index 994d24e4..d9e0e46b 100644 --- a/logic/auth/ValidateTask.cpp +++ b/logic/auth/flows/ValidateTask.cpp @@ -14,7 +14,7 @@ * limitations under the License. */ -#include +#include #include @@ -26,8 +26,8 @@ #include "logger/QsLog.h" -ValidateTask::ValidateTask(MojangAccountPtr account, QObject* parent) : - YggdrasilTask(account, parent) +ValidateTask::ValidateTask(MojangAccountPtr account, QObject *parent) + : YggdrasilTask(account, parent) { } @@ -62,5 +62,3 @@ QString ValidateTask::getStateMessage(const YggdrasilTask::State state) const return YggdrasilTask::getStateMessage(state); } } - - diff --git a/logic/auth/ValidateTask.h b/logic/auth/flows/ValidateTask.h similarity index 93% rename from logic/auth/ValidateTask.h rename to logic/auth/flows/ValidateTask.h index 5bbc69c7..3ff78c6a 100644 --- a/logic/auth/ValidateTask.h +++ b/logic/auth/flows/ValidateTask.h @@ -26,19 +26,18 @@ */ class ValidateTask : public YggdrasilTask { -Q_OBJECT + Q_OBJECT public: - ValidateTask(MojangAccountPtr account, QObject* parent=0); + ValidateTask(MojangAccountPtr account, QObject *parent = 0); protected: virtual QJsonObject getRequestContent() const; - + virtual QString getEndpoint() const; virtual bool processResponse(QJsonObject responseData); - + QString getStateMessage(const YggdrasilTask::State state) const; private: }; - diff --git a/logic/lists/MojangAccountList.cpp b/logic/lists/MojangAccountList.cpp index ec1937ad..466cc934 100644 --- a/logic/lists/MojangAccountList.cpp +++ b/logic/lists/MojangAccountList.cpp @@ -44,7 +44,6 @@ MojangAccountPtr MojangAccountList::findAccount(const QString &username) const return nullptr; } - const MojangAccountPtr MojangAccountList::at(int i) const { return MojangAccountPtr(m_accounts.at(i)); @@ -53,12 +52,13 @@ const MojangAccountPtr MojangAccountList::at(int i) const void MojangAccountList::addAccount(const MojangAccountPtr account) { beginResetModel(); + connect(account.get(), SIGNAL(changed()), SLOT(accountChanged())); m_accounts.append(account); endResetModel(); onListChanged(); } -void MojangAccountList::removeAccount(const QString& username) +void MojangAccountList::removeAccount(const QString &username) { beginResetModel(); for (auto account : m_accounts) @@ -81,7 +81,6 @@ void MojangAccountList::removeAccount(QModelIndex index) onListChanged(); } - MojangAccountPtr MojangAccountList::activeAccount() const { if (m_activeAccount.isEmpty()) @@ -90,7 +89,7 @@ MojangAccountPtr MojangAccountList::activeAccount() const return findAccount(m_activeAccount); } -void MojangAccountList::setActiveAccount(const QString& username) +void MojangAccountList::setActiveAccount(const QString &username) { beginResetModel(); if (username.isEmpty()) @@ -109,6 +108,11 @@ void MojangAccountList::setActiveAccount(const QString& username) onActiveChanged(); } +void MojangAccountList::accountChanged() +{ + // the list changed. there is no doubt. + onListChanged(); +} void MojangAccountList::onListChanged() { @@ -127,13 +131,11 @@ void MojangAccountList::onActiveChanged() emit activeAccountChanged(); } - int MojangAccountList::count() const { return m_accounts.count(); } - QVariant MojangAccountList::data(const QModelIndex &index, int role) const { if (!index.isValid()) @@ -220,10 +222,11 @@ void MojangAccountList::updateListData(QList versions) endResetModel(); } -bool MojangAccountList::loadList(const QString& filePath) +bool MojangAccountList::loadList(const QString &filePath) { QString path = filePath; - if (path.isEmpty()) path = m_listFilePath; + if (path.isEmpty()) + path = m_listFilePath; if (path.isEmpty()) { QLOG_ERROR() << "Can't load Mojang account list. No file path given and no default set."; @@ -231,7 +234,7 @@ bool MojangAccountList::loadList(const QString& filePath) } QFile file(path); - + // Try to open the file and fail if we can't. // TODO: We should probably report this error to the user. if (!file.open(QIODevice::ReadOnly)) @@ -286,6 +289,7 @@ bool MojangAccountList::loadList(const QString& filePath) MojangAccountPtr account = MojangAccount::loadFromJson(accountObj); if (account.get() != nullptr) { + connect(account.get(), SIGNAL(changed()), SLOT(accountChanged())); m_accounts.append(account); } else @@ -297,14 +301,15 @@ bool MojangAccountList::loadList(const QString& filePath) // Load the active account. m_activeAccount = root.value("activeAccount").toString(""); - + return true; } -bool MojangAccountList::saveList(const QString& filePath) +bool MojangAccountList::saveList(const QString &filePath) { QString path(filePath); - if (path.isEmpty()) path = m_listFilePath; + if (path.isEmpty()) + path = m_listFilePath; if (path.isEmpty()) { QLOG_ERROR() << "Can't save Mojang account list. No file path given and no default set."; @@ -337,7 +342,6 @@ bool MojangAccountList::saveList(const QString& filePath) // Create a JSON document object to convert our JSON to bytes. QJsonDocument doc(root); - // Now that we're done building the JSON object, we can write it to the file. QLOG_DEBUG() << "Writing account list to file."; QFile file(path); @@ -362,6 +366,5 @@ bool MojangAccountList::saveList(const QString& filePath) void MojangAccountList::setListFilePath(QString path, bool autosave) { m_listFilePath = path; - autosave = autosave; + m_autosave = autosave; } - diff --git a/logic/lists/MojangAccountList.h b/logic/lists/MojangAccountList.h index 908f5a7a..744f3c51 100644 --- a/logic/lists/MojangAccountList.h +++ b/logic/lists/MojangAccountList.h @@ -22,7 +22,6 @@ #include "logic/auth/MojangAccount.h" - /*! * \brief List of available Mojang accounts. * This should be loaded in the background by MultiMC on startup. @@ -44,10 +43,10 @@ public: enum VListColumns { // TODO: Add icon column. - + // First column - Active? ActiveColumn = 0, - + // Second column - Name NameColumn, }; @@ -74,7 +73,7 @@ public: /*! * Removes the mojang account with the given username from the account list. */ - virtual void removeAccount(const QString& username); + virtual void removeAccount(const QString &username); /*! * Removes the account at the given QModelIndex. @@ -88,7 +87,7 @@ public: * one doesn't exist. */ virtual MojangAccountPtr findAccount(const QString &username) const; - + /*! * Sets the default path to save the list file to. * If autosave is true, this list will automatically save to the given path whenever it changes. @@ -96,21 +95,21 @@ public: * after calling this function to ensure an autosaved change doesn't overwrite the list you intended * to load. */ - virtual void setListFilePath(QString path, bool autosave=false); + virtual void setListFilePath(QString path, bool autosave = false); /*! * \brief Loads the account list from the given file path. * If the given file is an empty string (default), will load from the default account list file. * \return True if successful, otherwise false. */ - virtual bool loadList(const QString& file=""); + virtual bool loadList(const QString &file = ""); /*! * \brief Saves the account list to the given file. * If the given file is an empty string (default), will save from the default account list file. * \return True if successful, otherwise false. */ - virtual bool saveList(const QString& file=""); + virtual bool saveList(const QString &file = ""); /*! * \brief Gets a pointer to the account that the user has selected as their "active" account. @@ -124,12 +123,13 @@ public: * Sets the given account as the current active account. * If the username given is an empty string, sets the active account to nothing. */ - virtual void setActiveAccount(const QString& username); + virtual void setActiveAccount(const QString &username); signals: /*! * Signal emitted to indicate that the account list has changed. - * This will also fire if the value of an element in the list changes (will be implemented later). + * This will also fire if the value of an element in the list changes (will be implemented + * later). */ void listChanged(); @@ -138,6 +138,13 @@ signals: */ void activeAccountChanged(); +public +slots: + /** + * This is called when one of the accounts changes and the list needs to be updated + */ + void accountChanged(); + protected: /*! * Called whenever the list changes. @@ -166,7 +173,7 @@ protected: * If true, the account list will automatically save to the account list path when it changes. * Ignored if m_listFilePath is blank. */ - bool m_autosave; + bool m_autosave = false; protected slots: @@ -184,4 +191,3 @@ slots: */ virtual void updateListData(QList versions); }; - diff --git a/test.cpp b/test.cpp deleted file mode 100644 index 2b015454..00000000 --- a/test.cpp +++ /dev/null @@ -1,60 +0,0 @@ - -#include - -#include "keyring.h" -#include "cmdutils.h" - -using namespace Util::Commandline; - -#include - -int main(int argc, char **argv) -{ - QCoreApplication app(argc, argv); - app.setApplicationName("MMC Keyring test"); - app.setOrganizationName("Orochimarufan"); - - Parser p; - p.addArgument("user", false); - p.addArgument("password", false); - p.addSwitch("set"); - p.addSwitch("get"); - p.addSwitch("list"); - p.addOption("service", "Test"); - p.addShortOpt("service", 's'); - - QHash args; - try { - args = p.parse(app.arguments()); - } catch (ParsingError) { - std::cout << "Syntax error." << std::endl; - return 1; - } - - if (args["set"].toBool()) { - if (args["user"].isNull() || args["password"].isNull()) { - std::cout << "set operation needs bot user and password set" << std::endl; - return 1; - } - - return Keyring::instance()->storePassword(args["service"].toString(), - args["user"].toString(), args["password"].toString()); - } else if (args["get"].toBool()) { - if (args["user"].isNull()) { - std::cout << "get operation needs user set" << std::endl; - return 1; - } - - std::cout << "Password: " << qPrintable(Keyring::instance()->getPassword(args["service"].toString(), - args["user"].toString())) << std::endl; - return 0; - } else if (args["list"].toBool()) { - QStringList accounts = Keyring::instance()->getStoredAccounts(args["service"].toString()); - std::cout << "stored accounts:" << std::endl << '\t' << qPrintable(accounts.join("\n\t")) << std::endl; - return 0; - } else { - std::cout << "No operation given!" << std::endl; - std::cout << qPrintable(p.compileHelp(argv[0])) << std::endl; - return 1; - } -}