mirror of
https://github.com/nextcloud/desktop.git
synced 2024-11-29 04:08:54 +03:00
parent
3a3ccb0834
commit
9564e5e92e
5 changed files with 77 additions and 11 deletions
|
@ -258,7 +258,10 @@ void AccountManager::saveAccountHelper(Account *acc, QSettings &settings, bool s
|
||||||
auto *jar = qobject_cast<CookieJar *>(acc->_am->cookieJar());
|
auto *jar = qobject_cast<CookieJar *>(acc->_am->cookieJar());
|
||||||
if (jar) {
|
if (jar) {
|
||||||
qCInfo(lcAccountManager) << "Saving cookies." << acc->cookieJarPath();
|
qCInfo(lcAccountManager) << "Saving cookies." << acc->cookieJarPath();
|
||||||
jar->save(acc->cookieJarPath());
|
if (!jar->save(acc->cookieJarPath()))
|
||||||
|
{
|
||||||
|
qCWarning(lcAccountManager) << "Failed to save cookies to" << acc->cookieJarPath();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,6 +21,7 @@
|
||||||
#include <QLoggingCategory>
|
#include <QLoggingCategory>
|
||||||
#include <QNetworkCookie>
|
#include <QNetworkCookie>
|
||||||
#include <QDataStream>
|
#include <QDataStream>
|
||||||
|
#include <QDir>
|
||||||
|
|
||||||
namespace OCC {
|
namespace OCC {
|
||||||
|
|
||||||
|
@ -95,27 +96,45 @@ void CookieJar::clearSessionCookies()
|
||||||
setAllCookies(removeExpired(allCookies()));
|
setAllCookies(removeExpired(allCookies()));
|
||||||
}
|
}
|
||||||
|
|
||||||
void CookieJar::save(const QString &fileName)
|
bool CookieJar::save(const QString &fileName)
|
||||||
{
|
{
|
||||||
QFile file;
|
const QFileInfo info(fileName);
|
||||||
file.setFileName(fileName);
|
if (!info.dir().exists())
|
||||||
|
{
|
||||||
|
info.dir().mkpath(".");
|
||||||
|
}
|
||||||
|
|
||||||
qCDebug(lcCookieJar) << fileName;
|
qCDebug(lcCookieJar) << fileName;
|
||||||
file.open(QIODevice::WriteOnly);
|
QFile file(fileName);
|
||||||
|
if (!file.open(QIODevice::WriteOnly))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
QDataStream stream(&file);
|
QDataStream stream(&file);
|
||||||
stream << removeExpired(allCookies());
|
stream << removeExpired(allCookies());
|
||||||
file.close();
|
file.close();
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void CookieJar::restore(const QString &fileName)
|
bool CookieJar::restore(const QString &fileName)
|
||||||
{
|
{
|
||||||
QFile file;
|
const QFileInfo info(fileName);
|
||||||
file.setFileName(fileName);
|
if (!info.exists())
|
||||||
file.open(QIODevice::ReadOnly);
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
QFile file(fileName);
|
||||||
|
if (!file.open(QIODevice::ReadOnly))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
QDataStream stream(&file);
|
QDataStream stream(&file);
|
||||||
QList<QNetworkCookie> list;
|
QList<QNetworkCookie> list;
|
||||||
stream >> list;
|
stream >> list;
|
||||||
setAllCookies(removeExpired(list));
|
setAllCookies(removeExpired(list));
|
||||||
file.close();
|
file.close();
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
QList<QNetworkCookie> CookieJar::removeExpired(const QList<QNetworkCookie> &cookies)
|
QList<QNetworkCookie> CookieJar::removeExpired(const QList<QNetworkCookie> &cookies)
|
||||||
|
|
|
@ -39,8 +39,8 @@ public:
|
||||||
using QNetworkCookieJar::setAllCookies;
|
using QNetworkCookieJar::setAllCookies;
|
||||||
using QNetworkCookieJar::allCookies;
|
using QNetworkCookieJar::allCookies;
|
||||||
|
|
||||||
void save(const QString &fileName);
|
bool save(const QString &fileName);
|
||||||
void restore(const QString &fileName);
|
bool restore(const QString &fileName);
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void newCookiesForUrl(const QList<QNetworkCookie> &cookieList, const QUrl &url);
|
void newCookiesForUrl(const QList<QNetworkCookie> &cookieList, const QUrl &url);
|
||||||
|
|
|
@ -37,6 +37,7 @@ nextcloud_add_test(OwnSql "")
|
||||||
nextcloud_add_test(SyncJournalDB "")
|
nextcloud_add_test(SyncJournalDB "")
|
||||||
nextcloud_add_test(SyncFileItem "")
|
nextcloud_add_test(SyncFileItem "")
|
||||||
nextcloud_add_test(ConcatUrl "")
|
nextcloud_add_test(ConcatUrl "")
|
||||||
|
nextcloud_add_test(Cookies "")
|
||||||
nextcloud_add_test(XmlParse "")
|
nextcloud_add_test(XmlParse "")
|
||||||
nextcloud_add_test(ChecksumValidator "")
|
nextcloud_add_test(ChecksumValidator "")
|
||||||
|
|
||||||
|
|
43
test/testcookies.cpp
Normal file
43
test/testcookies.cpp
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
/*
|
||||||
|
This software is in the public domain, furnished "as is", without technical
|
||||||
|
support, and with no warranty, express or implied, as to its usefulness for
|
||||||
|
any purpose.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <QtTest>
|
||||||
|
|
||||||
|
#include "libsync/cookiejar.h"
|
||||||
|
|
||||||
|
using namespace OCC;
|
||||||
|
|
||||||
|
class TestCookies : public QObject
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
|
private slots:
|
||||||
|
void testCookies()
|
||||||
|
{
|
||||||
|
QTemporaryDir tmp;
|
||||||
|
const QString nonexistingPath = tmp.filePath("someNonexistingDir/test.db");
|
||||||
|
QNetworkCookie cookieA = QNetworkCookie("foo", "bar");
|
||||||
|
// tomorrow rounded
|
||||||
|
cookieA.setExpirationDate(QDateTime(QDateTime::currentDateTimeUtc().addDays(1).date()));
|
||||||
|
const QList<QNetworkCookie> cookies = {cookieA, QNetworkCookie("foo2", "bar")};
|
||||||
|
CookieJar jar;
|
||||||
|
jar.setAllCookies(cookies);
|
||||||
|
QCOMPARE(cookies, jar.allCookies());
|
||||||
|
QVERIFY(jar.save(tmp.filePath("test.db")));
|
||||||
|
// ensure we are able to create a cookie jar in a non exisitning folder (mkdir)
|
||||||
|
QVERIFY(jar.save(nonexistingPath));
|
||||||
|
|
||||||
|
CookieJar jar2;
|
||||||
|
QVERIFY(jar2.restore(nonexistingPath));
|
||||||
|
// here we should have only cookieA as the second one was a session cookie
|
||||||
|
QCOMPARE(QList<QNetworkCookie>{cookieA}, jar2.allCookies());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
QTEST_APPLESS_MAIN(TestCookies)
|
||||||
|
#include "testcookies.moc"
|
Loading…
Reference in a new issue