mirror of
https://github.com/Chatterino/chatterino2.git
synced 2024-11-21 22:24:07 +01:00
added Credential class
This commit is contained in:
parent
5974438edf
commit
25f75f7580
3 changed files with 111 additions and 0 deletions
0
resources/licenses/qtkeychain.txt
Normal file
0
resources/licenses/qtkeychain.txt
Normal file
88
src/common/Credentials.cpp
Normal file
88
src/common/Credentials.cpp
Normal 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
|
23
src/common/Credentials.hpp
Normal file
23
src/common/Credentials.hpp
Normal 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
|
Loading…
Reference in a new issue