refs #502, cookie manager

* use authenticated user data objects in databaseauthentication
* shared threadsafe cookie manager across the QNetworkAccessManagers
* use cookie manager in existing readers
This commit is contained in:
Klaus Basan
2015-11-04 03:06:55 +01:00
committed by Mathew Sutcliffe
parent 98b86b6f27
commit dda64d0879
7 changed files with 212 additions and 22 deletions

View File

@@ -0,0 +1,91 @@
/* Copyright (C) 2015
* swift project Community / Contributors
*
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
* including this file, may be copied, modified, propagated, or distributed except according to the terms
* contained in the LICENSE file.
*/
#include "blackcore/cookiemanager.h"
#include "blackmisc/threadutilities.h"
#include <QCoreApplication>
#include <QNetworkCookieJar>
#include <QReadLocker>
#include <QWriteLocker>
using namespace BlackMisc;
namespace BlackCore
{
CCookieManager *CCookieManager::instance()
{
static CCookieManager *manager = nullptr;
if (!manager)
{
Q_ASSERT_X(CThreadUtils::isCurrentThreadApplicationThread(), Q_FUNC_INFO, "Supposed to run in application thread");
manager = new CCookieManager(QCoreApplication::instance());
}
return manager;
}
void CCookieManager::resetParent()
{
instance()->setParent(nullptr);
}
void CCookieManager::setToAccessManager(QNetworkAccessManager *manager)
{
if (!manager) { return; }
manager->setCookieJar(instance());
resetParent();
}
CCookieManager::CCookieManager(QObject *parent) : QNetworkCookieJar(parent)
{
// code
}
bool CCookieManager::setCookiesFromUrl(const QList<QNetworkCookie> &cookies, const QUrl &url)
{
QWriteLocker l(&m_lock);
return QNetworkCookieJar::setCookiesFromUrl(cookies, url);
}
QList<QNetworkCookie> CCookieManager::cookiesForUrl(const QUrl &url) const
{
QReadLocker l(&m_lock);
const QList<QNetworkCookie> cookies(QNetworkCookieJar::cookiesForUrl(url));
return cookies;
}
QList<QNetworkCookie> CCookieManager::cookiesForRequest(const QNetworkRequest &request) const
{
return cookiesForUrl(request.url());
}
bool CCookieManager::deleteCookie(const QNetworkCookie &cookie)
{
QWriteLocker l(&m_lock);
return QNetworkCookieJar::deleteCookie(cookie);
}
bool CCookieManager::insertCookie(const QNetworkCookie &cookie)
{
QWriteLocker l(&m_lock);
return QNetworkCookieJar::insertCookie(cookie);
}
bool CCookieManager::updateCookie(const QNetworkCookie &cookie)
{
QWriteLocker l(&m_lock);
return QNetworkCookieJar::updateCookie(cookie);
}
void CCookieManager::deleteAllCookies()
{
QWriteLocker l(&m_lock);
this->setAllCookies(QList<QNetworkCookie>());
}
} // ns

View File

