Commit 1b74bdb0 authored by Jesus Fernandez's avatar Jesus Fernandez

QNetworkAccessManager changed to optional

parent 54a8c221
......@@ -77,15 +77,16 @@ public:
QAbstractOAuthPrivate(QNetworkAccessManager *manager);
~QAbstractOAuthPrivate();
QNetworkAccessManager *networkAccessManager();
void setStatus(QAbstractOAuth::Status status);
QUrl authorizationUrl;
QVariantMap extraTokens;
QAbstractOAuth::Status status;
QAbstractOAuth::Status status = QAbstractOAuth::Status::NotAuthenticated;
QNetworkAccessManager::Operation operation;
QScopedPointer<QAbstractOAuthReplyHandler> replyHandler;
QScopedPointer<QOAuthOobReplyHandler> defaultReplyHandler;
QNetworkAccessManager *networkAccessManager;
QPointer<QNetworkAccessManager> networkAccessManagerPointer;
QAbstractOAuth::ModifyParametersFunction modifyParametersFunction;
static QUrlQuery createQuery(const QVariantMap &parameters);
......
......@@ -58,10 +58,9 @@ QT_BEGIN_NAMESPACE
Q_DECLARE_METATYPE(QAbstractOAuth::Error)
QAbstractOAuthPrivate::QAbstractOAuthPrivate(QNetworkAccessManager *manager)
: status(QAbstractOAuth::Status::NotAuthenticated),
replyHandler(nullptr),
: replyHandler(nullptr),
defaultReplyHandler(new QOAuthOobReplyHandler),
networkAccessManager(manager)
networkAccessManagerPointer(manager)
{
Q_ASSERT(manager);
}
......@@ -69,6 +68,14 @@ QAbstractOAuthPrivate::QAbstractOAuthPrivate(QNetworkAccessManager *manager)
QAbstractOAuthPrivate::~QAbstractOAuthPrivate()
{}
QNetworkAccessManager *QAbstractOAuthPrivate::networkAccessManager()
{
Q_Q(QAbstractOAuth);
if (!networkAccessManagerPointer)
networkAccessManagerPointer = new QNetworkAccessManager(q);
return networkAccessManagerPointer.data();
}
void QAbstractOAuthPrivate::setStatus(QAbstractOAuth::Status newStatus)
{
Q_Q(QAbstractOAuth);
......@@ -104,6 +111,22 @@ QAbstractOAuth::QAbstractOAuth(QAbstractOAuthPrivate &dd, QObject *parent)
QAbstractOAuth::~QAbstractOAuth()
{}
QNetworkAccessManager *QAbstractOAuth::networkAccessManager() const
{
Q_D(const QAbstractOAuth);
return d->networkAccessManagerPointer.data();
}
void QAbstractOAuth::setNetworkAccessManager(QNetworkAccessManager *networkAccessManager)
{
Q_D(QAbstractOAuth);
if (networkAccessManager != d->networkAccessManagerPointer) {
if (d->networkAccessManagerPointer && d->networkAccessManagerPointer->parent() == this)
delete d->networkAccessManagerPointer.data();
d->networkAccessManagerPointer = networkAccessManager;
}
}
QAbstractOAuth::Status QAbstractOAuth::status() const
{
Q_D(const QAbstractOAuth);
......
......@@ -106,6 +106,9 @@ public:
virtual ~QAbstractOAuth();
QNetworkAccessManager *networkAccessManager() const;
void setNetworkAccessManager(QNetworkAccessManager *networkAccessManager);
Status status() const;
// Resource Owner Authorization: https://tools.ietf.org/html/rfc5849#section-2.2
......
......@@ -125,7 +125,7 @@ QNetworkReply *QAbstractOAuth2::get(const QNetworkRequest &req, const QVariantMa
const QString bearer = d->bearerFormat.arg(d->token.key);
request.setRawHeader("Authorization", bearer.toUtf8());
return d->networkAccessManager->get(request);
return d->networkAccessManager()->get(request);
}
QNetworkReply *QAbstractOAuth2::post(const QNetworkRequest &req, const QVariantMap &parameters)
......@@ -146,7 +146,7 @@ QNetworkReply *QAbstractOAuth2::post(const QNetworkRequest &req, const QVariantM
const QString bearer = d->bearerFormat.arg(d->token.key);
request.setRawHeader("Authorization", bearer.toUtf8());
return d->networkAccessManager->post(request, QByteArray());
return d->networkAccessManager()->post(request, QByteArray());
}
QString QAbstractOAuth2::scope() const
......
......@@ -74,10 +74,10 @@ const QString String::oauthVersion = QStringLiteral("oauth_version");
static auto networkReplyErrorFunctionPointer = static_cast<void(QNetworkReply::*)(
QNetworkReply::NetworkError)>(&QNetworkReply::error);
QOAuth1Private::QOAuth1Private(QNetworkAccessManager *manager)
: QAbstractOAuthPrivate(manager)
QOAuth1Private::QOAuth1Private(QNetworkAccessManager *networkAccessManager)
: QAbstractOAuthPrivate(networkAccessManager)
{
Q_ASSERT(manager);
Q_ASSERT(networkAccessManager);
qRegisterMetaType<QOAuth1::Credential>("QOAuth1::Credential");
qRegisterMetaType<QNetworkReply::NetworkError>("QNetworkReply::NetworkError");
}
......@@ -196,7 +196,7 @@ QNetworkReply *QOAuth1Private::requestToken(QNetworkAccessManager::Operation ope
Q_Q(QOAuth1);
typedef OAuth1KeyString Key;
Q_ASSERT(networkAccessManager);
Q_ASSERT(networkAccessManager());
Q_ASSERT(operation == QNetworkAccessManager::GetOperation ||
operation == QNetworkAccessManager::PostOperation);
......@@ -222,14 +222,14 @@ QNetworkReply *QOAuth1Private::requestToken(QNetworkAccessManager::Operation ope
url.setQuery(QOAuth1Private::createQuery(parameters));
request.setUrl(url);
}
reply = networkAccessManager->get(request);
reply = networkAccessManager()->get(request);
}
else if (operation == QNetworkAccessManager::PostOperation) {
QUrlQuery query = QOAuth1Private::createQuery(parameters);
const QByteArray data = query.toString(QUrl::PrettyDecoded).toUtf8();
request.setHeader(QNetworkRequest::ContentTypeHeader,
QStringLiteral("application/x-www-form-urlencoded"));
reply = networkAccessManager->post(request, data);
reply = networkAccessManager()->post(request, data);
}
connect(reply, networkReplyErrorFunctionPointer, this, &QOAuth1Private::_q_onTokenRequestError);
......@@ -390,13 +390,13 @@ QNetworkReply *QOAuth1::get(const QNetworkRequest &req, const QVariantMap &param
allParameters.unite(parameters);
setup(&request, parameters, QNetworkAccessManager::GetOperation);
return d->networkAccessManager->get(request);
return d->networkAccessManager()->get(request);
}
QNetworkReply *QOAuth1::post(const QNetworkRequest &req, const QVariantMap &parameters)
{
Q_D(QOAuth1);
if (!d->networkAccessManager) {
if (!d->networkAccessManager()) {
qCritical("QNetworkAccessManager not available");
return nullptr;
}
......@@ -410,7 +410,7 @@ QNetworkReply *QOAuth1::post(const QNetworkRequest &req, const QVariantMap &para
for (auto it = parameters.begin(), end = parameters.end(); it != end; ++it)
query.addQueryItem(it.key(), it.value().toString());
QString data = query.toString(QUrl::FullyEncoded);
return d->networkAccessManager->post(request, data.toUtf8());
return d->networkAccessManager()->post(request, data.toUtf8());
}
QNetworkReply *QOAuth1::requestTemporaryCredentials(QNetworkAccessManager::Operation operation,
......@@ -418,7 +418,7 @@ QNetworkReply *QOAuth1::requestTemporaryCredentials(QNetworkAccessManager::Opera
const QVariantMap &parameters)
{
Q_D(QOAuth1);
if (Q_UNLIKELY(!d->networkAccessManager)) {
if (Q_UNLIKELY(!d->networkAccessManager())) {
qCritical("QNetworkAccessManager not available");
return nullptr;
}
......@@ -441,7 +441,7 @@ QNetworkReply *QOAuth1::requestTokenCredentials(QNetworkAccessManager::Operation
const QVariantMap &parameters)
{
Q_D(QOAuth1);
if (!d->networkAccessManager) {
if (!d->networkAccessManager()) {
qCritical("QNetworkAccessManager not available");
return nullptr;
}
......
......@@ -160,8 +160,6 @@ QOAuth2AuthorizationCodeFlow::QOAuth2AuthorizationCodeFlow(QNetworkAccessManager
QObject *parent)
: QAbstractOAuth2(*new QOAuth2AuthorizationCodeFlowPrivate(manager, port), parent)
{
Q_D(QOAuth2AuthorizationCodeFlow);
d->networkAccessManager = manager;
}
QOAuth2AuthorizationCodeFlow::QOAuth2AuthorizationCodeFlow(const QString &consumerKey,
......@@ -171,7 +169,6 @@ QOAuth2AuthorizationCodeFlow::QOAuth2AuthorizationCodeFlow(const QString &consum
: QAbstractOAuth2(*new QOAuth2AuthorizationCodeFlowPrivate(manager, port), parent)
{
Q_D(QOAuth2AuthorizationCodeFlow);
d->networkAccessManager = manager;
d->consumer = consumerKey;
}
......@@ -196,7 +193,6 @@ QOAuth2AuthorizationCodeFlow::QOAuth2AuthorizationCodeFlow(const QString &consum
: QAbstractOAuth2(*new QOAuth2AuthorizationCodeFlowPrivate(manager, port), parent)
{
Q_D(QOAuth2AuthorizationCodeFlow);
d->networkAccessManager = manager;
d->authorizationUrl = authenticateUrl;
d->accessTokenUrl = accessTokenUrl;
d->consumer = consumerKey;
......@@ -289,10 +285,11 @@ void QOAuth2AuthorizationCodeFlow::requestAccessToken(const QString &code)
QStringLiteral("application/x-www-form-urlencoded"));
const auto data = query.toString(QUrl::FullyEncoded);
d->accessTokenReply = d->networkAccessManager->post(request, data.toUtf8());
QObjectPrivate::connect(d->networkAccessManager, &QNetworkAccessManager::finished,
d->accessTokenReply = d->networkAccessManager()->post(request, data.toUtf8());
QObjectPrivate::connect(d->networkAccessManager(), &QNetworkAccessManager::finished,
d, &QOAuth2AuthorizationCodeFlowPrivate::accessTokenRequestFinished);
QObjectPrivate::connect(d->networkAccessManager, &QNetworkAccessManager::authenticationRequired,
QObjectPrivate::connect(d->networkAccessManager(),
&QNetworkAccessManager::authenticationRequired,
d, &QOAuth2AuthorizationCodeFlowPrivate::authenticate,
Qt::UniqueConnection);
}
......
......@@ -63,8 +63,6 @@ QOAuth2ImplicitGrantFlow::QOAuth2ImplicitGrantFlow(QNetworkAccessManager *manage
QObject *parent)
: QAbstractOAuth2(*new QOAuth2ImplicitGrantFlowPrivate(manager), parent)
{
Q_D(QOAuth2ImplicitGrantFlow);
d->networkAccessManager = manager;
}
QOAuth2ImplicitGrantFlow::QOAuth2ImplicitGrantFlow(const QString &consumerToken,
......@@ -73,7 +71,6 @@ QOAuth2ImplicitGrantFlow::QOAuth2ImplicitGrantFlow(const QString &consumerToken,
: QAbstractOAuth2(*new QOAuth2ImplicitGrantFlowPrivate(manager), parent)
{
Q_D(QOAuth2ImplicitGrantFlow);
d->networkAccessManager = manager;
d->consumer = consumerToken;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment