Correct some paths of oauth login flow.

This commit is contained in:
Martin Rotter 2017-09-26 11:43:33 +02:00
parent 947720a4af
commit 44b46b8542
6 changed files with 25 additions and 22 deletions

View File

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

View File

@ -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();

View File

@ -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() {

View File

@ -114,7 +114,7 @@ void InoreaderServiceRoot::start(bool freshly_activated) {
//loadCacheFromFile(accountId());
m_network->login();
m_network->oauth()->login();
}
void InoreaderServiceRoot::stop() {}

View File

@ -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)

View File

@ -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();