@@ -0,0 +1,79 @@
/* Copyright (C) 2015
* swift project Community / Contributors
*
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
* including this file, may be copied, modified, propagated, or distributed except according to the terms
* contained in the LICENSE file.
*/
//! \file
#ifndef BLACKCORE_COOKIEMANAGER_H
#define BLACKCORE_COOKIEMANAGER_H
#include "blackcore/blackcoreexport.h"
#include <QNetworkCookieJar>
#include <QNetworkCookie>
#include <QNetworkRequest>
#include <QNetworkAccessManager>
#include <QReadWriteLock>
namespace BlackCore
{
/*!
* Centralized cookie manager,
* which allows thread safe sharing of cookies
*/
class BLACKCORE_EXPORT CCookieManager : QNetworkCookieJar
{
Q_OBJECT
public:
//! cookiesForUrl::setCookiesFromUrl
//! \threadsafe
virtual bool setCookiesFromUrl(const QList<QNetworkCookie> &cookies, const QUrl &url) override;
//! QNetworkCookieJar::cookiesForUrl
//! \threadsafe
virtual QList<QNetworkCookie> cookiesForUrl(const QUrl &url) const override;
//! Cookies for request
//! \threadsafe
QList<QNetworkCookie> cookiesForRequest(const QNetworkRequest &request) const;
//! \copydoc QNetworkCookieJar::deleteCookie
//! \threadsafe
virtual bool deleteCookie(const QNetworkCookie &cookie) override;
//! Delete all cookies
//! \threadsafe
void deleteAllCookies();
//! \copydoc QNetworkCookieJar::insertCookie
//! \threadsafe
virtual bool insertCookie(const QNetworkCookie &cookie) override;
//! \copydoc QNetworkCookieJar::updateCookie
//! \threadsafe
virtual bool updateCookie(const QNetworkCookie &cookie) override;
//! Our central access manager
static CCookieManager *instance();
//! Set the central instance to the given access manager
static void setToAccessManager(QNetworkAccessManager *manager);
private:
//! Constructor
CCookieManager(QObject *parent = nullptr);
//! Reset the parent, required when CookieManager is set to QNetworkAccessManager
static void resetParent();
mutable QReadWriteLock m_lock { QReadWriteLock::Recursive };
};
} // ns
#endif // guard

View File

@@ -8,6 +8,7 @@
*/ */
#include "databaseauthentication.h" #include "databaseauthentication.h"
#include "blackcore/cookiemanager.h"
#include "blackmisc/network/networkutils.h" #include "blackmisc/network/networkutils.h"
#include "blackmisc/network/url.h" #include "blackmisc/network/url.h"
#include "blackmisc/logmessage.h" #include "blackmisc/logmessage.h"
@@ -17,6 +18,7 @@
#include <QJsonDocument> #include <QJsonDocument>
#include <QHttpPart> #include <QHttpPart>
#include <QHttpMultiPart> #include <QHttpMultiPart>
#include <QCoreApplication>
using namespace BlackMisc; using namespace BlackMisc;
using namespace BlackMisc::Network; using namespace BlackMisc::Network;
@@ -28,11 +30,7 @@ namespace BlackCore
m_networkManager(new QNetworkAccessManager(this)) m_networkManager(new QNetworkAccessManager(this))
{ {
this->connect(this->m_networkManager, &QNetworkAccessManager::finished, this, &CDatabaseAuthenticationService::ps_parseServerResponse); this->connect(this->m_networkManager, &QNetworkAccessManager::finished, this, &CDatabaseAuthenticationService::ps_parseServerResponse);
} CCookieManager::setToAccessManager(this->m_networkManager);
const Network::CAuthenticatedUser &CDatabaseAuthenticationService::getUser() const
{
return m_user;
} }
void CDatabaseAuthenticationService::gracefulShutdown() void CDatabaseAuthenticationService::gracefulShutdown()
@@ -42,7 +40,7 @@ namespace BlackCore
this->logoff(); this->logoff();
} }
BlackMisc::CStatusMessageList CDatabaseAuthenticationService::login(const QString &username, const QString &password) CStatusMessageList CDatabaseAuthenticationService::login(const QString &username, const QString &password)
{ {
CStatusMessageList msgs; CStatusMessageList msgs;
static const CLogCategoryList cats(CLogCategoryList(this).join({ CLogCategory::validation()})); static const CLogCategoryList cats(CLogCategoryList(this).join({ CLogCategory::validation()}));
@@ -90,6 +88,7 @@ namespace BlackCore
url.setQuery("logoff=true"); url.setQuery("logoff=true");
QNetworkRequest request(CNetworkUtils::getNetworkRequest(url)); QNetworkRequest request(CNetworkUtils::getNetworkRequest(url));
this->m_networkManager->get(request); this->m_networkManager->get(request);
this->m_user.set(CAuthenticatedUser());
} }
void CDatabaseAuthenticationService::ps_parseServerResponse(QNetworkReply *nwReplyPtr) void CDatabaseAuthenticationService::ps_parseServerResponse(QNetworkReply *nwReplyPtr)
@@ -101,6 +100,7 @@ namespace BlackCore
QString urlString(nwReply->url().toString()); QString urlString(nwReply->url().toString());
if (urlString.toLower().contains("logoff")) if (urlString.toLower().contains("logoff"))
{ {
CCookieManager::instance()->deleteAllCookies();
emit logoffFinished(); emit logoffFinished();
return; return;
} }
@@ -134,6 +134,7 @@ namespace BlackCore
msgs.push_back(CStatusMessage(cats, CStatusMessage::SeverityError, "User has no roles")); msgs.push_back(CStatusMessage(cats, CStatusMessage::SeverityError, "User has no roles"));
} }
} }
this->m_user.set(user);
emit userAuthenticationFinished(user, msgs); emit userAuthenticationFinished(user, msgs);
} }
else else
@@ -143,4 +144,9 @@ namespace BlackCore
} }
} }
void CDatabaseAuthenticationService::ps_userChanged()
{
// code goes here
}
} // namespace } // namespace

