nextcloud-desktop/src/libsync/cookiejar.cpp

152 lines
3.7 KiB
C++
Raw Normal View History

/*
* 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
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* 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"
#include <QFile>
#include <QDateTime>
#include <QLoggingCategory>
#include <QNetworkCookie>
#include <QDataStream>
2020-01-24 19:57:34 +03:00
#include <QDir>
2014-11-10 00:34:07 +03:00
namespace OCC {
Q_LOGGING_CATEGORY(lcCookieJar, "nextcloud.sync.cookiejar", QtInfoMsg)
namespace {
2017-05-17 11:55:42 +03:00
const unsigned int JAR_VERSION = 23;
}
QDataStream &operator<<(QDataStream &stream, const QList<QNetworkCookie> &list)
{
stream << JAR_VERSION;
stream << quint32(list.size());
for (const auto &cookie : list)
stream << cookie.toRawForm();
return stream;
}
QDataStream &operator>>(QDataStream &stream, QList<QNetworkCookie> &list)
{
list.clear();
quint32 version = 0;
stream >> version;
if (version != JAR_VERSION)
return stream;
quint32 count = 0;
stream >> count;
2017-05-17 11:55:42 +03:00
for (quint32 i = 0; i < count; ++i) {
QByteArray value;
stream >> value;
QList<QNetworkCookie> newCookies = QNetworkCookie::parseCookies(value);
if (newCookies.count() == 0 && value.length() != 0) {
qCWarning(lcCookieJar) << "CookieJar: Unable to parse saved cookie:" << value;
}
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)
{
}
CookieJar::~CookieJar() = default;
2017-05-17 11:55:42 +03:00
bool CookieJar::setCookiesFromUrl(const QList<QNetworkCookie> &cookieList, const QUrl &url)
{
2017-05-17 11:55:42 +03:00
if (QNetworkCookieJar::setCookiesFromUrl(cookieList, url)) {
Q_EMIT newCookiesForUrl(cookieList, url);
return true;
}
2017-05-17 11:55:42 +03:00
return false;
}
2014-05-15 13:29:23 +04:00
QList<QNetworkCookie> CookieJar::cookiesForUrl(const QUrl &url) const
{
QList<QNetworkCookie> cookies = QNetworkCookieJar::cookiesForUrl(url);
qCDebug(lcCookieJar) << url << "requests:" << cookies;
return cookies;
}
void CookieJar::clearSessionCookies()
2014-05-28 13:41:06 +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)
{
2020-01-24 19:57:34 +03:00
const QFileInfo info(fileName);
if (!info.dir().exists())
{
info.dir().mkpath(".");
}
qCDebug(lcCookieJar) << fileName;
2020-01-24 19:57:34 +03:00
QFile file(fileName);
if (!file.open(QIODevice::WriteOnly))
{
return false;
}
QDataStream stream(&file);
stream << removeExpired(allCookies());
file.close();
2020-01-24 19:57:34 +03:00
return true;
}
2020-01-24 19:57:34 +03:00
bool CookieJar::restore(const QString &fileName)
{
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;
}
QDataStream stream(&file);
QList<QNetworkCookie> list;
stream >> list;
setAllCookies(removeExpired(list));
file.close();
2020-01-24 19:57:34 +03:00
return true;
}
QList<QNetworkCookie> CookieJar::removeExpired(const QList<QNetworkCookie> &cookies)
{
QList<QNetworkCookie> updatedList;
2017-05-17 11:55:42 +03:00
foreach (const QNetworkCookie &cookie, cookies) {
if (cookie.expirationDate() > QDateTime::currentDateTimeUtc() && !cookie.isSessionCookie()) {
updatedList << cookie;
}
}
return updatedList;
}
2014-11-10 00:34:07 +03:00
} // namespace OCC