From f67e4c2104f6b307953893a4a27417723af87c7a Mon Sep 17 00:00:00 2001 From: Morgan McMillian Date: Fri, 23 Sep 2016 13:43:12 -0700 Subject: [PATCH] work on auth handling, still not right yet though --- buildnum | 2 +- src/Pnut.cpp | 35 ++++++++++++++++++++++++++++++----- src/Pnut.h | 2 ++ 3 files changed, 33 insertions(+), 6 deletions(-) diff --git a/buildnum b/buildnum index 19c7bdb..978b4e8 100644 --- a/buildnum +++ b/buildnum @@ -1 +1 @@ -16 \ No newline at end of file +26 \ No newline at end of file diff --git a/src/Pnut.cpp b/src/Pnut.cpp index 995e35c..b72cd5f 100644 --- a/src/Pnut.cpp +++ b/src/Pnut.cpp @@ -30,6 +30,7 @@ Pnut::Pnut() req_id = 0; before_id = 0; since_id = 0; + authInProgress = false; m_appSettings = new QSettings("Morgan McMillian", "Goober"); oauthRequest = new KQOAuthRequest; @@ -40,6 +41,11 @@ Pnut::Pnut() Q_ASSERT(auth_req); + bool net_req = connect(oauthManager, SIGNAL(requestReady(QByteArray)), + this, SLOT(onRequestReady(QByteArray))); + + Q_ASSERT(net_req); + bool conn_req = connect(oauthManager, SIGNAL(authorizedRequestReady(QByteArray,int)), this, SLOT(onAuthorizedRequestReady(QByteArray,int))); @@ -52,7 +58,7 @@ Pnut::~Pnut() { delete oauthRequest; delete oauthManager; - qDebug() << "Pnut destroyed,, I am death."; + qDebug() << "Pnut destroyed, I am death."; } int Pnut::sinceId() const @@ -77,10 +83,14 @@ void Pnut::setBeforeId(const int& id) void Pnut::authorize() { - QUrl url(PNUT_AUTH_URL); - KQOAuthParameters parameters; - parameters.insert("scope", "basic stream write_post follow update_profile presence"); - oauthManager->getOauth2UserAuthorization(url, PNUT_CLIENT_ID, parameters); + if (authInProgress != true) + { + authInProgress = true; + QUrl url(PNUT_AUTH_URL); + KQOAuthParameters parameters; + parameters.insert("scope", "basic stream write_post follow update_profile presence"); + oauthManager->getOauth2UserAuthorization(url, PNUT_CLIENT_ID, parameters); + } } void Pnut::getRequest(QUrl url, KQOAuthParameters parameters, int id) @@ -123,6 +133,21 @@ void Pnut::onAuthorizationReceived(QString token, QString verifier) qDebug() << "- authorization error -"; qDebug() << oauthManager->lastError(); } + authInProgress = false; +} + +void Pnut::onRequestReady(QByteArray data) +{ + qDebug() << "- onRequestReady -"; + switch (oauthManager->lastError()) + { + case KQOAuthManager::NetworkError: + qDebug() << "- NetworkError - assume re-authorization"; + m_appSettings->remove("access_token"); + m_appSettings->remove("username"); + authorize(); + break; + } } void Pnut::onAuthorizedRequestReady(QByteArray data, int id) diff --git a/src/Pnut.h b/src/Pnut.h index b0efc2f..3f315f9 100644 --- a/src/Pnut.h +++ b/src/Pnut.h @@ -73,6 +73,7 @@ public: Q_INVOKABLE void getUserInfo(); public slots: + void onRequestReady(QByteArray data); void onAuthorizedRequestReady(QByteArray data, int id); void onAuthorizationReceived(QString token, QString verifier); @@ -94,6 +95,7 @@ private: int req_id; QMap req_map; QMap rtype_map; + bool authInProgress; void getRequest(QUrl url, KQOAuthParameters parameters, int id); void postRequest(QUrl url, KQOAuthParameters parameters, int id, QByteArray data);