View File

@@ -7,18 +7,21 @@
* contained in the LICENSE file. * contained in the LICENSE file.
*/ */
#ifndef BLACKCORE_DATABASE_USER_H #ifndef BLACKCORE_DATABASE_CDATABASEUATHENTICATIONSERVICE_H
#define BLACKCORE_DATABASE_USER_H #define BLACKCORE_DATABASE_CDATABASEUATHENTICATIONSERVICE_H
//! \file //! \file
#include "blackcore/blackcoreexport.h" #include "blackcore/blackcoreexport.h"
#include "blackmisc/network/authenticateduser.h" #include "blackmisc/network/authenticateduser.h"
#include "blackcore/data/globalsetup.h" #include "blackcore/data/globalsetup.h"
#include "blackcore/data/authenticateduser.h"
#include "blackmisc/statusmessagelist.h" #include "blackmisc/statusmessagelist.h"
#include <QString> #include <QString>
#include <QNetworkReply> #include <QNetworkReply>
#include <QNetworkCookieJar>
#include <QSharedPointer>
namespace BlackCore namespace BlackCore
{ {
@@ -31,9 +34,6 @@ namespace BlackCore
//! Constructor //! Constructor
CDatabaseAuthenticationService(QObject *parent = nullptr); CDatabaseAuthenticationService(QObject *parent = nullptr);
//! Get the user
const BlackMisc::Network::CAuthenticatedUser &getUser() const;
//! Shutdown //! Shutdown
void gracefulShutdown(); void gracefulShutdown();
@@ -55,11 +55,15 @@ namespace BlackCore
//! Parse login answer //! Parse login answer
void ps_parseServerResponse(QNetworkReply *nwReplyPtr); void ps_parseServerResponse(QNetworkReply *nwReplyPtr);
//! User object changed
void ps_userChanged();
private: private:
BlackMisc::Network::CAuthenticatedUser m_user; CData<BlackCore::Data::GlobalSetup> m_setup {this}; //!< data cache
CData<BlackCore::Data::GlobalSetup> m_setup {this}; //!< data cache BlackCore::CData<BlackCore::Data::AuthenticatedUser> m_user {this, &CDatabaseAuthenticationService::ps_userChanged};
QNetworkAccessManager *m_networkManager = nullptr;
bool m_shutdown = false; QNetworkAccessManager *m_networkManager = nullptr;
bool m_shutdown = false;
}; };
} // namespace } // namespace

View File

