diff --git a/resources/licenses/qtkeychain.txt b/resources/licenses/qtkeychain.txt new file mode 100644 index 000000000..e69de29bb diff --git a/src/common/Credentials.cpp b/src/common/Credentials.cpp new file mode 100644 index 000000000..ad0e9c423 --- /dev/null +++ b/src/common/Credentials.cpp @@ -0,0 +1,88 @@ +#include "Credentials.hpp" + +#include "keychain.h" +#include "singletons/Paths.hpp" + +#define FORMAT_NAME \ + ([&] { \ + assert(!provider.contains(":")); \ + return QString("chatterino:%1:%2").arg(provider).arg(name_); \ + })() + +namespace chatterino { + +Credentials &Credentials::getInstance() +{ + static Credentials creds; + return creds; +} + +Credentials::Credentials() +{ +} + +QString Credentials::get(const QString &provider, const QString &name_, + std::function &&onLoaded) +{ + auto name = FORMAT_NAME; + + if (getPaths()->isPortable()) + { + assert(false); + + return {}; + } + else + { + auto job = new QKeychain::ReadPasswordJob("chatterino"); + job->setAutoDelete(true); + job->setKey(name); + QObject::connect(job, &QKeychain::Job::finished, qApp, + [job, onLoaded = std::move(onLoaded)](auto) mutable { + onLoaded(job->textData()); + }); + job->start(); + + return job->textData(); + } +} + +void Credentials::set(const QString &provider, const QString &name_, + const QString &credential) +{ + auto name = FORMAT_NAME; + + if (getPaths()->isPortable()) + { + assert(false); + } + else + { + auto job = new QKeychain::WritePasswordJob("chatterino"); + job->setAutoDelete(true); + job->setKey(name); + job->setTextData(credential); + QObject::connect(job, &QKeychain::Job::finished, qApp, [](auto) {}); + job->start(); + } +} + +void Credentials::erase(const QString &provider, const QString &name_) +{ + auto name = FORMAT_NAME; + + if (getPaths()->isPortable()) + { + assert(false); + } + else + { + auto job = new QKeychain::DeletePasswordJob("chatterino"); + job->setAutoDelete(true); + job->setKey(name); + QObject::connect(job, &QKeychain::Job::finished, qApp, [](auto) {}); + job->start(); + } +} + +} // namespace chatterino diff --git a/src/common/Credentials.hpp b/src/common/Credentials.hpp new file mode 100644 index 000000000..95ae27afb --- /dev/null +++ b/src/common/Credentials.hpp @@ -0,0 +1,23 @@ +#pragma once + +#include +#include + +namespace chatterino { + +class Credentials +{ +public: + static Credentials &getInstance(); + + QString get(const QString &provider, const QString &name, + std::function &&onLoaded); + void set(const QString &provider, const QString &name, + const QString &credential); + void erase(const QString &provider, const QString &name); + +private: + Credentials(); +}; + +} // namespace chatterino