2023-08-05 22:14:05 -04:00
|
|
|
// SPDX-FileCopyrightText: 2023 Joshua Goins <josh@redstrate.com>
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
2023-07-30 08:49:34 -04:00
|
|
|
#include "account.h"
|
|
|
|
|
|
|
|
#include <QNetworkReply>
|
|
|
|
#include <QNetworkRequest>
|
2023-09-23 13:16:28 -04:00
|
|
|
#include <cotp.h>
|
2023-10-06 18:08:21 -04:00
|
|
|
#include <qcorocore.h>
|
2023-09-18 18:03:19 -04:00
|
|
|
#include <qt6keychain/keychain.h>
|
2023-07-30 08:49:34 -04:00
|
|
|
|
2023-10-11 13:03:23 -04:00
|
|
|
#include "astra_log.h"
|
2023-07-30 08:49:34 -04:00
|
|
|
#include "launchercore.h"
|
2023-10-08 18:02:02 -04:00
|
|
|
#include "utility.h"
|
2023-07-30 08:49:34 -04:00
|
|
|
|
2023-12-17 12:01:28 -05:00
|
|
|
using namespace Qt::StringLiterals;
|
|
|
|
|
2023-07-30 08:49:34 -04:00
|
|
|
Account::Account(LauncherCore &launcher, const QString &key, QObject *parent)
|
|
|
|
: QObject(parent)
|
|
|
|
, m_config(key)
|
|
|
|
, m_key(key)
|
|
|
|
, m_launcher(launcher)
|
|
|
|
{
|
2023-12-20 19:47:57 -05:00
|
|
|
fetchPassword();
|
2023-07-30 08:49:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
QString Account::uuid() const
|
|
|
|
{
|
|
|
|
return m_key;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Account::name() const
|
|
|
|
{
|
|
|
|
return m_config.name();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Account::setName(const QString &name)
|
|
|
|
{
|
|
|
|
if (m_config.name() != name) {
|
|
|
|
m_config.setName(name);
|
|
|
|
m_config.save();
|
|
|
|
Q_EMIT nameChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-18 22:36:33 -04:00
|
|
|
int Account::language() const
|
|
|
|
{
|
|
|
|
return m_config.language();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Account::setLanguage(const int value)
|
|
|
|
{
|
|
|
|
if (m_config.language() != value) {
|
|
|
|
m_config.setLanguage(value);
|
|
|
|
m_config.save();
|
|
|
|
Q_EMIT languageChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-30 08:49:34 -04:00
|
|
|
QString Account::lodestoneId() const
|
|
|
|
{
|
|
|
|
return m_config.lodestoneId();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Account::setLodestoneId(const QString &id)
|
|
|
|
{
|
|
|
|
if (m_config.lodestoneId() != id) {
|
|
|
|
m_config.setLodestoneId(id);
|
|
|
|
m_config.save();
|
|
|
|
Q_EMIT lodestoneIdChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Account::avatarUrl() const
|
|
|
|
{
|
2023-12-17 11:05:57 -05:00
|
|
|
return m_avatarUrl;
|
2023-07-30 08:49:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Account::isSapphire() const
|
|
|
|
{
|
|
|
|
return m_config.isSapphire();
|
|
|
|
}
|
|
|
|
|
2024-07-04 20:53:06 -04:00
|
|
|
void Account::setIsSapphire(const bool value)
|
2023-07-30 08:49:34 -04:00
|
|
|
{
|
|
|
|
if (m_config.isSapphire() != value) {
|
|
|
|
m_config.setIsSapphire(value);
|
|
|
|
m_config.save();
|
|
|
|
Q_EMIT isSapphireChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Account::lobbyUrl() const
|
|
|
|
{
|
|
|
|
return m_config.lobbyUrl();
|
|
|
|
}
|
|
|
|
|
2024-07-04 20:53:06 -04:00
|
|
|
void Account::setLobbyUrl(const QString &url)
|
2023-07-30 08:49:34 -04:00
|
|
|
{
|
2024-07-04 20:53:06 -04:00
|
|
|
if (m_config.lobbyUrl() != url) {
|
|
|
|
m_config.setLobbyUrl(url);
|
2023-07-30 08:49:34 -04:00
|
|
|
m_config.save();
|
|
|
|
Q_EMIT lobbyUrlChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Account::rememberPassword() const
|
|
|
|
{
|
|
|
|
return m_config.rememberPassword();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Account::setRememberPassword(const bool value)
|
|
|
|
{
|
|
|
|
if (m_config.rememberPassword() != value) {
|
|
|
|
m_config.setRememberPassword(value);
|
|
|
|
m_config.save();
|
|
|
|
Q_EMIT rememberPasswordChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Account::rememberOTP() const
|
|
|
|
{
|
|
|
|
return m_config.rememberOTP();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Account::setRememberOTP(const bool value)
|
|
|
|
{
|
|
|
|
if (m_config.rememberOTP() != value) {
|
|
|
|
m_config.setRememberOTP(value);
|
|
|
|
m_config.save();
|
|
|
|
Q_EMIT rememberOTPChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Account::useOTP() const
|
|
|
|
{
|
|
|
|
return m_config.useOTP();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Account::setUseOTP(const bool value)
|
|
|
|
{
|
|
|
|
if (m_config.useOTP() != value) {
|
|
|
|
m_config.setUseOTP(value);
|
|
|
|
m_config.save();
|
|
|
|
Q_EMIT useOTPChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Account::GameLicense Account::license() const
|
|
|
|
{
|
|
|
|
return static_cast<GameLicense>(m_config.license());
|
|
|
|
}
|
|
|
|
|
|
|
|
void Account::setLicense(const GameLicense license)
|
|
|
|
{
|
|
|
|
if (static_cast<GameLicense>(m_config.license()) != license) {
|
|
|
|
m_config.setLicense(static_cast<int>(license));
|
|
|
|
m_config.save();
|
|
|
|
Q_EMIT licenseChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Account::isFreeTrial() const
|
|
|
|
{
|
|
|
|
return m_config.isFreeTrial();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Account::setIsFreeTrial(const bool value)
|
|
|
|
{
|
|
|
|
if (m_config.isFreeTrial() != value) {
|
|
|
|
m_config.setIsFreeTrial(value);
|
|
|
|
m_config.save();
|
|
|
|
Q_EMIT isFreeTrialChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-30 10:11:14 -04:00
|
|
|
QString Account::getPassword()
|
2023-07-30 08:49:34 -04:00
|
|
|
{
|
2023-10-06 18:08:21 -04:00
|
|
|
return QCoro::waitFor(getKeychainValue(QStringLiteral("password")));
|
2023-07-30 08:49:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Account::setPassword(const QString &password)
|
|
|
|
{
|
2023-09-16 21:22:20 -04:00
|
|
|
setKeychainValue(QStringLiteral("password"), password);
|
2023-12-20 19:47:57 -05:00
|
|
|
|
|
|
|
if (m_needsPassword) {
|
|
|
|
m_needsPassword = false;
|
|
|
|
Q_EMIT needsPasswordChanged();
|
|
|
|
}
|
2023-07-30 08:49:34 -04:00
|
|
|
}
|
|
|
|
|
2024-08-22 18:46:45 -04:00
|
|
|
void Account::setAvatarUrl(const QString &url)
|
|
|
|
{
|
|
|
|
if (m_avatarUrl != url) {
|
|
|
|
m_avatarUrl = url;
|
|
|
|
Q_EMIT avatarUrlChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-30 10:11:14 -04:00
|
|
|
QString Account::getOTP()
|
2023-07-30 08:49:34 -04:00
|
|
|
{
|
2024-07-04 20:53:06 -04:00
|
|
|
const auto otpSecret = QCoro::waitFor(getKeychainValue(QStringLiteral("otp-secret")));
|
2023-10-06 18:08:21 -04:00
|
|
|
if (otpSecret.isEmpty()) {
|
|
|
|
return {};
|
|
|
|
}
|
2023-07-30 08:49:34 -04:00
|
|
|
|
2023-10-06 18:08:21 -04:00
|
|
|
cotp_error err;
|
|
|
|
char *totp = get_totp(otpSecret.toStdString().c_str(), 6, 30, SHA1, &err);
|
2023-07-30 08:49:34 -04:00
|
|
|
|
2023-10-06 18:08:21 -04:00
|
|
|
if (err == NO_ERROR) {
|
2023-12-17 10:09:01 -05:00
|
|
|
QString totpStr = QString::fromLatin1(totp);
|
2023-10-06 18:08:21 -04:00
|
|
|
free(totp);
|
|
|
|
return totpStr;
|
|
|
|
} else {
|
|
|
|
return {};
|
|
|
|
}
|
2023-07-30 08:49:34 -04:00
|
|
|
}
|
|
|
|
|
2023-09-20 15:33:26 -04:00
|
|
|
void Account::setOTPSecret(const QString &secret)
|
|
|
|
{
|
|
|
|
setKeychainValue(QStringLiteral("otp-secret"), secret);
|
|
|
|
}
|
|
|
|
|
2023-08-18 14:52:06 -04:00
|
|
|
QDir Account::getConfigDir() const
|
|
|
|
{
|
|
|
|
const QDir dataDir = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
|
2023-09-16 21:22:20 -04:00
|
|
|
const QDir userDir = dataDir.absoluteFilePath(QStringLiteral("user"));
|
2023-08-18 14:52:06 -04:00
|
|
|
return userDir.absoluteFilePath(m_key);
|
|
|
|
}
|
|
|
|
|
2024-04-19 20:50:14 -04:00
|
|
|
QString Account::getConfigPath() const
|
|
|
|
{
|
|
|
|
return getConfigDir().absolutePath();
|
|
|
|
}
|
|
|
|
|
2023-12-20 21:52:43 -05:00
|
|
|
void Account::setKeychainValue(const QString &key, const QString &value)
|
2023-07-30 08:49:34 -04:00
|
|
|
{
|
2024-08-22 18:48:29 -04:00
|
|
|
if (Utility::isSteamDeck()) {
|
2023-12-20 21:57:51 -05:00
|
|
|
auto stateConfig = KSharedConfig::openStateConfig();
|
|
|
|
|
|
|
|
stateConfig->group(QStringLiteral("Passwords")).writeEntry(m_key + QStringLiteral("-") + key, value);
|
|
|
|
stateConfig->sync();
|
|
|
|
} else {
|
|
|
|
auto job = new QKeychain::WritePasswordJob(QStringLiteral("Astra"), this);
|
|
|
|
job->setTextData(value);
|
2023-10-08 18:20:13 -04:00
|
|
|
#ifdef FLATPAK
|
2023-12-20 21:57:51 -05:00
|
|
|
job->setKey(QStringLiteral("flatpak-") + m_key + QStringLiteral("-") + key);
|
2023-10-08 18:20:13 -04:00
|
|
|
#else
|
2023-12-20 21:57:51 -05:00
|
|
|
job->setKey(m_key + QStringLiteral("-") + key);
|
2023-10-08 18:20:13 -04:00
|
|
|
#endif
|
2023-12-20 21:57:51 -05:00
|
|
|
job->start();
|
2023-12-20 21:23:38 -05:00
|
|
|
|
2023-12-20 21:57:51 -05:00
|
|
|
connect(job, &QKeychain::WritePasswordJob::finished, this, [job] {
|
|
|
|
if (job->error() != QKeychain::NoError) {
|
|
|
|
qWarning(ASTRA_LOG) << "Error when writing" << job->errorString();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2023-07-30 08:49:34 -04:00
|
|
|
}
|
|
|
|
|
2023-10-06 18:08:21 -04:00
|
|
|
QCoro::Task<QString> Account::getKeychainValue(const QString &key)
|
2023-07-30 08:49:34 -04:00
|
|
|
{
|
2024-08-22 18:48:29 -04:00
|
|
|
if (Utility::isSteamDeck()) {
|
2023-12-20 21:57:51 -05:00
|
|
|
co_return KSharedConfig::openStateConfig()->group(QStringLiteral("Passwords")).readEntry(m_key + QStringLiteral("-") + key);
|
|
|
|
} else {
|
|
|
|
auto job = new QKeychain::ReadPasswordJob(QStringLiteral("Astra"), this);
|
2023-10-08 18:20:13 -04:00
|
|
|
#ifdef FLATPAK
|
2023-12-20 21:57:51 -05:00
|
|
|
job->setKey(QStringLiteral("flatpak-") + m_key + QStringLiteral("-") + key);
|
2023-10-08 18:20:13 -04:00
|
|
|
#else
|
2023-12-20 21:57:51 -05:00
|
|
|
job->setKey(m_key + QStringLiteral("-") + key);
|
2023-10-08 18:20:13 -04:00
|
|
|
#endif
|
2023-12-20 21:57:51 -05:00
|
|
|
job->start();
|
2023-07-30 08:49:34 -04:00
|
|
|
|
2023-12-20 21:57:51 -05:00
|
|
|
co_await qCoro(job, &QKeychain::ReadPasswordJob::finished);
|
2023-07-30 08:49:34 -04:00
|
|
|
|
2023-12-20 21:57:51 -05:00
|
|
|
if (job->error() != QKeychain::NoError) {
|
2024-05-26 08:43:33 -04:00
|
|
|
qWarning(ASTRA_LOG) << "Error when reading" << key << job->errorString() << "for account" << uuid();
|
2023-12-20 21:57:51 -05:00
|
|
|
}
|
2023-12-20 21:23:38 -05:00
|
|
|
|
2023-12-20 21:57:51 -05:00
|
|
|
co_return job->textData();
|
|
|
|
}
|
2023-07-30 08:49:34 -04:00
|
|
|
}
|
2023-10-11 13:03:23 -04:00
|
|
|
|
2023-12-20 19:47:57 -05:00
|
|
|
bool Account::needsPassword() const
|
|
|
|
{
|
|
|
|
return m_needsPassword;
|
|
|
|
}
|
|
|
|
|
|
|
|
QCoro::Task<> Account::fetchPassword()
|
|
|
|
{
|
|
|
|
const QString password = co_await getKeychainValue(QStringLiteral("password"));
|
|
|
|
m_needsPassword = password.isEmpty();
|
|
|
|
Q_EMIT needsPasswordChanged();
|
|
|
|
|
|
|
|
co_return;
|
|
|
|
}
|
|
|
|
|
2023-12-17 11:12:13 -05:00
|
|
|
#include "moc_account.cpp"
|