From 51b8c15147bd41d75be352f89ea2315c1aae8c01 Mon Sep 17 00:00:00 2001 From: Kyle Fowler Date: Wed, 11 Apr 2012 11:01:21 -0700 Subject: [PATCH] Oauth2 updates --- oauth/kqoauthauthreplyserver.cpp | 40 +++++++++++++++++++++++++------- oauth/kqoauthauthreplyserver_p.h | 2 ++ oauth/kqoauthmanager.cpp | 32 ++++++++++++++++++++++++- oauth/kqoauthmanager.h | 2 ++ oauth/kqoauthrequest.cpp | 4 +--- 5 files changed, 67 insertions(+), 13 deletions(-) diff --git a/oauth/kqoauthauthreplyserver.cpp b/oauth/kqoauthauthreplyserver.cpp index cc666db..d1883d6 100644 --- a/oauth/kqoauthauthreplyserver.cpp +++ b/oauth/kqoauthauthreplyserver.cpp @@ -20,6 +20,7 @@ #include #include #include +#include #include "kqoauthauthreplyserver.h" #include "kqoauthauthreplyserver_p.h" @@ -37,7 +38,7 @@ KQOAuthAuthReplyServerPrivate::~KQOAuthAuthReplyServerPrivate() void KQOAuthAuthReplyServerPrivate::onIncomingConnection() { Q_Q(KQOAuthAuthReplyServer); - + qDebug() << "Incoming Connection"; socket = q->nextPendingConnection(); connect(socket, SIGNAL(readyRead()), this, SLOT(onBytesReady()), Qt::UniqueConnection); @@ -45,10 +46,32 @@ void KQOAuthAuthReplyServerPrivate::onIncomingConnection() { void KQOAuthAuthReplyServerPrivate::onBytesReady() { Q_Q(KQOAuthAuthReplyServer); - + qDebug() << "Socket peer host address: " << socket->peerAddress(); QByteArray reply; QByteArray content; - content.append("

Account authorized, go ahead back to the tumblr app and start your experience!

"); + QFile file(localFile); + if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { + + } else { + qDebug() << "first url worked"; + QTextStream in(&file); + while (!in.atEnd()) { + QString line = in.readLine(); + qDebug() << line; + } + } + + QByteArray data = socket->readAll(); + qDebug()<< "Query Data: " << data; + QMultiMap queryParams = parseQueryParams(&data); + if(queryParams.size() == 0 && !handlingRedirect) { //assume theres a hash and do the redirect hack + handlingRedirect = true; + content.append("

Account authorized, go ahead back to the tumblr app and start your experience!

"); + } else { + handlingRedirect = false; + //TODO then send down the local file if there is one + content.append("

Account linked, go ahead back to the app and check the status!

