Oauth2 updates

This commit is contained in:
Kyle Fowler 2012-04-11 11:01:21 -07:00
parent 068b80b3f3
commit 51b8c15147
5 changed files with 67 additions and 13 deletions

View file

@ -20,6 +20,7 @@
#include <QTcpSocket>
#include <QStringList>
#include <QUrl>
#include <QFile>
#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("<HTML><h1>Account authorized, go ahead back to the tumblr app and start your experience!</h1></HTML>");
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<QString, QString> queryParams = parseQueryParams(&data);
if(queryParams.size() == 0 && !handlingRedirect) { //assume theres a hash and do the redirect hack
handlingRedirect = true;
content.append("<HTML><head><script type=\"text/javascript\">var str='http://'+window.location.host + '?' + window.location.hash.substring(1); window.location=str;</script></head><h1>Account authorized, go ahead back to the tumblr app and start your experience!</h1></HTML>");
} else {
handlingRedirect = false;
//TODO then send down the local file if there is one
content.append("<HTML><h1>Account linked, go ahead back to the app and check the status!</h1></HTML>");
}
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<QString, QString> queryParams = parseQueryParams(&data);
socket->disconnectFromHost();
q->close();
emit q->verificationReceived(queryParams);
if(!handlingRedirect) {
socket->disconnectFromHost();
q->close();
emit q->verificationReceived(queryParams);
}
}
QMultiMap<QString, QString> KQOAuthAuthReplyServerPrivate::parseQueryParams(QByteArray *data) {

View file

@ -42,6 +42,8 @@ public:
Q_DECLARE_PUBLIC(KQOAuthAuthReplyServer);
QTcpSocket *socket;
QString localFile;
private:
bool handlingRedirect;
};
#endif // KQOAUTHAUTHREPLYSERVER_P_H

View file

@ -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<QSslError>&)), this, SLOT(onSslError(QNetworkReply* reply, const QList<QSslError> &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<QString, QString>)),
this, SLOT( onVerificationReceived(QMultiMap<QString, QString>)));
this, SLOT( onOauth2VerificationReceived(QMultiMap<QString, QString>)));
QString serverString = "http://localhost:";
serverString.append(QString::number(d->callbackServer->serverPort()));
@ -661,6 +667,25 @@ void KQOAuthManager::onVerificationReceived(QMultiMap<QString, QString> response
emit authorizationReceived(token, verifier);
}
void KQOAuthManager::onOauth2VerificationReceived(QMultiMap<QString, QString> 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<QSslError> &errors) {
Q_UNUSED(errors);
reply->ignoreSslErrors();
}

View file

@ -180,7 +180,9 @@ private Q_SLOTS:
void onRequestReplyReceived( QNetworkReply *reply );
void onAuthorizedRequestReplyReceived( QNetworkReply *reply );
void onVerificationReceived(QMultiMap<QString, QString> response);
void onOauth2VerificationReceived(QMultiMap<QString, QString> response);
void slotError(QNetworkReply::NetworkError error);
void onSslError(QNetworkReply *reply, const QList<QSslError> &errors);
private:
KQOAuthManagerPrivate *d_ptr;

View file

@ -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;
}