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

View File

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

View File

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

View File

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

View File

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