"); + } reply.append("HTTP/1.0 200 OK \r\n"); reply.append("Content-Type: text/html; charset=\"utf-8\"\r\n"); @@ -56,13 +79,12 @@ void KQOAuthAuthReplyServerPrivate::onBytesReady() { reply.append("\r\n"); reply.append(content); socket->write(reply); - - QByteArray data = socket->readAll(); - QMultiMap queryParams = parseQueryParams(&data); - socket->disconnectFromHost(); - q->close(); - emit q->verificationReceived(queryParams); + if(!handlingRedirect) { + socket->disconnectFromHost(); + q->close(); + emit q->verificationReceived(queryParams); + } } QMultiMap KQOAuthAuthReplyServerPrivate::parseQueryParams(QByteArray *data) { diff --git a/oauth/kqoauthauthreplyserver_p.h b/oauth/kqoauthauthreplyserver_p.h index 4d94ec6..1ebe524 100644 --- a/oauth/kqoauthauthreplyserver_p.h +++ b/oauth/kqoauthauthreplyserver_p.h @@ -42,6 +42,8 @@ public: Q_DECLARE_PUBLIC(KQOAuthAuthReplyServer); QTcpSocket *socket; QString localFile; +private: + bool handlingRedirect; }; #endif // KQOAUTHAUTHREPLYSERVER_P_H diff --git a/oauth/kqoauthmanager.cpp b/oauth/kqoauthmanager.cpp index 38f8c73..5f49348 100644 --- a/oauth/kqoauthmanager.cpp +++ b/oauth/kqoauthmanager.cpp @@ -159,6 +159,7 @@ void KQOAuthManager::executeRequest(KQOAuthRequest *request) { } if (!request->requestEndpoint().isValid()) { + qDebug() << request->requestEndpoint(); qWarning() << "Request endpoint URL is not valid. Cannot proceed."; d->error = KQOAuthManager::RequestEndpointError; return; @@ -219,6 +220,7 @@ void KQOAuthManager::executeRequest(KQOAuthRequest *request) { // Submit the request including the params. QNetworkReply *reply = d->networkManager->get(networkRequest); + reply->ignoreSslErrors(); connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(slotError(QNetworkReply::NetworkError))); @@ -236,6 +238,7 @@ void KQOAuthManager::executeRequest(KQOAuthRequest *request) { } else { reply = d->networkManager->post(networkRequest, request->rawData()); } + reply->ignoreSslErrors(); connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(slotError(QNetworkReply::NetworkError))); @@ -256,6 +259,7 @@ void KQOAuthManager::executeAuthorizedRequest(KQOAuthRequest *request, int id) { } if (!request->requestEndpoint().isValid()) { + qDebug() << request->requestEndpoint(); qWarning() << "Request endpoint URL is not valid. Cannot proceed."; d->error = KQOAuthManager::RequestEndpointError; return; @@ -300,6 +304,7 @@ void KQOAuthManager::executeAuthorizedRequest(KQOAuthRequest *request, int id) { disconnect(d->networkManager, SIGNAL(finished(QNetworkReply *)), this, SLOT(onRequestReplyReceived(QNetworkReply *))); + connect(d->networkManager, SIGNAL(sslErrors(QNetworkReply*, const QList&)), this, SLOT(onSslError(QNetworkReply* reply, const QList &errors))); connect(d->networkManager, SIGNAL(finished(QNetworkReply *)), this, SLOT(onAuthorizedRequestReplyReceived(QNetworkReply*)), Qt::UniqueConnection); @@ -315,6 +320,7 @@ void KQOAuthManager::executeAuthorizedRequest(KQOAuthRequest *request, int id) { // Submit the request including the params. QNetworkReply *reply = d->networkManager->get(networkRequest); + reply->ignoreSslErrors(); connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(slotError(QNetworkReply::NetworkError))); @@ -413,7 +419,7 @@ void KQOAuthManager::getOauth2UserAuthorization(QUrl authorizationEndpoint, QStr d->setupCallbackServer(); connect(d->callbackServer, SIGNAL(verificationReceived(QMultiMap)), - this, SLOT( onVerificationReceived(QMultiMap))); + this, SLOT( onOauth2VerificationReceived(QMultiMap))); QString serverString = "http://localhost:"; serverString.append(QString::number(d->callbackServer->serverPort())); @@ -661,6 +667,25 @@ void KQOAuthManager::onVerificationReceived(QMultiMap response emit authorizationReceived(token, verifier); } +void KQOAuthManager::onOauth2VerificationReceived(QMultiMap response) { + Q_D(KQOAuthManager); + + foreach(QString key, response.keys()){ + qDebug() << key; + } + + QString token = response.value("access_token"); + if (token.isEmpty()) { + d->error = KQOAuthManager::RequestUnauthorized; + } + + if (d->error == KQOAuthManager::NoError) { + d->isVerified = true; + } + + emit authorizationReceived(token, NULL); +} + void KQOAuthManager::slotError(QNetworkReply::NetworkError error) { Q_UNUSED(error) Q_D(KQOAuthManager); @@ -675,3 +700,8 @@ void KQOAuthManager::slotError(QNetworkReply::NetworkError error) { reply->deleteLater(); } +void KQOAuthManager::onSslError(QNetworkReply *reply, const QList &errors) { + Q_UNUSED(errors); + reply->ignoreSslErrors(); +} + diff --git a/oauth/kqoauthmanager.h b/oauth/kqoauthmanager.h index 2bf88df..514af77 100644 --- a/oauth/kqoauthmanager.h +++ b/oauth/kqoauthmanager.h @@ -180,7 +180,9 @@ private Q_SLOTS: void onRequestReplyReceived( QNetworkReply *reply ); void onAuthorizedRequestReplyReceived( QNetworkReply *reply ); void onVerificationReceived(QMultiMap response); + void onOauth2VerificationReceived(QMultiMap response); void slotError(QNetworkReply::NetworkError error); + void onSslError(QNetworkReply *reply, const QList &errors); private: KQOAuthManagerPrivate *d_ptr; diff --git a/oauth/kqoauthrequest.cpp b/oauth/kqoauthrequest.cpp index 3cd5bdb..28f657a 100644 --- a/oauth/kqoauthrequest.cpp +++ b/oauth/kqoauthrequest.cpp @@ -575,9 +575,7 @@ bool KQOAuthRequest::validateXAuthRequest() const { bool KQOAuthRequest::validateOauth2Request() const { Q_D(const KQOAuthRequest); - if (d->oauthRequestEndpoint.isEmpty() - || d->oauthConsumerKey.isEmpty() - || d->oauthConsumerSecretKey.isEmpty()) + if (d->oauthRequestEndpoint.isEmpty() || d->oauthToken.isEmpty()) { return false; }