persistent encrypted cookies

This commit is contained in:
Martin Rotter 2021-03-23 12:22:41 +01:00
parent bfe019f22c
commit 4caa7313e4
5 changed files with 105 additions and 8 deletions

View File

@ -15,6 +15,9 @@
DKEY WebEngineAttributes::ID = "web_engine_attributes";
#endif
// Cookies.
DKEY Cookies::ID = "cookies";
// AdBlock.
DKEY AdBlock::ID = "adblock";

View File

@ -32,6 +32,10 @@ namespace WebEngineAttributes {
}
#endif
namespace Cookies {
KEY ID;
}
namespace AdBlock {
KEY ID;

View File

@ -2,14 +2,22 @@
#include "network-web/cookiejar.h"
#include "3rd-party/boolinq/boolinq.h"
#include "definitions/definitions.h"
#include "miscellaneous/application.h"
#include "miscellaneous/iofactory.h"
#include "miscellaneous/settings.h"
#include <QDateTime>
#include <QDir>
#include <QNetworkCookie>
#include <QSettings>
CookieJar::CookieJar(QObject* parent) : QNetworkCookieJar(parent) {}
CookieJar::CookieJar(QObject* parent) : QNetworkCookieJar(parent) {
loadCookies();
}
QList<QNetworkCookie> CookieJar::extractCookiesFromUrl(const QString& url) const {
QList<QNetworkCookie> CookieJar::extractCookiesFromUrl(const QString& url) {
if (!url.contains(QSL(COOKIE_URL_IDENTIFIER))) {
return {};
}
@ -37,11 +45,85 @@ QList<QNetworkCookie> CookieJar::extractCookiesFromUrl(const QString& url) const
return cookies;
}
bool CookieJar::insertCookies(const QList<QNetworkCookie>& cookies) {
bool result = true;
void CookieJar::loadCookies() {
Settings* sett = qApp->settings();
sett->beginGroup(GROUP(Cookies));
auto keys = sett->allKeys();
sett->endGroup();
for (const QString& cookie_key : qAsConst(keys)) {
QByteArray encoded = sett->password(GROUP(Cookies), cookie_key, {}).toByteArray();
if (!encoded.isEmpty()) {
auto cookie = QNetworkCookie::parseCookies(encoded);
if (!cookie.isEmpty()) {
if (!QNetworkCookieJar::insertCookie(cookie.at(0))) {
qCriticalNN << LOGSEC_NETWORK
<< "Failed to load cookie"
<< QUOTE_W_SPACE(cookie_key)
<< "from settings.";
}
}
}
}
}
void CookieJar::saveCookies() {
auto cookies = allCookies();
Settings* sett = qApp->settings();
int i = 1;
sett->beginGroup(GROUP(Cookies));
qobject_cast<QSettings*>(sett)->remove(QString());
sett->endGroup();
for (const QNetworkCookie& cookie : cookies) {
result &= insertCookie(cookie);
if (cookie.isSessionCookie()) {
continue;
}
sett->setPassword(GROUP(Cookies),
QSL("%1-%2").arg(QString::number(i++), cookie.name()),
cookie.toRawForm(QNetworkCookie::RawForm::Full));
}
}
QList<QNetworkCookie> CookieJar::cookiesForUrl(const QUrl& url) const {
return QNetworkCookieJar::cookiesForUrl(url);
}
bool CookieJar::setCookiesFromUrl(const QList<QNetworkCookie>& cookie_list, const QUrl& url) {
return QNetworkCookieJar::setCookiesFromUrl(cookie_list, url);
}
bool CookieJar::insertCookie(const QNetworkCookie& cookie) {
auto result = QNetworkCookieJar::insertCookie(cookie);
if (result) {
saveCookies();
}
return result;
}
bool CookieJar::updateCookie(const QNetworkCookie& cookie) {
auto result = QNetworkCookieJar::updateCookie(cookie);
if (result) {
saveCookies();
}
return result;
}
bool CookieJar::deleteCookie(const QNetworkCookie& cookie) {
auto result = QNetworkCookieJar::deleteCookie(cookie);
if (result) {
saveCookies();
}
return result;

View File

@ -9,8 +9,16 @@ class CookieJar : public QNetworkCookieJar {
public:
explicit CookieJar(QObject* parent = nullptr);
QList<QNetworkCookie> extractCookiesFromUrl(const QString& url) const;
bool insertCookies(const QList<QNetworkCookie>& cookies);
virtual QList<QNetworkCookie> cookiesForUrl(const QUrl& url) const;
virtual bool setCookiesFromUrl(const QList<QNetworkCookie>& cookie_list, const QUrl& url);
virtual bool insertCookie(const QNetworkCookie& cookie);
virtual bool updateCookie(const QNetworkCookie& cookie);
virtual bool deleteCookie(const QNetworkCookie& cookie);
static QList<QNetworkCookie> extractCookiesFromUrl(const QString& url);
private:
void loadCookies();
void saveCookies();
};
#endif // COOKIEJAR_H

View File

@ -61,7 +61,7 @@ void Downloader::manipulateData(const QString& url,
const QString& username,
const QString& password) {
auto cookies = qApp->web()->cookieJar()->extractCookiesFromUrl(url);
auto cookies = CookieJar::extractCookiesFromUrl(url);
if (!cookies.isEmpty()) {
qApp->web()->cookieJar()->setCookiesFromUrl(cookies, url);