Make inoreader network stuff clearer.

This commit is contained in:
Martin Rotter 2017-09-25 13:50:31 +02:00
parent d045e7ea72
commit 9b1b3c37d3
7 changed files with 37 additions and 24 deletions

View File

@ -6,8 +6,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>645</width>
<height>310</height>
<width>577</width>
<height>412</height>
</rect>
</property>
<property name="windowTitle">

View File

@ -66,7 +66,7 @@ OAuth2Service::OAuth2Service(QString authUrl, QString tokenUrl, QString clientId
connect(this, &OAuth2Service::authCodeObtained, this, &OAuth2Service::retrieveAccessToken);
}
void OAuth2Service::setBearerHeader(QNetworkRequest& req) {
void OAuth2Service::attachBearerHeader(QNetworkRequest& req) {
req.setRawHeader(QString("Authorization").toLocal8Bit(), QString("Bearer %1").arg(m_accessToken).toLocal8Bit());
}
@ -74,7 +74,7 @@ void OAuth2Service::setOAuthTokenGrantType(QString oAuthTokenGrantType) {
m_tokenGrantType = oAuthTokenGrantType;
}
QString OAuth2Service::oAuthTokenGrantType() {
QString OAuth2Service::grant_type() {
return m_tokenGrantType;
}
@ -120,6 +120,10 @@ void OAuth2Service::refreshAccessToken(QString refresh_token) {
m_networkManager.post(networkRequest, content.toUtf8());
}
void OAuth2Service::cleanTokens() {
m_refreshToken = m_accessToken = QString();
}
void OAuth2Service::tokenRequestFinished(QNetworkReply* networkReply) {
QJsonDocument jsonDocument = QJsonDocument::fromJson(networkReply->readAll());
QJsonObject rootObject = jsonDocument.object();
@ -127,9 +131,12 @@ void OAuth2Service::tokenRequestFinished(QNetworkReply* networkReply) {
qDebug() << "Token response:";
qDebug() << jsonDocument.toJson();
if(rootObject.keys().contains("error")) {
if (rootObject.keys().contains("error")) {
QString error = rootObject.value("error").toString();
QString error_description = rootObject.value("error_description").toString();
cleanTokens();
emit tokenRetrieveError(error, error_description);
}
else {
@ -138,7 +145,7 @@ void OAuth2Service::tokenRequestFinished(QNetworkReply* networkReply) {
// TODO: Start timer to refresh tokens.
emit accessTokenReceived(m_accessToken, m_refreshToken, rootObject.value("expires_in").toInt());
emit tokensReceived(m_accessToken, m_refreshToken, rootObject.value("expires_in").toInt());
}
networkReply->deleteLater();
@ -152,6 +159,14 @@ void OAuth2Service::setRefreshToken(const QString& refresh_token) {
m_refreshToken = refresh_token;
}
void OAuth2Service::login() {
// TODO: ted se rovnou vola autorizace (prihlasovaci dialog)
// ale vylepsit a v pripade ze je zadan refresh token,,
// tak nejdříve zkusit obnovit? a začátek procesu
// volat jen když je to fakt potřeba.
retrieveAuthCode();
}
void OAuth2Service::retrieveAuthCode() {
QString auth_url = m_authUrl + QString("?client_id=%1&scope=%2&"
"redirect_uri=%3&response_type=code&state=abcdef").arg(m_clientId,
@ -160,7 +175,10 @@ void OAuth2Service::retrieveAuthCode() {
OAuthLogin login_page(qApp->mainFormWidget());
connect(&login_page, &OAuthLogin::authGranted, this, &OAuth2Service::authCodeObtained);
connect(&login_page, &OAuthLogin::authRejected, this, &OAuth2Service::authFailed);
connect(&login_page, &OAuthLogin::authRejected, [this]() {
cleanTokens();
emit authFailed();
});
login_page.login(auth_url, m_redirectUri);
}

View File

@ -52,10 +52,10 @@ class OAuth2Service : public QObject {
explicit OAuth2Service(QString authUrl, QString tokenUrl, QString clientId,
QString clientSecret, QString scope, QObject* parent = 0);
void setBearerHeader(QNetworkRequest& req);
void attachBearerHeader(QNetworkRequest& req);
void setOAuthTokenGrantType(QString oAuthTokenGrantType);
QString oAuthTokenGrantType();
void setOAuthTokenGrantType(QString grant_type);
QString grant_type();
QString accessToken() const;
void setAccessToken(const QString& access_token);
@ -64,7 +64,7 @@ class OAuth2Service : public QObject {
void setRefreshToken(const QString& refresh_token);
signals:
void accessTokenReceived(QString access_token, QString refresh_token, int expires_in);
void tokensReceived(QString access_token, QString refresh_token, int expires_in);
void tokenRetrieveError(QString error, QString error_description);
// User failed to authenticate or rejected it.
@ -74,11 +74,13 @@ class OAuth2Service : public QObject {
void authCodeObtained(QString auth_code);
public slots:
void login();
void retrieveAuthCode();
void retrieveAccessToken(QString auth_code);
void refreshAccessToken(QString refresh_token = QString());
private slots:
void cleanTokens();
void tokenRequestFinished(QNetworkReply* networkReply);
private:

View File

@ -71,7 +71,7 @@ void FormEditInoreaderAccount::testSetup() {
tr("Access granted successfully."));
}
else {
m_network->logIn();
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."));

View File

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

View File

@ -61,14 +61,8 @@ void InoreaderNetworkFactory::setBatchSize(int batch_size) {
m_batchSize = batch_size;
}
void InoreaderNetworkFactory::logIn() {
m_oauth2->retrieveAuthCode();
}
void InoreaderNetworkFactory::logInIfNeeded() {
if (!isLoggedIn()) {
logIn();
}
void InoreaderNetworkFactory::login() {
m_oauth2->login();
}
void InoreaderNetworkFactory::initializeOauth() {
@ -91,7 +85,7 @@ RootItem* InoreaderNetworkFactory::feedsCategories(bool obtain_icons) {
QNetworkRequest req(QUrl(INOREADER_API_LIST_LABELS));
m_oauth2->setBearerHeader(req);
m_oauth2->attachBearerHeader(req);
QNetworkReply* reply = SilentNetworkAccessManager::instance()->get(req);
QEventLoop loop;

View File

@ -49,8 +49,7 @@ class InoreaderNetworkFactory : public QObject {
RootItem* feedsCategories(bool obtain_icons);
public slots:
void logIn();
void logInIfNeeded();
void login();
signals:
void accessGranted();