1
Fork 0
mirror of https://github.com/redstrate/Astra.git synced 2025-04-20 11:47:46 +00:00

Improve Account::getKeychainValue, make async

This commit is contained in:
Joshua Goins 2023-10-06 18:08:21 -04:00
parent b366c046e0
commit ed2ea9da33
2 changed files with 20 additions and 20 deletions

View file

@ -6,6 +6,7 @@
#include <QDir> #include <QDir>
#include <QObject> #include <QObject>
#include <QtQml/qqmlregistration.h> #include <QtQml/qqmlregistration.h>
#include <qcorotask.h>
#include "accountconfig.h" #include "accountconfig.h"
@ -101,7 +102,7 @@ private:
/* /*
* Retrieves a value from the keychain. This function is synchronous. * Retrieves a value from the keychain. This function is synchronous.
*/ */
QString getKeychainValue(const QString &key); QCoro::Task<QString> getKeychainValue(const QString &key);
AccountConfig m_config; AccountConfig m_config;
QString m_key; QString m_key;

View file

@ -7,6 +7,7 @@
#include <QNetworkReply> #include <QNetworkReply>
#include <QNetworkRequest> #include <QNetworkRequest>
#include <cotp.h> #include <cotp.h>
#include <qcorocore.h>
#include <qt6keychain/keychain.h> #include <qt6keychain/keychain.h>
#include "launchercore.h" #include "launchercore.h"
@ -173,7 +174,7 @@ void Account::setIsFreeTrial(const bool value)
QString Account::getPassword() QString Account::getPassword()
{ {
return getKeychainValue(QStringLiteral("password")); return QCoro::waitFor(getKeychainValue(QStringLiteral("password")));
} }
void Account::setPassword(const QString &password) void Account::setPassword(const QString &password)
@ -183,13 +184,21 @@ void Account::setPassword(const QString &password)
QString Account::getOTP() QString Account::getOTP()
{ {
auto otpSecret = getKeychainValue(QStringLiteral("otp-secret")); auto otpSecret = QCoro::waitFor(getKeychainValue(QStringLiteral("otp-secret")));
if (otpSecret.isEmpty()) {
return {};
}
char *totp = get_totp(otpSecret.toStdString().c_str(), 6, 30, SHA1, nullptr); cotp_error err;
QString totpStr(totp); char *totp = get_totp(otpSecret.toStdString().c_str(), 6, 30, SHA1, &err);
free(totp);
return totpStr; if (err == NO_ERROR) {
QString totpStr(totp);
free(totp);
return totpStr;
} else {
return {};
}
} }
void Account::setOTPSecret(const QString &secret) void Account::setOTPSecret(const QString &secret)
@ -251,24 +260,14 @@ void Account::setKeychainValue(const QString &key, const QString &value)
job->start(); job->start();
} }
QString Account::getKeychainValue(const QString &key) QCoro::Task<QString> Account::getKeychainValue(const QString &key)
{ {
auto loop = new QEventLoop(this);
auto job = new QKeychain::ReadPasswordJob(QStringLiteral("Astra"), this); auto job = new QKeychain::ReadPasswordJob(QStringLiteral("Astra"), this);
job->setKey(m_key + QStringLiteral("-") + key); job->setKey(m_key + QStringLiteral("-") + key);
job->setInsecureFallback(m_launcher.isSteamDeck()); job->setInsecureFallback(m_launcher.isSteamDeck());
job->start(); job->start();
QString value; co_await qCoro(job, &QKeychain::ReadPasswordJob::finished);
QObject::connect(job, &QKeychain::ReadPasswordJob::finished, [loop, job, &value](QKeychain::Job *j) { co_return job->textData();
Q_UNUSED(j)
value = job->textData();
loop->quit();
});
loop->exec();
return value;
} }