1
Fork 0
mirror of https://github.com/redstrate/Astra.git synced 2025-04-21 20:27:45 +00:00

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).
This commit is contained in:
Joshua Goins 2022-09-05 17:59:16 -04:00
parent 21f63b8abd
commit 8fbe1b6514
4 changed files with 45 additions and 78 deletions

View file

@ -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 {

View file

@ -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;
}

View file

@ -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) {

View file

@ -10,7 +10,6 @@
#include <QMessageBox>
#include <QPushButton>
#include <QToolTip>
#include <qt5keychain/keychain.h>
#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);