Commit f0ba3c9a authored by Jesus Fernandez's avatar Jesus Fernandez

Added QOAuth1::requestToken

parent 7072c2cf
......@@ -3,4 +3,4 @@ DEPENDPATH += $$OUT_PWD/../QtOAuth
win32:CONFIG(release, debug|release): LIBS += -L$$OUT_PWD/../QtOAuth/release/ -lQtOAuth
else:win32:CONFIG(debug, debug|release): LIBS += -L$$OUT_PWD/../QtOAuth/debug/ -lQtOAuth
else:unix: LIBS += -L$$OUT_PWD/../QtOAuth/ -lQtOAuth
else:unix: LIBS += -L$$OUT_PWD/../../QtOAuth/ -lQtOAuth
TEMPLATE = subdirs
SUBDIRS += QtOAuth twittertimeline redditclient
SUBDIRS += \
QtOAuth \
tests \
examples
......@@ -87,8 +87,11 @@ public:
QNetworkAccessManager::Operation operation,
const QVariantMap parameters);
QNetworkReply *requestToken(QNetworkAccessManager::Operation operation, const QUrl &url,
QNetworkReply *requestToken(QNetworkAccessManager::Operation operation,
const QUrl &url,
const QOAuthTokenPair &token,
const QVariantMap &additionalParameters);
QNetworkReply *requestAuthorizationGrant(QNetworkAccessManager::Operation operation,
const QUrl &url,
const QVariantMap &additionalParameters);
......
......@@ -92,8 +92,6 @@ void QOAuth1Private::_q_onTokenRequestFinished()
else {
q->setToken(qMakePair(token, tokenSecret));
q->setStatus(QAbstractOAuth::Status::TokenReceived);
q->postAuthorizationGrant();
}
}
......@@ -187,22 +185,14 @@ void QOAuth1Private::appendSignature(QVariantMap *headers, const QUrl &url,
}
QNetworkReply *QOAuth1Private::requestToken(QNetworkAccessManager::Operation operation,
const QUrl &url, const QVariantMap &parameters)
const QUrl &url,
const QOAuthTokenPair &token,
const QVariantMap &parameters)
{
Q_Q(QOAuth1);
if (q->status() != QAbstractOAuth::Status::NotAuthenticated) {
qWarning("Already authenticated");
return nullptr;
} else if (!networkAccessManager) {
qCritical("QNetworkAccessManager not available");
return nullptr;
}
q->setStatus(QAbstractOAuth::Status::NotAuthenticated);
q->setToken(QOAuthTokenPair());
extraTokens.clear();
Q_EMIT q->extraTokensChanged(extraTokens);
Q_ASSERT(networkAccessManager);
Q_ASSERT(operation == QNetworkAccessManager::GetOperation ||
operation == QNetworkAccessManager::PostOperation);
QNetworkRequest request(url);
request.setAttribute(QNetworkRequest::FollowRedirectsAttribute, true);
......@@ -210,6 +200,8 @@ QNetworkReply *QOAuth1Private::requestToken(QNetworkAccessManager::Operation ope
QVariantMap headers;
appendCommonHeaders(&headers);
headers.insert(QStringLiteral("oauth_callback"), q->callback());
if (!token.first.isEmpty())
headers.insert(QStringLiteral("oauth_token"), token.first);
appendSignature(&headers, url, operation, parameters);
request.setRawHeader("Authorization", q->generateAuthorizationHeader(headers));
......@@ -382,6 +374,49 @@ QNetworkReply *QOAuth1::post(const QNetworkRequest &req, const QVariantMap &para
return d->networkAccessManager->post(request, data.toLatin1());
}
QNetworkReply *QOAuth1::requestToken(QNetworkAccessManager::Operation operation,
const QUrl &url,
const QVariantMap &additionalParameters)
{
Q_D(QOAuth1);
if (Q_UNLIKELY(!d->networkAccessManager)) {
qCritical("QNetworkAccessManager not available");
return nullptr;
}
if (Q_UNLIKELY(url.isEmpty())) {
qCritical() << "Request Url not set";
return nullptr;
}
if (Q_UNLIKELY(operation != QNetworkAccessManager::GetOperation &&
operation != QNetworkAccessManager::PostOperation)) {
qCritical("Operation not supported");
return nullptr;
}
return d->requestToken(operation, url, d->token, additionalParameters);
}
QNetworkReply *QOAuth1::requestToken(QNetworkAccessManager::Operation operation,
const QUrl &url,
const QOAuthTokenPair &token,
const QVariantMap &additionalParameters)
{
Q_D(QOAuth1);
if (!d->networkAccessManager) {
qCritical("QNetworkAccessManager not available");
return nullptr;
}
if (Q_UNLIKELY(url.isEmpty())) {
qCritical() << "Request Url not set";
return nullptr;
}
if (Q_UNLIKELY(operation != QNetworkAccessManager::GetOperation &&
operation != QNetworkAccessManager::PostOperation)) {
qCritical("Operation not supported");
return nullptr;
}
return d->requestToken(operation, url, token, additionalParameters);
}
void QOAuth1::setup(QNetworkRequest *request, const QVariantMap &signingParameters,
QNetworkAccessManager::Operation operation)
{
......@@ -551,10 +586,13 @@ void QOAuth1::grant()
if (d->status == Status::NotAuthenticated) {
d->setStatus(Status::TokenReceived);
postAuthorizationGrant();
requestToken(QNetworkAccessManager::PostOperation, authorizationGrantUrl());
} else if (d->status == Status::TokenReceived) {
d->setStatus(Status::Authenticated);
Q_EMIT granted();
}
});
postRequestToken(extraTokens());
requestToken(QNetworkAccessManager::PostOperation, requestTokenUrl(), QOAuthTokenPair());
}
void QOAuth1::setExtraTokens(const QVariantMap &tokens)
......@@ -563,56 +601,6 @@ void QOAuth1::setExtraTokens(const QVariantMap &tokens)
d->extraTokens = tokens;
}
QNetworkReply * QOAuth1::getRequestToken(const QVariantMap &parameters)
{
Q_D(QOAuth1);
if (!d->networkAccessManager) {
qCritical("QNetworkAccessManager not available");
return nullptr;
}
if (Q_UNLIKELY(requestTokenUrl().isEmpty())) {
qCritical() << "Request Url not set";
return nullptr;
}
return d->requestToken(QNetworkAccessManager::GetOperation, requestTokenUrl(),
parameters);
}
QNetworkReply * QOAuth1::postRequestToken(const QVariantMap &parameters)
{
Q_D(QOAuth1);
if (Q_UNLIKELY(d->requestTokenUrl.isEmpty())) {
qCritical("Request Url not set");
return nullptr;
}
return d->requestToken(QNetworkAccessManager::PostOperation, d->requestTokenUrl,
parameters);
}
QNetworkReply * QOAuth1::getAuthorizationGrant(const QVariantMap &parameters)
{
Q_D(QOAuth1);
if (Q_UNLIKELY(d->authorizationGrantUrl.isEmpty())) {
qCritical("Authorization grant Url not set");
return nullptr;
}
return d->requestAuthorizationGrant(QNetworkAccessManager::GetOperation,
d->authorizationGrantUrl,
parameters);
}
QNetworkReply * QOAuth1::postAuthorizationGrant(const QVariantMap &parameters)
{
Q_D(QOAuth1);
if (Q_UNLIKELY(d->authorizationGrantUrl.isEmpty())) {
qCritical("Authorization grant Url not set");
return nullptr;
}
return d->requestAuthorizationGrant(QNetworkAccessManager::PostOperation,
d->authorizationGrantUrl,
parameters);
}
QT_END_NAMESPACE
#endif // QT_NO_HTTP
......@@ -50,7 +50,9 @@ class QOAuth1Private;
class Q_NETWORK_EXPORT QOAuth1: public QAbstractOAuth
{
Q_OBJECT
Q_PROPERTY(SignatureMethod signatureMethod READ signatureMethod WRITE setSignatureMethod
Q_PROPERTY(SignatureMethod signatureMethod
READ signatureMethod
WRITE setSignatureMethod
NOTIFY signatureMethodChanged)
public:
......@@ -68,6 +70,15 @@ public:
virtual QNetworkReply *post(const QNetworkRequest &req,
const QVariantMap &parameters = QVariantMap()) override;
QNetworkReply *requestToken(QNetworkAccessManager::Operation operation,
const QUrl &url,
const QVariantMap &additionalParameters = QVariantMap());
QNetworkReply *requestToken(QNetworkAccessManager::Operation operation,
const QUrl &url,
const QOAuthTokenPair &token,
const QVariantMap &additionalParameters = QVariantMap());
virtual void grant() override;
Q_SIGNALS:
......@@ -76,12 +87,6 @@ Q_SIGNALS:
protected:
void setExtraTokens(const QVariantMap &tokens);
virtual QNetworkReply *getRequestToken(const QVariantMap &parameters = QVariantMap());
virtual QNetworkReply *postRequestToken(const QVariantMap &parameters = QVariantMap());
virtual QNetworkReply *getAuthorizationGrant(const QVariantMap &parameters = QVariantMap());
virtual QNetworkReply *postAuthorizationGrant(const QVariantMap &parameters = QVariantMap());
void setup(QNetworkRequest *request,
const QVariantMap &signingParameters,
QNetworkAccessManager::Operation operation);
......
TEMPLATE = subdirs
SUBDIRS += \
twittertimeline \
redditclient
......@@ -13,4 +13,4 @@ HEADERS += \
FORMS +=
include(../QtOAuth.pri)
include(../../QtOAuth.pri)
......@@ -18,5 +18,5 @@ INSTALLS += target
FORMS += \
twitterdialog.ui
include(../QtOAuth.pri)
include(../../QtOAuth.pri)
CONFIG += testcase
TARGET = tst_qoauth
SOURCES += tst_qoauth.cpp
INCLUDEPATH += /
QT = core core-private network testlib
include(../../QtOAuth.pri)
This diff is collapsed.
TEMPLATE = subdirs
SUBDIRS += \
qoauth
for(p, SUBDIRS) {
X = $$find(p, INCLUDEPATH)
p.INCLUDEPATH += /TEST
message(Configuring test $${X})
# message(p)
}
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