added Credential class

This commit is contained in:
fourtf 2019-09-09 22:27:46 +02:00
parent 5974438edf
commit 25f75f7580
3 changed files with 111 additions and 0 deletions

View file

View file

@ -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<void(QString)> &&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

View file

@ -0,0 +1,23 @@
#pragma once
#include <QString>
#include <functional>
namespace chatterino {
class Credentials
{
public:
static Credentials &getInstance();
QString get(const QString &provider, const QString &name,
std::function<void(QString)> &&onLoaded);
void set(const QString &provider, const QString &name,
const QString &credential);
void erase(const QString &provider, const QString &name);
private:
Credentials();
};
} // namespace chatterino