2014-05-14 13:11:45 +04:00
|
|
|
/*
|
|
|
|
* Copyright (C) by Daniel Molkentin <danimo@owncloud.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
2016-10-25 12:00:07 +03:00
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
2014-05-14 13:11:45 +04:00
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "cookiejar.h"
|
|
|
|
|
2014-11-10 01:25:57 +03:00
|
|
|
#include "configfile.h"
|
2014-05-14 13:11:45 +04:00
|
|
|
|
|
|
|
#include <QFile>
|
|
|
|
#include <QDateTime>
|
2017-05-09 15:24:11 +03:00
|
|
|
#include <QLoggingCategory>
|
2014-05-14 13:22:56 +04:00
|
|
|
#include <QNetworkCookie>
|
2015-04-19 12:01:47 +03:00
|
|
|
#include <QDataStream>
|
2020-01-24 19:57:34 +03:00
|
|
|
#include <QDir>
|
2014-05-14 13:11:45 +04:00
|
|
|
|
2014-11-10 00:34:07 +03:00
|
|
|
namespace OCC {
|
2014-05-14 13:11:45 +04:00
|
|
|
|
2017-12-28 22:33:10 +03:00
|
|
|
Q_LOGGING_CATEGORY(lcCookieJar, "nextcloud.sync.cookiejar", QtInfoMsg)
|
2017-05-09 15:24:11 +03:00
|
|
|
|
2014-05-14 13:11:45 +04:00
|
|
|
namespace {
|
2017-05-17 11:55:42 +03:00
|
|
|
const unsigned int JAR_VERSION = 23;
|
2014-05-14 13:11:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
QDataStream &operator<<(QDataStream &stream, const QList<QNetworkCookie> &list)
|
|
|
|
{
|
|
|
|
stream << JAR_VERSION;
|
|
|
|
stream << quint32(list.size());
|
2020-08-13 13:23:02 +03:00
|
|
|
for (const auto &cookie : list)
|
|
|
|
stream << cookie.toRawForm();
|
2014-05-14 13:11:45 +04:00
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
QDataStream &operator>>(QDataStream &stream, QList<QNetworkCookie> &list)
|
|
|
|
{
|
|
|
|
list.clear();
|
|
|
|
|
2020-05-29 16:07:05 +03:00
|
|
|
quint32 version = 0;
|
2014-05-14 13:11:45 +04:00
|
|
|
stream >> version;
|
|
|
|
|
|
|
|
if (version != JAR_VERSION)
|
|
|
|
return stream;
|
|
|
|
|
2020-05-29 16:07:05 +03:00
|
|
|
quint32 count = 0;
|
2014-05-14 13:11:45 +04:00
|
|
|
stream >> count;
|
2017-05-17 11:55:42 +03:00
|
|
|
for (quint32 i = 0; i < count; ++i) {
|
2014-05-14 13:11:45 +04:00
|
|
|
QByteArray value;
|
|
|
|
stream >> value;
|
|
|
|
QList<QNetworkCookie> newCookies = QNetworkCookie::parseCookies(value);
|
|
|
|
if (newCookies.count() == 0 && value.length() != 0) {
|
2017-05-09 15:24:11 +03:00
|
|
|
qCWarning(lcCookieJar) << "CookieJar: Unable to parse saved cookie:" << value;
|
2014-05-14 13:11:45 +04:00
|
|
|
}
|
|
|
|
for (int j = 0; j < newCookies.count(); ++j)
|
|
|
|
list.append(newCookies.at(j));
|
|
|
|
if (stream.atEnd())
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
2017-05-17 11:55:42 +03:00
|
|
|
CookieJar::CookieJar(QObject *parent)
|
|
|
|
: QNetworkCookieJar(parent)
|
2014-05-14 13:11:45 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-05-25 22:33:24 +03:00
|
|
|
CookieJar::~CookieJar() = default;
|
2014-05-14 13:11:45 +04:00
|
|
|
|
2017-05-17 11:55:42 +03:00
|
|
|
bool CookieJar::setCookiesFromUrl(const QList<QNetworkCookie> &cookieList, const QUrl &url)
|
2014-05-14 13:11:45 +04:00
|
|
|
{
|
2017-05-17 11:55:42 +03:00
|
|
|
if (QNetworkCookieJar::setCookiesFromUrl(cookieList, url)) {
|
|
|
|
Q_EMIT newCookiesForUrl(cookieList, url);
|
|
|
|
return true;
|
|
|
|
}
|
2014-05-14 13:11:45 +04:00
|
|
|
|
2017-05-17 11:55:42 +03:00
|
|
|
return false;
|
2014-05-14 13:11:45 +04:00
|
|
|
}
|
|
|
|
|
2014-05-15 13:29:23 +04:00
|
|
|
QList<QNetworkCookie> CookieJar::cookiesForUrl(const QUrl &url) const
|
2014-05-14 13:11:45 +04:00
|
|
|
{
|
|
|
|
QList<QNetworkCookie> cookies = QNetworkCookieJar::cookiesForUrl(url);
|
2017-05-09 15:24:11 +03:00
|
|
|
qCDebug(lcCookieJar) << url << "requests:" << cookies;
|
2014-05-14 13:11:45 +04:00
|
|
|
return cookies;
|
|
|
|
}
|
|
|
|
|
2014-05-28 17:24:14 +04:00
|
|
|
void CookieJar::clearSessionCookies()
|
2014-05-28 13:41:06 +04:00
|
|
|
{
|
2014-05-28 17:24:14 +04:00
|
|
|
setAllCookies(removeExpired(allCookies()));
|
2014-05-28 13:41:06 +04:00
|
|
|
}
|
|
|
|
|
2020-01-24 19:57:34 +03:00
|
|
|
bool CookieJar::save(const QString &fileName)
|
2014-05-14 13:11:45 +04:00
|
|
|
{
|
2020-01-24 19:57:34 +03:00
|
|
|
const QFileInfo info(fileName);
|
|
|
|
if (!info.dir().exists())
|
|
|
|
{
|
|
|
|
info.dir().mkpath(".");
|
|
|
|
}
|
|
|
|
|
2017-05-09 15:24:11 +03:00
|
|
|
qCDebug(lcCookieJar) << fileName;
|
2020-01-24 19:57:34 +03:00
|
|
|
QFile file(fileName);
|
|
|
|
if (!file.open(QIODevice::WriteOnly))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2014-05-14 13:11:45 +04:00
|
|
|
QDataStream stream(&file);
|
2015-03-19 13:57:25 +03:00
|
|
|
stream << removeExpired(allCookies());
|
2014-05-14 13:11:45 +04:00
|
|
|
file.close();
|
2020-01-24 19:57:34 +03:00
|
|
|
return true;
|
2014-05-14 13:11:45 +04:00
|
|
|
}
|
|
|
|
|
2020-01-24 19:57:34 +03:00
|
|
|
bool CookieJar::restore(const QString &fileName)
|
2014-05-14 13:11:45 +04:00
|
|
|
{
|
2020-01-24 19:57:34 +03:00
|
|
|
const QFileInfo info(fileName);
|
|
|
|
if (!info.exists())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
QFile file(fileName);
|
|
|
|
if (!file.open(QIODevice::ReadOnly))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2014-05-14 13:11:45 +04:00
|
|
|
QDataStream stream(&file);
|
|
|
|
QList<QNetworkCookie> list;
|
|
|
|
stream >> list;
|
|
|
|
setAllCookies(removeExpired(list));
|
|
|
|
file.close();
|
2020-01-24 19:57:34 +03:00
|
|
|
return true;
|
2014-05-14 13:11:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
QList<QNetworkCookie> CookieJar::removeExpired(const QList<QNetworkCookie> &cookies)
|
|
|
|
{
|
|
|
|
QList<QNetworkCookie> updatedList;
|
2017-05-17 11:55:42 +03:00
|
|
|
foreach (const QNetworkCookie &cookie, cookies) {
|
2017-09-25 12:49:11 +03:00
|
|
|
if (cookie.expirationDate() > QDateTime::currentDateTimeUtc() && !cookie.isSessionCookie()) {
|
2014-05-14 13:11:45 +04:00
|
|
|
updatedList << cookie;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return updatedList;
|
|
|
|
}
|
|
|
|
|
2014-11-10 00:34:07 +03:00
|
|
|
} // namespace OCC
|