diff options
author | Liang Qi <liang.qi@qt.io> | 2018-01-20 22:42:09 +0100 |
---|---|---|
committer | Liang Qi <liang.qi@qt.io> | 2018-01-20 22:42:33 +0100 |
commit | 1a251f8fcf2749604dc15a4d7d2ceb636daac61c (patch) | |
tree | 0e098b4c39e060680b0d8c90b8ecc0bd17cd5794 | |
parent | 05e9d9227203b843e817a4675948428985680ecf (diff) | |
parent | 8f4f2e0cae642ff5eb0b87ae1d4e9c5e6ccf2bd8 (diff) |
Merge remote-tracking branch 'origin/5.10' into dev
Conflicts:
.qmake.conf
Change-Id: I815c33bbe5a821a47c66d3d4b43ffd3873337c20
-rw-r--r-- | examples/oauth/redditclient/redditclient.pro | 1 | ||||
-rw-r--r-- | examples/oauth/twittertimeline/twittertimeline.pro | 1 | ||||
-rw-r--r-- | src/oauth/doc/qtnetworkauth.qdocconf | 5 | ||||
-rw-r--r-- | src/oauth/qabstractoauth2.cpp | 29 | ||||
-rw-r--r-- | src/oauth/qabstractoauth2.h | 5 | ||||
-rw-r--r-- | src/oauth/qabstractoauthreplyhandler.cpp | 84 | ||||
-rw-r--r-- | src/oauth/qoauth1.cpp | 2 | ||||
-rw-r--r-- | tests/auto/auto.pro | 1 | ||||
-rw-r--r-- | tests/auto/oauth1/tst_oauth1.cpp | 19 | ||||
-rw-r--r-- | tests/auto/oauth2/oauth2.pro | 8 | ||||
-rw-r--r-- | tests/auto/oauth2/tst_oauth2.cpp | 100 | ||||
-rw-r--r-- | tests/auto/shared/webserver.h | 11 |
12 files changed, 249 insertions, 17 deletions
diff --git a/examples/oauth/redditclient/redditclient.pro b/examples/oauth/redditclient/redditclient.pro index c507bed..456fc78 100644 --- a/examples/oauth/redditclient/redditclient.pro +++ b/examples/oauth/redditclient/redditclient.pro @@ -1,4 +1,5 @@ QT += widgets network networkauth +requires(qtConfig(listview)) TARGET = redditclient diff --git a/examples/oauth/twittertimeline/twittertimeline.pro b/examples/oauth/twittertimeline/twittertimeline.pro index 4448979..c0dcee2 100644 --- a/examples/oauth/twittertimeline/twittertimeline.pro +++ b/examples/oauth/twittertimeline/twittertimeline.pro @@ -1,4 +1,5 @@ QT = core widgets network networkauth +requires(qtConfig(tableview)) CONFIG -= app_bundle HEADERS += \ diff --git a/src/oauth/doc/qtnetworkauth.qdocconf b/src/oauth/doc/qtnetworkauth.qdocconf index 246038e..153e3ae 100644 --- a/src/oauth/doc/qtnetworkauth.qdocconf +++ b/src/oauth/doc/qtnetworkauth.qdocconf @@ -33,8 +33,7 @@ sourcedirs += .. examplesinstallpath = oauth exampledirs += ../../../examples/oauth +manifestmeta.highlighted.names = "QtNetworkAuth/Twitter Timeline Example" + navigation.landingpage = "Qt Network Authorization" navigation.cppclassespage = "Qt Network Authorization C++ Classes" - -# TODO: Remove once out of technology preview -navigation.homepage = "Qt Documentation (Technology Preview)" diff --git a/src/oauth/qabstractoauth2.cpp b/src/oauth/qabstractoauth2.cpp index 96dc096..63f8ade 100644 --- a/src/oauth/qabstractoauth2.cpp +++ b/src/oauth/qabstractoauth2.cpp @@ -40,6 +40,7 @@ #include <QtNetwork/qnetworkreply.h> #include <QtNetwork/qnetworkrequest.h> #include <QtNetwork/qnetworkaccessmanager.h> +#include <QtNetwork/qhttpmultipart.h> QT_BEGIN_NAMESPACE @@ -267,11 +268,25 @@ QNetworkReply *QAbstractOAuth2::post(const QUrl &url, const QVariantMap ¶met { Q_D(QAbstractOAuth2); const auto data = d->convertParameters(parameters); + return post(url, data); +} + +QNetworkReply *QAbstractOAuth2::post(const QUrl &url, const QByteArray &data) +{ + Q_D(QAbstractOAuth2); QNetworkReply *reply = d->networkAccessManager()->post(d->createRequest(url), data); connect(reply, &QNetworkReply::finished, [this, reply]() { emit finished(reply); }); return reply; } +QNetworkReply *QAbstractOAuth2::post(const QUrl &url, QHttpMultiPart *multiPart) +{ + Q_D(QAbstractOAuth2); + QNetworkReply *reply = d->networkAccessManager()->post(d->createRequest(url), multiPart); + connect(reply, &QNetworkReply::finished, [this, reply]() { emit finished(reply); }); + return reply; +} + /*! Sends an authenticated PUT request and returns a new QNetworkReply. The \a url and \a parameters are used to create @@ -284,11 +299,25 @@ QNetworkReply *QAbstractOAuth2::put(const QUrl &url, const QVariantMap ¶mete { Q_D(QAbstractOAuth2); const auto data = d->convertParameters(parameters); + return put(url, data); +} + +QNetworkReply *QAbstractOAuth2::put(const QUrl &url, const QByteArray &data) +{ + Q_D(QAbstractOAuth2); QNetworkReply *reply = d->networkAccessManager()->put(d->createRequest(url), data); connect(reply, &QNetworkReply::finished, std::bind(&QAbstractOAuth::finished, this, reply)); return reply; } +QNetworkReply *QAbstractOAuth2::put(const QUrl &url, QHttpMultiPart *multiPart) +{ + Q_D(QAbstractOAuth2); + QNetworkReply *reply = d->networkAccessManager()->put(d->createRequest(url), multiPart); + connect(reply, &QNetworkReply::finished, std::bind(&QAbstractOAuth::finished, this, reply)); + return reply; +} + /*! Sends an authenticated DELETE request and returns a new QNetworkReply. The \a url and \a parameters are used to create diff --git a/src/oauth/qabstractoauth2.h b/src/oauth/qabstractoauth2.h index 0f600b2..9262170 100644 --- a/src/oauth/qabstractoauth2.h +++ b/src/oauth/qabstractoauth2.h @@ -39,6 +39,7 @@ QT_BEGIN_NAMESPACE +class QHttpMultiPart; class QAbstractOAuth2Private; class Q_OAUTH_EXPORT QAbstractOAuth2 : public QAbstractOAuth { @@ -65,8 +66,12 @@ public: const QVariantMap ¶meters = QVariantMap()) override; Q_INVOKABLE QNetworkReply *post(const QUrl &url, const QVariantMap ¶meters = QVariantMap()) override; + Q_INVOKABLE virtual QNetworkReply *post(const QUrl &url, const QByteArray &data); + Q_INVOKABLE virtual QNetworkReply *post(const QUrl &url, QHttpMultiPart *multiPart); Q_INVOKABLE QNetworkReply *put(const QUrl &url, const QVariantMap ¶meters = QVariantMap()) override; + Q_INVOKABLE virtual QNetworkReply *put(const QUrl &url, const QByteArray &data); + Q_INVOKABLE virtual QNetworkReply *put(const QUrl &url, QHttpMultiPart *multiPart); Q_INVOKABLE QNetworkReply *deleteResource(const QUrl &url, const QVariantMap ¶meters = QVariantMap()) override; diff --git a/src/oauth/qabstractoauthreplyhandler.cpp b/src/oauth/qabstractoauthreplyhandler.cpp index 29fa635..001ebed 100644 --- a/src/oauth/qabstractoauthreplyhandler.cpp +++ b/src/oauth/qabstractoauthreplyhandler.cpp @@ -36,27 +36,91 @@ Q_LOGGING_CATEGORY(lcReplyHandler, "qt.networkauth.replyhandler") QT_BEGIN_NAMESPACE +/*! + \class QAbstractOAuthReplyHandler + \inmodule QtNetworkAuth + \ingroup oauth + \brief Handles replies to OAuth authentication requests + \since 5.8 + + The QAbstractOAuthReplyHandler class handles the answers + to all OAuth authentication requests. + This class is designed as a base whose subclasses implement + custom behavior in the callback() and networkReplyFinished() + methods. +*/ + +/*! + \fn QString QAbstractOAuthReplyHandler::callback() const + + Returns an absolute URI that the server will redirect the + resource owner back to when the Resource Owner Authorization step + is completed. If the client is unable to receive callbacks or a + callback URI has been established via other means, the parameter + value \b must be set to "oob" (all lower-case), to indicate an + out-of-band configuration. + + Derived classes should implement this function to provide the + expected callback type. +*/ + +/*! + \fn void QAbstractOAuthReplyHandler::networkReplyFinished(QNetworkReply *reply) + + After the server determines whether the request is valid this + function will be called. Reimplement it to get the data received + from the server wrapped in \a reply. +*/ + +/*! + \fn void QAbstractOAuthReplyHandler::callbackReceived(const QVariantMap &values) + + This signal is emitted when the reply from the server is + received, with \a values containing the token credentials + and any additional information the server may have returned. + When this signal is emitted, the authorization process + is complete. +*/ + +/*! + \fn void QAbstractOAuthReplyHandler::tokensReceived(const QVariantMap &tokens) + + This signal is emitted when new \a tokens are received from the + server. +*/ + +/*! + \fn void QAbstractOAuthReplyHandler::replyDataReceived(const QByteArray &data) + + This signal is emitted when an HTTP request finishes and the + data is available. \a data contains the response before parsing. +*/ + +/*! + \fn void QAbstractOAuthReplyHandler::callbackDataReceived(const QByteArray &data) + + This signal is emitted when a callback request is received: + \a data contains the information before parsing. +*/ + +/*! + Constructs a reply handler as a child of \a parent. +*/ QAbstractOAuthReplyHandler::QAbstractOAuthReplyHandler(QObject *parent) : QObject(parent) {} +/*! + Destroys the reply handler. +*/ QAbstractOAuthReplyHandler::~QAbstractOAuthReplyHandler() {} +/*! \internal */ QAbstractOAuthReplyHandler::QAbstractOAuthReplyHandler(QObjectPrivate &d, QObject *parent) : QObject(d, parent) {} -/*! - \class QAbstractOAuthReplyHandler - \inmodule QtNetworkAuth - \ingroup oauth - \brief Handles replies to OAuth authentication requests - \since 5.8 - - The QAbstractOAuthReplyHandler class handles the answers - to all OAuth authentication requests -*/ QT_END_NAMESPACE #endif // QT_NO_HTTP diff --git a/src/oauth/qoauth1.cpp b/src/oauth/qoauth1.cpp index 581ce4a..fcff92e 100644 --- a/src/oauth/qoauth1.cpp +++ b/src/oauth/qoauth1.cpp @@ -391,7 +391,7 @@ void QOAuth1::setClientCredentials(const QString &clientIdentifier, QString QOAuth1::tokenSecret() const { Q_D(const QOAuth1); - return d->clientIdentifierSharedKey; + return d->tokenSecret; } /*! Sets \a tokenSecret as the current token secret used to sign diff --git a/tests/auto/auto.pro b/tests/auto/auto.pro index 41f3293..a0bbb36 100644 --- a/tests/auto/auto.pro +++ b/tests/auto/auto.pro @@ -3,6 +3,7 @@ TEMPLATE = subdirs SUBDIRS += \ cmake \ oauth1 \ + oauth2 \ oauth1signature \ oauthhttpserverreplyhandler diff --git a/tests/auto/oauth1/tst_oauth1.cpp b/tests/auto/oauth1/tst_oauth1.cpp index 50822bd..7bfc628 100644 --- a/tests/auto/oauth1/tst_oauth1.cpp +++ b/tests/auto/oauth1/tst_oauth1.cpp @@ -145,6 +145,7 @@ public Q_SLOTS: private Q_SLOTS: void clientIdentifierSignal(); void clientSharedSecretSignal(); + void tokenSignal(); void tokenSecretSignal(); void temporaryCredentialsUrlSignal(); void temporaryTokenCredentialsUrlSignal(); @@ -252,7 +253,7 @@ void tst_OAuth1::clientSharedSecretSignal() PropertyTester::run(&QOAuth1::clientSharedSecretChanged, setters); } -void tst_OAuth1::tokenSecretSignal() +void tst_OAuth1::tokenSignal() { using PropertyTester = PropertyTester<QString>; PropertyTester::SetterFunctions setters { @@ -268,6 +269,22 @@ void tst_OAuth1::tokenSecretSignal() PropertyTester::run(&QOAuth1::tokenChanged, setters); } +void tst_OAuth1::tokenSecretSignal() +{ + using PropertyTester = PropertyTester<QString>; + PropertyTester::SetterFunctions setters { + [](QString *expectedValue, QOAuth1 *object) { + *expectedValue = "setTokenSecret"; + object->setTokenSecret(*expectedValue); + }, + [](QString *expectedValue, QOAuth1 *object) { + *expectedValue = "setTokenCredentials"; + object->setTokenCredentials(qMakePair(QString(), *expectedValue)); + } + }; + PropertyTester::run(&QOAuth1::tokenSecretChanged, setters); +} + void tst_OAuth1::temporaryCredentialsUrlSignal() { using PropertyTester = PropertyTester<QUrl>; diff --git a/tests/auto/oauth2/oauth2.pro b/tests/auto/oauth2/oauth2.pro new file mode 100644 index 0000000..74f3fda --- /dev/null +++ b/tests/auto/oauth2/oauth2.pro @@ -0,0 +1,8 @@ +TEMPLATE = app +CONFIG += testcase +TARGET = tst_oauth2 +SOURCES += tst_oauth2.cpp + +include(../shared/shared.pri) + +QT = core core-private network networkauth networkauth-private testlib diff --git a/tests/auto/oauth2/tst_oauth2.cpp b/tests/auto/oauth2/tst_oauth2.cpp new file mode 100644 index 0000000..826be06 --- /dev/null +++ b/tests/auto/oauth2/tst_oauth2.cpp @@ -0,0 +1,100 @@ +/**************************************************************************** +** +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtTest> + +#include <QtNetworkAuth/qabstractoauthreplyhandler.h> +#include <QtNetworkAuth/qoauth2authorizationcodeflow.h> + +#include "webserver.h" + +class tst_OAuth2 : public QObject +{ + Q_OBJECT + +private Q_SLOTS: + void getToken(); +}; + +struct ReplyHandler : QAbstractOAuthReplyHandler +{ + QString callback() const override + { + return QLatin1String("test"); + } + + void networkReplyFinished(QNetworkReply *reply) override + { + QVariantMap data; + const auto items = QUrlQuery(reply->readAll()).queryItems(); + for (const auto &pair : items) + data.insert(pair.first, pair.second); + Q_EMIT tokensReceived(data); + } + + void emitCallbackReceived(const QVariantMap &data) + { + Q_EMIT callbackReceived(data); + } +}; + +void tst_OAuth2::getToken() +{ + WebServer webServer([](const WebServer::HttpRequest &request, QTcpSocket *socket) { + if (request.url.path() == QLatin1String("/accessToken")) { + const QString text = "access_token=token&token_type=bearer"; + const QByteArray replyMessage { + "HTTP/1.0 200 OK\r\n" + "Content-Type: application/x-www-form-urlencoded; charset=\"utf-8\"\r\n" + "Content-Length: " + QByteArray::number(text.size()) + "\r\n\r\n" + + text.toUtf8() + }; + socket->write(replyMessage); + } + }); + QOAuth2AuthorizationCodeFlow oauth2; + oauth2.setAuthorizationUrl(webServer.url(QLatin1String("authorization"))); + oauth2.setAccessTokenUrl(webServer.url(QLatin1String("accessToken"))); + auto replyHandler = new ReplyHandler; + oauth2.setReplyHandler(replyHandler); + connect(&oauth2, &QOAuth2AuthorizationCodeFlow::authorizeWithBrowser, [&](const QUrl &url) { + const QUrlQuery query(url.query()); + replyHandler->emitCallbackReceived(QVariantMap { + { QLatin1String("code"), QLatin1String("test") }, + { QLatin1String("state"), + query.queryItemValue(QLatin1String("state")) } + }); + }); + QSignalSpy grantedSpy(&oauth2, &QOAuth2AuthorizationCodeFlow::granted); + oauth2.grant(); + QTRY_COMPARE(grantedSpy.count(), 1); + QCOMPARE(oauth2.token(), QLatin1String("token")); +} + +QTEST_MAIN(tst_OAuth2) +#include "tst_oauth2.moc" diff --git a/tests/auto/shared/webserver.h b/tests/auto/shared/webserver.h index 458c24a..b28a0e5 100644 --- a/tests/auto/shared/webserver.h +++ b/tests/auto/shared/webserver.h @@ -32,6 +32,7 @@ #include <functional> #include <cctype> +#include <QtCore/qcoreapplication.h> #include <QtNetwork/qtcpserver.h> class WebServer : public QTcpServer @@ -268,8 +269,14 @@ bool WebServer::HttpRequest::readBody(QTcpSocket *socket) return false; fragment.resize(bytesLeft); } - while (socket->bytesAvailable() && bytesLeft) - bytesLeft -= socket->read(&fragment.data()[fragment.size() - bytesLeft], bytesLeft); + while (bytesLeft) { + int got = socket->read(&fragment.data()[fragment.size() - bytesLeft], bytesLeft); + if (got < 0) + return false; // error + bytesLeft -= got; + if (bytesLeft) + qApp->processEvents(); + } fragment.swap(body); state = State::AllDone; return true; |