From 8fbe1b6514676b78424aff463f4349ab5580f44f Mon Sep 17 00:00:00 2001 From: Joshua Goins Date: Mon, 5 Sep 2022 17:59:16 -0400 Subject: [PATCH] Introduce new and dedicated keychain functions This replaced the manual use of QtKeychain which could be error-prone, and nasty. This WILL reset saved passwords unfortunately, because the name was set wrong (it was previously LauncherWindow instead of Astra). --- launcher/core/include/launchercore.h | 10 ++++ launcher/core/src/launchercore.cpp | 73 ++++++++++--------------- launcher/desktop/src/launcherwindow.cpp | 34 ++---------- launcher/desktop/src/settingswindow.cpp | 6 +- 4 files changed, 45 insertions(+), 78 deletions(-) diff --git a/launcher/core/include/launchercore.h b/launcher/core/include/launchercore.h index e02c640..73ed117 100755 --- a/launcher/core/include/launchercore.h +++ b/launcher/core/include/launchercore.h @@ -98,6 +98,16 @@ public: GameLicense license = GameLicense::WindowsStandalone; bool isFreeTrial = false; + + /* + * Sets a value in the keychain. This function is asynchronous. + */ + void setKeychainValue(QString key, QString value); + + /* + * Retrieves a value from the keychain. This function is synchronous. + */ + QString getKeychainValue(QString key); }; struct AppSettings { diff --git a/launcher/core/src/launchercore.cpp b/launcher/core/src/launchercore.cpp index edb5d68..bf8e968 100755 --- a/launcher/core/src/launchercore.cpp +++ b/launcher/core/src/launchercore.cpp @@ -649,49 +649,9 @@ void LauncherCore::login(LoginInformation* loginInformation) { } bool LauncherCore::autoLogin(ProfileSettings& profile) { - auto loop = new QEventLoop(); - - QString username, password; - QString otpSecret; - - auto usernameJob = new QKeychain::ReadPasswordJob("LauncherWindow"); - usernameJob->setKey(profile.name + "-username"); - usernameJob->start(); - - QObject::connect( - usernameJob, &QKeychain::ReadPasswordJob::finished, [loop, usernameJob, &username](QKeychain::Job* j) { - username = usernameJob->textData(); - loop->quit(); - }); - - loop->exec(); - - auto passwordJob = new QKeychain::ReadPasswordJob("LauncherWindow"); - passwordJob->setKey(profile.name + "-password"); - passwordJob->start(); - - QObject::connect( - passwordJob, &QKeychain::ReadPasswordJob::finished, [loop, passwordJob, &password](QKeychain::Job* j) { - password = passwordJob->textData(); - loop->quit(); - }); - - loop->exec(); - - // TODO: handle cases where the user doesn't want to store their OTP secret, so we have to manually prompt them - if(profile.useOneTimePassword && profile.rememberOTPSecret) { - auto otpJob = new QKeychain::ReadPasswordJob("LauncherWindow"); - otpJob->setKey(profile.name + "-otpsecret"); - otpJob->start(); - - QObject::connect( - otpJob, &QKeychain::ReadPasswordJob::finished, [loop, otpJob, &otpSecret](QKeychain::Job* j) { - otpSecret = otpJob->textData(); - loop->quit(); - }); - - loop->exec(); - } + QString username = profile.getKeychainValue("username"); + QString password = profile.getKeychainValue("password"); + QString otpSecret = profile.getKeychainValue("otpsecret"); auto info = new LoginInformation(); info->settings = &profile; @@ -713,3 +673,30 @@ bool LauncherCore::autoLogin(ProfileSettings& profile) { return true; } + +void ProfileSettings::setKeychainValue(QString key, QString value) { + auto job = new QKeychain::WritePasswordJob("Astra"); + job->setTextData(value); + job->setKey(name + "-" + key); + job->start(); +} + +QString ProfileSettings::getKeychainValue(QString key) { + auto loop = new QEventLoop(); + + auto job = new QKeychain::ReadPasswordJob("Astra"); + job->setKey(name + "-" + key); + job->start(); + + QString value; + + QObject::connect( + job, &QKeychain::ReadPasswordJob::finished, [loop, job, &value](QKeychain::Job* j) { + value = job->textData(); + loop->quit(); + }); + + loop->exec(); + + return value; +} diff --git a/launcher/desktop/src/launcherwindow.cpp b/launcher/desktop/src/launcherwindow.cpp index 1b699b9..ae1c6ce 100644 --- a/launcher/desktop/src/launcherwindow.cpp +++ b/launcher/desktop/src/launcherwindow.cpp @@ -285,23 +285,13 @@ LauncherWindow::LauncherWindow(LauncherCore& core, QWidget* parent) : QMainWindo info->password = passwordEdit->text(); info->oneTimePassword = otpEdit->text(); -#ifndef QT_DEBUG if (currentProfile().rememberUsername) { - auto job = new QKeychain::WritePasswordJob("LauncherWindow"); - job->setTextData(usernameEdit->text()); - job->setKey(currentProfile().name + "-username"); - job->start(); + profile.setKeychainValue("username", usernameEdit->text()); } -#endif -#ifndef QT_DEBUG if (currentProfile().rememberPassword) { - auto job = new QKeychain::WritePasswordJob("LauncherWindow"); - job->setTextData(passwordEdit->text()); - job->setKey(currentProfile().name + "-password"); - job->start(); + profile.setKeychainValue("password", passwordEdit->text()); } -#endif this->core.login(info); }); @@ -368,30 +358,14 @@ void LauncherWindow::reloadControls() { } rememberUsernameBox->setChecked(currentProfile().rememberUsername); -#ifndef QT_DEBUG if (currentProfile().rememberUsername) { - auto job = new QKeychain::ReadPasswordJob("LauncherWindow"); - job->setKey(currentProfile().name + "-username"); - job->start(); - - connect(job, &QKeychain::ReadPasswordJob::finished, [=](QKeychain::Job* j) { - usernameEdit->setText(job->textData()); - }); + usernameEdit->setText(currentProfile().getKeychainValue("username")); } -#endif rememberPasswordBox->setChecked(currentProfile().rememberPassword); -#ifndef QT_DEBUG if (currentProfile().rememberPassword) { - auto job = new QKeychain::ReadPasswordJob("LauncherWindow"); - job->setKey(currentProfile().name + "-password"); - job->start(); - - connect(job, &QKeychain::ReadPasswordJob::finished, [=](QKeychain::Job* j) { - passwordEdit->setText(job->textData()); - }); + passwordEdit->setText(currentProfile().getKeychainValue("password")); } -#endif bool canLogin = true; if (currentProfile().isSapphire) { diff --git a/launcher/desktop/src/settingswindow.cpp b/launcher/desktop/src/settingswindow.cpp index 466b5f2..e0b82c5 100644 --- a/launcher/desktop/src/settingswindow.cpp +++ b/launcher/desktop/src/settingswindow.cpp @@ -10,7 +10,6 @@ #include #include #include -#include #include "gamescopesettingswindow.h" #include "launchercore.h" @@ -446,10 +445,7 @@ void SettingsWindow::setupLoginTab(QFormLayout& layout) { connect(otpSecretButton, &QPushButton::pressed, [=] { auto otpSecret = QInputDialog::getText(this, "OTP Input", "Enter your OTP Secret:"); - auto job = new QKeychain::WritePasswordJob("LauncherWindow"); - job->setTextData(otpSecret); - job->setKey(this->getCurrentProfile().name + "-otpsecret"); - job->start(); + getCurrentProfile().setKeychainValue("otpsecret", otpSecret); }); otpSecretButton->setToolTip("Enter your OTP secret from Square Enix here. You cannot easily retrieve this if you forget it."); layout.addRow(otpSecretButton);