Commit f0ba3c9a authored by Jesus Fernandez's avatar Jesus Fernandez

Added QOAuth1::requestToken

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