-
-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathPasswordStore.cpp
More file actions
159 lines (143 loc) · 5.65 KB
/
PasswordStore.cpp
File metadata and controls
159 lines (143 loc) · 5.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Author: Kang Lin <kl222@126.com>
#include <QCoreApplication>
#include <QEventLoop>
#include <QJsonDocument>
#include <QJsonObject>
#include <QTimer>
#include <QLoggingCategory>
#include <QSettings>
#if HAVE_QTKEYCHAIN
#include "keychain.h"
#endif
#include "ParameterPlugin.h"
#include "PasswordStore.h"
#include "RabbitCommonEncrypt.h"
#include "RabbitCommonDir.h"
static constexpr int KEYCHAIN_TIMEOUT_MS = 2000; // timeout for blocking waits
static Q_LOGGING_CATEGORY(log, "WebBrowser.PasswordStore")
CPasswordStore::CPasswordStore(CParameterWebBrowser *pPara, QObject *parent)
: QObject(parent)
, m_pPara(pPara)
{
qDebug(log) << Q_FUNC_INFO;
}
CPasswordStore::~CPasswordStore()
{
qDebug(log) << Q_FUNC_INFO;
}
static QString ServiceName()
{
// Use application name as the keychain service; fall back to a sensible default.
return "io.github.KangLin/RabbitRemoteControl";
}
#if HAVE_QTKEYCHAIN
QVariantMap CPasswordStore::getCredentials(const QString &host)
{
QVariantMap result;
if (host.isEmpty()) return result;
const QString key = ServiceName() + QStringLiteral("autofill/%1").arg(host);
auto pJob = new QKeychain::ReadPasswordJob(ServiceName());
bool check = connect(pJob, &QKeychain::ReadPasswordJob::finished,
this, [this, pJob, host]() {
if (pJob->error() != QKeychain::NoError) {
qDebug(log) << "ReadPasswordJob error for" << host << ":"
<< pJob->errorString();
return;
}
QString text = pJob->textData();
if (!text.isEmpty()) {
QVariantMap result;
// Expect JSON { "username": "...", "password": "..." }
QJsonDocument doc = QJsonDocument::fromJson(text.toUtf8());
if (doc.isObject()) {
QJsonObject obj = doc.object();
if (obj.contains(QStringLiteral("username")))
result["username"] = obj.value(QStringLiteral("username")).toString();
if (obj.contains(QStringLiteral("password")))
result["password"] = obj.value(QStringLiteral("password")).toString();
} else {
// Fallback: treat stored text as password only
result["password"] = text;
}
qDebug(log) << "Get credentials for" << host;
emit this->sigCredentialsReady(host, result);
}
});
Q_ASSERT(check);
pJob->setKey(key);
pJob->start();
return result;
}
void CPasswordStore::saveCredentials(
const QString &host, const QString &username, const QString &password)
{
if (host.isEmpty() || username.isEmpty()) return;
const QString key = ServiceName() + QStringLiteral("autofill/%1").arg(host);
QJsonObject obj;
obj.insert(QStringLiteral("username"), username);
obj.insert(QStringLiteral("password"), password);
const QString payload =
QString::fromUtf8(QJsonDocument(obj).toJson(QJsonDocument::Compact));
auto pJob = new QKeychain::WritePasswordJob(ServiceName());
bool check = connect(pJob, &QKeychain::WritePasswordJob::finished,
[this, host, pJob]() {
if (pJob->error() != QKeychain::NoError) {
qDebug(log) << "Saved credentials error for" << host << ":"
<< pJob->errorString();
return;
}
emit this->credentialsSaved(host);
qDebug(log) << "Saved credentials for" << host;
});
Q_ASSERT(check);
pJob->setKey(key);
pJob->setTextData(payload);
pJob->start();
}
#else
QVariantMap CPasswordStore::getCredentials(const QString &host)
{
QVariantMap result;
if (host.isEmpty()) return result;
QString szFile;
szFile = RabbitCommon::CDir::Instance()->GetDirUserData()
+ QDir::separator() + "WebCredentialsStore.ini";
QSettings set(szFile, QSettings::IniFormat);
const QString key = QStringLiteral("autofill/%1").arg(host);
QString text;
int nRet = m_pPara->LoadPassword("Password", key, text, set);
if (!nRet && !text.isEmpty()) {
// Expect JSON { "username": "...", "password": "..." }
QJsonDocument doc = QJsonDocument::fromJson(text.toUtf8());
if (doc.isObject()) {
QJsonObject obj = doc.object();
if (obj.contains(QStringLiteral("username")))
result["username"] = obj.value(QStringLiteral("username")).toString();
if (obj.contains(QStringLiteral("password")))
result["password"] = obj.value(QStringLiteral("password")).toString();
} else {
// Fallback: treat stored text as password only
result["password"] = text;
}
emit sigCredentialsReady(host, result);
} else
qCritical(log) << "Get credentials fail:" << host;
return result;
}
void CPasswordStore::saveCredentials(
const QString &host, const QString &username, const QString &password)
{
if (host.isEmpty() || username.isEmpty()) return;
QString szFile;
szFile = RabbitCommon::CDir::Instance()->GetDirUserData()
+ QDir::separator() + "WebCredentialsStore.ini";
QSettings set(szFile, QSettings::IniFormat);
const QString key = QStringLiteral("autofill/%1").arg(host);
QJsonObject obj;
obj.insert(QStringLiteral("username"), username);
obj.insert(QStringLiteral("password"), password);
const QString payload = QString::fromUtf8(QJsonDocument(obj).toJson(QJsonDocument::Compact));
m_pPara->SavePassword(key, payload, set, m_pPara->GetAutoFillUserAndPassword());
emit credentialsSaved(host);
}
#endif