Commit 28ad61dc authored by Jesus Fernandez's avatar Jesus Fernandez

Changes in reply handler

parent 8d1ea38f
......@@ -70,7 +70,7 @@ public:
QOAuth2AuthorizationCodeFlowPrivate(QNetworkAccessManager *manager = nullptr);
void _q_handleCallback(const QVariantMap &data);
void _q_accessTokenRequestFinished(QNetworkReply *reply);
void _q_accessTokenRequestFinished(const QVariantMap &values);
void _q_authenticate(QNetworkReply *reply, QAuthenticator *authenticator);
QUrl accessTokenUrl;
......
......@@ -39,8 +39,11 @@
#ifndef QT_NO_HTTP
#include <qobject.h>
#include <qabstractoauthreplyhandler.h>
#include <QtOAuth/qabstractoauthreplyhandler.h>
#include <QtCore/qobject.h>
#include <QtNetwork/qnetworkreply.h>
QT_BEGIN_NAMESPACE
......
......@@ -59,7 +59,7 @@ public:
virtual QString callback() const = 0;
public Q_SLOTS:
virtual void networkReplyFinished() = 0;
virtual void networkReplyFinished(QNetworkReply *reply) = 0;
Q_SIGNALS:
void callbackReceived(const QVariantMap &values);
......@@ -68,6 +68,7 @@ Q_SIGNALS:
void replyDataReceived(const QByteArray &data);
void callbackDataReceived(const QByteArray &data);
protected:
QAbstractOAuthReplyHandler(QObjectPrivate &d, QObject *parent = nullptr);
......
......@@ -159,10 +159,10 @@ QNetworkReply *QOAuth1Private::requestToken(QNetworkAccessManager::Operation ope
connect(reply, networkReplyErrorFunctionPointer, this, &QOAuth1Private::_q_onTokenRequestError);
const QAbstractOAuthReplyHandler *handler = replyHandler ? replyHandler.data()
: defaultReplyHandler.data();
QObject::connect(reply, SIGNAL(finished()), handler, SLOT(networkReplyFinished()),
Qt::UniqueConnection);
QAbstractOAuthReplyHandler *handler = replyHandler ? replyHandler.data()
: defaultReplyHandler.data();
QObject::connect(reply, &QNetworkReply::finished,
std::bind(&QAbstractOAuthReplyHandler::networkReplyFinished, handler, reply));
connect(handler, &QAbstractOAuthReplyHandler::tokensReceived, this,
&QOAuth1Private::_q_tokensReceived);
......
......@@ -99,42 +99,25 @@ void QOAuth2AuthorizationCodeFlowPrivate::_q_handleCallback(const QVariantMap &d
q->requestAccessToken(code);
}
void QOAuth2AuthorizationCodeFlowPrivate::_q_accessTokenRequestFinished(QNetworkReply *reply)
void QOAuth2AuthorizationCodeFlowPrivate::_q_accessTokenRequestFinished(const QVariantMap &values)
{
Q_Q(QOAuth2AuthorizationCodeFlow);
typedef QAbstractOAuth2Private::OAuth2KeyString Key;
if (reply != currentReply)
return;
if (reply->error() == QNetworkReply::UnknownNetworkError) {
qWarning("QOAuth2AuthorizationCodeFlow: %s", qPrintable(reply->errorString()));
setStatus(QAbstractOAuth::Status::NotAuthenticated);
return;
}
const QByteArray json = reply->readAll();
Q_EMIT q->replyDataReceived(json);
if (json.isEmpty()) {
qCritical("QOAuth2AuthorizationCodeFlowPrivate::accessTokenRequestFinished: Empty answer");
return;
}
qDebug("QOAuth2AuthorizationCodeFlowPrivate::accessTokenRequestFinished: %s", qPrintable(json));
const auto document = QJsonDocument::fromJson(json);
Q_ASSERT(document.isObject());
const auto object = document.object();
if (object.contains(Key::error)) {
const QString error = object.value(Key::error).toString();
if (values.contains(Key::error)) {
const QString error = values.value(Key::error).toString();
qCritical("QOAuth2AuthorizationCodeFlow Error: %s", qPrintable(error));
return;
}
const QString accessToken = object.value(Key::accessToken).toString();
tokenType = object.value(Key::tokenType).toString();
const int expiresIn = object.value(Key::expiresIn).toInt(-1);
refreshToken = object.value(Key::refreshToken).toString();
scope = object.value(Key::scope).toString();
bool ok;
const QString accessToken = values.value(Key::accessToken).toString();
tokenType = values.value(Key::tokenType).toString();
int expiresIn = values.value(Key::expiresIn).toInt(&ok);
if (!ok)
expiresIn = -1;
refreshToken = values.value(Key::refreshToken).toString();
scope = values.value(Key::scope).toString();
if (accessToken.isEmpty()) {
qWarning("QOAuth2AuthorizationCodeFlow: Access token not received");
return;
......@@ -261,8 +244,9 @@ void QOAuth2AuthorizationCodeFlow::refreshAccessToken()
const QString data = query.toString(QUrl::FullyEncoded);
d->currentReply = d->networkAccessManager()->post(request, data.toUtf8());
QObjectPrivate::connect(d->networkAccessManager(), &QNetworkAccessManager::finished,
d, &QOAuth2AuthorizationCodeFlowPrivate::_q_accessTokenRequestFinished);
QObject::connect(d->currentReply, &QNetworkReply::finished,
std::bind(&QAbstractOAuthReplyHandler::networkReplyFinished, replyHandler(),
d->currentReply));
QObjectPrivate::connect(d->networkAccessManager(),
&QNetworkAccessManager::authenticationRequired,
d, &QOAuth2AuthorizationCodeFlowPrivate::_q_authenticate,
......@@ -318,8 +302,12 @@ void QOAuth2AuthorizationCodeFlow::requestAccessToken(const QString &code)
const QString data = query.toString(QUrl::FullyEncoded);
d->currentReply = d->networkAccessManager()->post(request, data.toUtf8());
QObjectPrivate::connect(d->networkAccessManager(), &QNetworkAccessManager::finished,
d, &QOAuth2AuthorizationCodeFlowPrivate::_q_accessTokenRequestFinished);
QObject::connect(d->currentReply, &QNetworkReply::finished,
std::bind(&QAbstractOAuthReplyHandler::networkReplyFinished, replyHandler(),
d->currentReply));
QObjectPrivate::connect(d->replyHandler, &QAbstractOAuthReplyHandler::tokensReceived, d,
&QOAuth2AuthorizationCodeFlowPrivate::_q_accessTokenRequestFinished,
Qt::UniqueConnection);
QObjectPrivate::connect(d->networkAccessManager(),
&QNetworkAccessManager::authenticationRequired,
d, &QOAuth2AuthorizationCodeFlowPrivate::_q_authenticate,
......
......@@ -39,9 +39,12 @@
#ifndef QT_NO_HTTP
#include <qoauthoobreplyhandler.h>
#include <QtOAuth/qoauthoobreplyhandler.h>
#include <qnetworkreply.h>
#include <QtCore/qjsonobject.h>
#include <QtCore/qjsondocument.h>
#include <QtNetwork/qnetworkreply.h>
QT_BEGIN_NAMESPACE
......@@ -58,20 +61,54 @@ QOAuthOobReplyHandler::QOAuthOobReplyHandler(QObjectPrivate &d, QObject *parent)
: QAbstractOAuthReplyHandler(d, parent)
{}
void QOAuthOobReplyHandler::networkReplyFinished()
void QOAuthOobReplyHandler::networkReplyFinished(QNetworkReply *reply)
{
QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender());
Q_ASSERT(reply);
if (reply->error() == QNetworkReply::UnknownNetworkError) {
qWarning("QOAuthOobReplyHandler::networkReplyFinished: %s",
qPrintable(reply->errorString()));
return;
} else if (reply->header(QNetworkRequest::ContentTypeHeader).isNull()) {
qCritical("QOAuthOobReplyHandler::networkReplyFinished: Empty Content-type header");
return;
}
const QString contentType = reply->header(QNetworkRequest::ContentTypeHeader).toString();
const QByteArray data = reply->readAll();
Q_EMIT replyDataReceived(data);
if (data.isEmpty())
if (data.isEmpty()) {
qCritical("QOAuthOobReplyHandler::networkReplyFinished: No received data");
return;
}
const QVariantMap response = parseResponse(data);
if (response.isEmpty())
Q_EMIT replyDataReceived(data);
QVariantMap ret;
if (contentType.startsWith(QStringLiteral("text/html"))) {
QList<QByteArray> parameters = data.split('&');
for (auto it = parameters.begin(), end = parameters.end(); it != end; ++it) {
QList<QByteArray> kv = it->split('=');
if (kv.length() == 2)
ret.insert(QUrl::fromPercentEncoding(kv[0]), QUrl::fromPercentEncoding(kv[1]));
}
} else if (contentType.startsWith(QStringLiteral("application/json"))) {
const QJsonDocument document = QJsonDocument::fromJson(data);
if (!document.isObject()) {
qCritical("QOAuthOobReplyHandler::networkReplyFinished: Received data is not a JSON"
"object: %s", qPrintable(data));
return;
}
const QJsonObject object = document.object();
if (object.isEmpty()) {
qCritical("QOAuthOobReplyHandler::networkReplyFinished: Received empty JSON object: %s",
qPrintable(data));
}
ret = object.toVariantMap();
} else {
qCritical("QOAuthOobReplyHandler::networkReplyFinished: Unknown Content-type: %s",
qPrintable(contentType));
return;
}
Q_EMIT tokensReceived(response);
Q_EMIT tokensReceived(ret);
}
QVariantMap QOAuthOobReplyHandler::parseResponse(const QByteArray &response)
......
......@@ -58,8 +58,7 @@ public:
protected:
QOAuthOobReplyHandler(QObjectPrivate &d, QObject *parent = nullptr);
protected Q_SLOTS:
virtual void networkReplyFinished() override;
virtual void networkReplyFinished(QNetworkReply *reply) override;
private:
QVariantMap parseResponse(const QByteArray &response);
......
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