Commit 06cf71c3 authored by Jesus Fernandez's avatar Jesus Fernandez

OAuth2 now uses reply handler

parent b857079a
......@@ -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;
......
......@@ -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
......@@ -60,7 +63,11 @@ QOAuthOobReplyHandler::QOAuthOobReplyHandler(QObjectPrivate &d, QObject *parent)
void QOAuthOobReplyHandler::networkReplyFinished(QNetworkReply *reply)
{
if (reply->header(QNetworkRequest::ContentTypeHeader).isNull()) {
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;
}
......@@ -82,6 +89,19 @@ void QOAuthOobReplyHandler::networkReplyFinished(QNetworkReply *reply)
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));
......
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