@@ -8,7 +8,9 @@
*/ */
#include "blackcore/setupreader.h" #include "blackcore/setupreader.h"
#include "blackcore/cookiemanager.h"
#include "blackmisc/sequence.h" #include "blackmisc/sequence.h"
#include "blackmisc/network/networkutils.h"
#include "blackmisc/logmessage.h" #include "blackmisc/logmessage.h"
#include "blackmisc/network/networkutils.h" #include "blackmisc/network/networkutils.h"
#include "blackmisc/fileutilities.h" #include "blackmisc/fileutilities.h"
@@ -17,6 +19,7 @@
#include <QRegularExpression> #include <QRegularExpression>
#include <QJsonDocument> #include <QJsonDocument>
#include <QJsonObject> #include <QJsonObject>
#include <QNetworkCookieJar>
#include <QTimer> #include <QTimer>
using namespace BlackMisc; using namespace BlackMisc;
@@ -30,8 +33,11 @@ namespace BlackCore
CDatabaseReader(owner, "CIcaoDataReader") CDatabaseReader(owner, "CIcaoDataReader")
{ {
this->m_networkManagerAircraft = new QNetworkAccessManager(this); this->m_networkManagerAircraft = new QNetworkAccessManager(this);
CCookieManager::setToAccessManager(this->m_networkManagerAircraft);
this->m_networkManagerAirlines = new QNetworkAccessManager(this); this->m_networkManagerAirlines = new QNetworkAccessManager(this);
CCookieManager::setToAccessManager(this->m_networkManagerAirlines);
this->m_networkManagerCountries = new QNetworkAccessManager(this); this->m_networkManagerCountries = new QNetworkAccessManager(this);
CCookieManager::setToAccessManager(this->m_networkManagerCountries);
this->connect(this->m_networkManagerAircraft, &QNetworkAccessManager::finished, this, &CIcaoDataReader::ps_parseAircraftIcaoData); this->connect(this->m_networkManagerAircraft, &QNetworkAccessManager::finished, this, &CIcaoDataReader::ps_parseAircraftIcaoData);
this->connect(this->m_networkManagerAirlines, &QNetworkAccessManager::finished, this, &CIcaoDataReader::ps_parseAirlineIcaoData); this->connect(this->m_networkManagerAirlines, &QNetworkAccessManager::finished, this, &CIcaoDataReader::ps_parseAirlineIcaoData);
@@ -125,7 +131,7 @@ namespace BlackCore
void CIcaoDataReader::ps_read(BlackMisc::Network::CEntityFlags::Entity entities) void CIcaoDataReader::ps_read(BlackMisc::Network::CEntityFlags::Entity entities)
{ {
this->threadAssertCheck(); this->threadAssertCheck(); // runs in background thread
Q_ASSERT(this->m_networkManagerAircraft); Q_ASSERT(this->m_networkManagerAircraft);
Q_ASSERT(this->m_networkManagerAirlines); Q_ASSERT(this->m_networkManagerAirlines);
Q_ASSERT(this->m_networkManagerCountries); Q_ASSERT(this->m_networkManagerCountries);
@@ -134,8 +140,7 @@ namespace BlackCore
if (entities.testFlag(CEntityFlags::AircraftIcaoEntity)) if (entities.testFlag(CEntityFlags::AircraftIcaoEntity))
{ {
QUrl url(getAircraftIcaoUrl()); QUrl url(getAircraftIcaoUrl());
QNetworkRequest requestAircraft(url); QNetworkRequest requestAircraft(CNetworkUtils::getNetworkRequest(url));
CNetworkUtils::ignoreSslVerification(requestAircraft);
this->m_networkManagerAircraft->get(requestAircraft); this->m_networkManagerAircraft->get(requestAircraft);
entitiesTriggered |= CEntityFlags::AircraftIcaoEntity; entitiesTriggered |= CEntityFlags::AircraftIcaoEntity;
} }
@@ -143,8 +148,7 @@ namespace BlackCore
if (entities.testFlag(CEntityFlags::AirlineIcaoEntity)) if (entities.testFlag(CEntityFlags::AirlineIcaoEntity))
{ {
QUrl url(getAirlineIcaoUrl()); QUrl url(getAirlineIcaoUrl());
QNetworkRequest requestAirline(url); QNetworkRequest requestAirline(CNetworkUtils::getNetworkRequest(url));
CNetworkUtils::ignoreSslVerification(requestAirline);
this->m_networkManagerAirlines->get(requestAirline); this->m_networkManagerAirlines->get(requestAirline);
entitiesTriggered |= CEntityFlags::AirlineIcaoEntity; entitiesTriggered |= CEntityFlags::AirlineIcaoEntity;
} }
@@ -152,8 +156,7 @@ namespace BlackCore
if (entities.testFlag(CEntityFlags::CountryEntity)) if (entities.testFlag(CEntityFlags::CountryEntity))
{ {
QUrl url(getCountryUrl()); QUrl url(getCountryUrl());
QNetworkRequest requestCountry(url); QNetworkRequest requestCountry(CNetworkUtils::getNetworkRequest(url));
CNetworkUtils::ignoreSslVerification(requestCountry);
this->m_networkManagerCountries->get(requestCountry); this->m_networkManagerCountries->get(requestCountry);
entitiesTriggered |= CEntityFlags::CountryEntity; entitiesTriggered |= CEntityFlags::CountryEntity;
} }

View File

@@ -8,6 +8,7 @@
*/ */
#include "blackcore/setupreader.h" #include "blackcore/setupreader.h"
#include "blackcore/cookiemanager.h"
#include "blackmisc/sequence.h" #include "blackmisc/sequence.h"
#include "blackmisc/logmessage.h" #include "blackmisc/logmessage.h"
#include "blackmisc/network/networkutils.h" #include "blackmisc/network/networkutils.h"
@@ -31,8 +32,11 @@ namespace BlackCore
CDatabaseReader(owner, "CModelDataReader") CDatabaseReader(owner, "CModelDataReader")
{ {
this->m_networkManagerLivery = new QNetworkAccessManager(this); this->m_networkManagerLivery = new QNetworkAccessManager(this);
CCookieManager::setToAccessManager(this->m_networkManagerLivery);
this->m_networkManagerDistributor = new QNetworkAccessManager(this); this->m_networkManagerDistributor = new QNetworkAccessManager(this);
CCookieManager::setToAccessManager(this->m_networkManagerDistributor);
this->m_networkManagerModel = new QNetworkAccessManager(this); this->m_networkManagerModel = new QNetworkAccessManager(this);
CCookieManager::setToAccessManager(this->m_networkManagerModel);
this->connect(this->m_networkManagerLivery, &QNetworkAccessManager::finished, this, &CModelDataReader::ps_parseLiveryData); this->connect(this->m_networkManagerLivery, &QNetworkAccessManager::finished, this, &CModelDataReader::ps_parseLiveryData);
this->connect(this->m_networkManagerDistributor, &QNetworkAccessManager::finished, this, &CModelDataReader::ps_parseDistributorData); this->connect(this->m_networkManagerDistributor, &QNetworkAccessManager::finished, this, &CModelDataReader::ps_parseDistributorData);

View File

@@ -9,6 +9,7 @@
#include "guiutility.h" #include "guiutility.h"
#include "blackcore/context_runtime.h" #include "blackcore/context_runtime.h"
#include "blackcore/cookiemanager.h"
#include "blackmisc/filelogger.h" #include "blackmisc/filelogger.h"
#include "blackmisc/logmessage.h" #include "blackmisc/logmessage.h"
#include "blackmisc/project.h" #include "blackmisc/project.h"
@@ -70,6 +71,8 @@ namespace BlackGui
void CGuiUtility::initSwiftGuiApplication(QApplication &a, const QString &applicationName, const QPixmap &icon) void CGuiUtility::initSwiftGuiApplication(QApplication &a, const QString &applicationName, const QPixmap &icon)
{ {
CRuntime::registerMetadata(); // register metadata CRuntime::registerMetadata(); // register metadata
CCookieManager::instance(); // init cookie manager if ever needed
CLogHandler::instance()->install(); // make sure we have a log handler! CLogHandler::instance()->install(); // make sure we have a log handler!
QApplication::setApplicationName(applicationName); QApplication::setApplicationName(applicationName);