From 44b46b85420bc246909eda0446bbebfd2bc37929 Mon Sep 17 00:00:00 2001 From: Martin Rotter Date: Tue, 26 Sep 2017 11:43:33 +0200 Subject: [PATCH] Correct some paths of oauth login flow. --- src/network-web/oauth2service.cpp | 14 +++++++++++--- src/network-web/oauth2service.h | 4 +++- .../inoreader/gui/formeditinoreaderaccount.cpp | 14 ++++++++++---- src/services/inoreader/inoreaderserviceroot.cpp | 2 +- .../inoreader/network/inoreadernetworkfactory.cpp | 8 -------- .../inoreader/network/inoreadernetworkfactory.h | 5 ----- 6 files changed, 25 insertions(+), 22 deletions(-) diff --git a/src/network-web/oauth2service.cpp b/src/network-web/oauth2service.cpp index 9790f7814..9c77b48e5 100755 --- a/src/network-web/oauth2service.cpp +++ b/src/network-web/oauth2service.cpp @@ -164,16 +164,24 @@ void OAuth2Service::setRefreshToken(const QString& refresh_token) { m_refreshToken = refresh_token; } -void OAuth2Service::login() { +bool OAuth2Service::login() { + bool did_token_expire = m_tokensExpireIn.isNull() || m_tokensExpireIn < QDateTime::currentDateTime(); + bool does_token_exist = !m_refreshToken.isEmpty(); + // We refresh current tokens only if: // 1. We have some existing refresh token. // AND // 2. We do not know its expiration date or it passed. - if (!m_refreshToken.isEmpty() && (m_tokensExpireIn.isNull() || m_tokensExpireIn < QDateTime::currentDateTime())) { + if (does_token_exist && did_token_expire) { refreshAccessToken(); + return false; + } + else if (!does_token_exist) { + retrieveAuthCode(); + return false; } else { - retrieveAuthCode(); + return true; } } diff --git a/src/network-web/oauth2service.h b/src/network-web/oauth2service.h index fa7f63507..f23861198 100755 --- a/src/network-web/oauth2service.h +++ b/src/network-web/oauth2service.h @@ -81,7 +81,9 @@ class OAuth2Service : public QObject { // Performs login if needed. If some refresh token is set, then // the initial "auth" step is skipped and attempt to refresh // access token is made. - void login(); + // Returns true, if user is already logged in (final state). + // Returns false, if user is NOT logged in (asynchronous flow). + bool login(); private slots: void cleanTokens(); diff --git a/src/services/inoreader/gui/formeditinoreaderaccount.cpp b/src/services/inoreader/gui/formeditinoreaderaccount.cpp index 637ca833c..2a72561b0 100755 --- a/src/services/inoreader/gui/formeditinoreaderaccount.cpp +++ b/src/services/inoreader/gui/formeditinoreaderaccount.cpp @@ -61,10 +61,16 @@ FormEditInoreaderAccount::FormEditInoreaderAccount(QWidget* parent) : QDialog(pa FormEditInoreaderAccount::~FormEditInoreaderAccount() {} void FormEditInoreaderAccount::testSetup() { - m_network->login(); - m_ui.m_lblTestResult->setStatus(WidgetWithStatus::StatusType::Progress, - tr("Requested access approval. Respond to it, please."), - tr("Access approval was requested via OAuth 2.0 protocol.")); + if (m_network->oauth()->login()) { + m_ui.m_lblTestResult->setStatus(WidgetWithStatus::StatusType::Ok, + tr("You are already logged in."), + tr("Access granted.")); + } + else { + m_ui.m_lblTestResult->setStatus(WidgetWithStatus::StatusType::Progress, + tr("Requested access approval. Respond to it, please."), + tr("Access approval was requested via OAuth 2.0 protocol.")); + } } void FormEditInoreaderAccount::onClickedOk() { diff --git a/src/services/inoreader/inoreaderserviceroot.cpp b/src/services/inoreader/inoreaderserviceroot.cpp index 8c94f12d0..f97274af7 100755 --- a/src/services/inoreader/inoreaderserviceroot.cpp +++ b/src/services/inoreader/inoreaderserviceroot.cpp @@ -114,7 +114,7 @@ void InoreaderServiceRoot::start(bool freshly_activated) { //loadCacheFromFile(accountId()); - m_network->login(); + m_network->oauth()->login(); } void InoreaderServiceRoot::stop() {} diff --git a/src/services/inoreader/network/inoreadernetworkfactory.cpp b/src/services/inoreader/network/inoreadernetworkfactory.cpp index 0d313b529..b804d11c8 100755 --- a/src/services/inoreader/network/inoreadernetworkfactory.cpp +++ b/src/services/inoreader/network/inoreadernetworkfactory.cpp @@ -45,10 +45,6 @@ OAuth2Service* InoreaderNetworkFactory::oauth() const { return m_oauth2; } -bool InoreaderNetworkFactory::isLoggedIn() const { - return !m_oauth2->refreshToken().isEmpty(); -} - QString InoreaderNetworkFactory::userName() const { return m_username; } @@ -61,10 +57,6 @@ void InoreaderNetworkFactory::setBatchSize(int batch_size) { m_batchSize = batch_size; } -void InoreaderNetworkFactory::login() { - m_oauth2->login(); -} - void InoreaderNetworkFactory::initializeOauth() { connect(m_oauth2, &OAuth2Service::tokensRetrieveError, [](QString error, QString error_description) { Q_UNUSED(error) diff --git a/src/services/inoreader/network/inoreadernetworkfactory.h b/src/services/inoreader/network/inoreadernetworkfactory.h index 4d9d44bfe..0cb511fbd 100755 --- a/src/services/inoreader/network/inoreadernetworkfactory.h +++ b/src/services/inoreader/network/inoreadernetworkfactory.h @@ -34,8 +34,6 @@ class InoreaderNetworkFactory : public QObject { OAuth2Service* oauth() const; - bool isLoggedIn() const; - QString userName() const; void setUsername(const QString& username); @@ -48,9 +46,6 @@ class InoreaderNetworkFactory : public QObject { // Returned items do not have primary IDs assigned. RootItem* feedsCategories(bool obtain_icons); - public slots: - void login(); - private: void initializeOauth();