Store app id/key + redirect URI.

This commit is contained in:
Martin Rotter 2017-10-02 14:29:26 +02:00
parent 9676c3fb9a
commit 2c7a524cb3
7 changed files with 63 additions and 24 deletions

View File

@ -1573,14 +1573,19 @@ QList<ServiceRoot*> DatabaseQueries::getInoreaderAccounts(QSqlDatabase db, bool*
return roots;
}
bool DatabaseQueries::overwriteInoreaderAccount(QSqlDatabase db, const QString& username,
bool DatabaseQueries::overwriteInoreaderAccount(QSqlDatabase db, const QString& username, const QString& app_id,
const QString& app_key, const QString& redirect_url,
const QString& refresh_token, int batch_size, int account_id) {
QSqlQuery query(db);
query.prepare("UPDATE InoreaderAccounts "
"SET username = :username, refresh_token = :refresh_token , msg_limit = :msg_limit "
"SET username = :username, app_id = :app_id, app_key = :app_key, "
"redirect_url = :redirect_url, refresh_token = :refresh_token , msg_limit = :msg_limit "
"WHERE id = :id;");
query.bindValue(QSL(":username"), username);
query.bindValue(QSL(":app_id"), app_id);
query.bindValue(QSL(":app_key"), app_key);
query.bindValue(QSL(":redirect_url"), redirect_url);
query.bindValue(QSL(":refresh_token"), refresh_token);
query.bindValue(QSL(":id"), account_id);
query.bindValue(QSL(":msg_limit"), batch_size <= 0 ? INOREADER_DEFAULT_BATCH_SIZE : batch_size);

View File

@ -83,8 +83,9 @@ class DatabaseQueries {
static Assignment getInoreaderFeeds(QSqlDatabase db, int account_id, bool* ok = nullptr);
static bool storeNewInoreaderTokens(QSqlDatabase db, const QString& refresh_token, int account_id);
static QList<ServiceRoot*> getInoreaderAccounts(QSqlDatabase db, bool* ok = nullptr);
static bool overwriteInoreaderAccount(QSqlDatabase db, const QString& username,
const QString& refresh_token, int batch_size, int account_id);
static bool overwriteInoreaderAccount(QSqlDatabase db, const QString& username, const QString& app_id,
const QString& app_key, const QString& redirect_url, const QString& refresh_token,
int batch_size, int account_id);
static bool createInoreaderAccount(QSqlDatabase db, int id_to_assign, const QString& username,
const QString& refresh_token, int batch_size);
#endif

View File

@ -162,6 +162,22 @@ void OAuth2Service::tokenRequestFinished(QNetworkReply* networkReply) {
networkReply->deleteLater();
}
QString OAuth2Service::accessToken() const {
return m_accessToken;
}
void OAuth2Service::setAccessToken(const QString& access_token) {
m_accessToken = access_token;
}
QDateTime OAuth2Service::tokensExpireIn() const {
return m_tokensExpireIn;
}
void OAuth2Service::setTokensExpireIn(const QDateTime& tokens_expire_in) {
m_tokensExpireIn = tokens_expire_in;
}
QString OAuth2Service::clientSecret() const {
return m_clientSecret;
}

View File

@ -71,6 +71,12 @@ class OAuth2Service : public QObject {
QString clientSecret() const;
void setClientSecret(const QString& client_secret);
QDateTime tokensExpireIn() const;
void setTokensExpireIn(const QDateTime& tokens_expire_in);
QString accessToken() const;
void setAccessToken(const QString& access_token);
signals:
void tokensReceived(QString access_token, QString refresh_token, int expires_in);
void tokensRetrieveError(QString error, QString error_description);

View File

@ -26,7 +26,8 @@
#include "services/inoreader/inoreaderserviceroot.h"
FormEditInoreaderAccount::FormEditInoreaderAccount(QWidget* parent) : QDialog(parent),
m_network(nullptr), m_editableRoot(nullptr) {
m_oauth(new OAuth2Service(INOREADER_OAUTH_AUTH_URL, INOREADER_OAUTH_TOKEN_URL,
INOREADER_OAUTH_CLI_ID, INOREADER_OAUTH_CLI_KEY, INOREADER_OAUTH_SCOPE)), m_editableRoot(nullptr) {
m_ui.setupUi(this);
GuiUtilities::setLabelAsNotice(*m_ui.m_lblAuthInfo, true);
@ -58,12 +59,17 @@ FormEditInoreaderAccount::FormEditInoreaderAccount(QWidget* parent) : QDialog(pa
m_ui.m_spinLimitMessages->setMaximum(INOREADER_MAX_BATCH_SIZE);
checkUsername(m_ui.m_txtUsername->lineEdit()->text());
hookNetwork();
}
FormEditInoreaderAccount::~FormEditInoreaderAccount() {}
void FormEditInoreaderAccount::testSetup() {
if (m_network->oauth()->login()) {
m_oauth->setClientId(m_ui.m_txtAppId->lineEdit()->text());
m_oauth->setClientSecret(m_ui.m_txtAppKey->lineEdit()->text());
m_oauth->setRedirectUri(m_ui.m_txtRedirectUrl->lineEdit()->text());
if (m_oauth->login()) {
m_ui.m_lblTestResult->setStatus(WidgetWithStatus::StatusType::Ok,
tr("You are already logged in."),
tr("Access granted."));
@ -76,13 +82,19 @@ void FormEditInoreaderAccount::onClickedOk() {
if (m_editableRoot == nullptr) {
// We want to confirm newly created account.
// So save new account into DB, setup its properties.
m_editableRoot = new InoreaderServiceRoot(m_network);
m_editableRoot = new InoreaderServiceRoot(nullptr);
editing_account = false;
}
// We copy credentials from testing OAuth to live OAuth.
m_editableRoot->network()->oauth()->setAccessToken(m_oauth->accessToken());
m_editableRoot->network()->oauth()->setRefreshToken(m_oauth->refreshToken());
m_editableRoot->network()->oauth()->setTokensExpireIn(m_oauth->tokensExpireIn());
m_editableRoot->network()->oauth()->setClientId(m_ui.m_txtAppId->lineEdit()->text());
m_editableRoot->network()->oauth()->setClientSecret(m_ui.m_txtAppKey->lineEdit()->text());
m_editableRoot->network()->oauth()->setRedirectUri(m_ui.m_txtRedirectUrl->lineEdit()->text());
m_editableRoot->network()->setUsername(m_ui.m_txtUsername->lineEdit()->text());
m_editableRoot->network()->setBatchSize(m_ui.m_spinLimitMessages->value());
m_editableRoot->saveAccountDataToDatabase();
@ -128,28 +140,19 @@ void FormEditInoreaderAccount::onAuthGranted() {
}
void FormEditInoreaderAccount::hookNetwork() {
connect(m_network->oauth(), &OAuth2Service::tokensReceived, this, &FormEditInoreaderAccount::onAuthGranted);
connect(m_network->oauth(), &OAuth2Service::tokensRetrieveError, this, &FormEditInoreaderAccount::onAuthError);
connect(m_network->oauth(), &OAuth2Service::authFailed, this, &FormEditInoreaderAccount::onAuthFailed);
}
void FormEditInoreaderAccount::unhookNetwork() {
disconnect(m_network->oauth(), &OAuth2Service::tokensReceived, this, &FormEditInoreaderAccount::onAuthGranted);
disconnect(m_network->oauth(), &OAuth2Service::tokensRetrieveError, this, &FormEditInoreaderAccount::onAuthError);
disconnect(m_network->oauth(), &OAuth2Service::authFailed, this, &FormEditInoreaderAccount::onAuthFailed);
connect(m_oauth, &OAuth2Service::tokensReceived, this, &FormEditInoreaderAccount::onAuthGranted);
connect(m_oauth, &OAuth2Service::tokensRetrieveError, this, &FormEditInoreaderAccount::onAuthError);
connect(m_oauth, &OAuth2Service::authFailed, this, &FormEditInoreaderAccount::onAuthFailed);
}
InoreaderServiceRoot* FormEditInoreaderAccount::execForCreate() {
setWindowTitle(tr("Add new Inoreader account"));
m_network = new InoreaderNetworkFactory(this);
m_ui.m_txtAppId->lineEdit()->setText(INOREADER_OAUTH_CLI_ID);
m_ui.m_txtAppKey->lineEdit()->setText(INOREADER_OAUTH_CLI_KEY);
m_ui.m_txtRedirectUrl->lineEdit()->setText(INOREADER_OAUTH_CLI_REDIRECT);
hookNetwork();
exec();
unhookNetwork();
return m_editableRoot;
}
@ -158,16 +161,22 @@ void FormEditInoreaderAccount::execForEdit(InoreaderServiceRoot* existing_root)
setWindowTitle(tr("Edit existing Inoreader account"));
m_editableRoot = existing_root;
// We copy settings from existing OAuth to our testing OAuth.
m_oauth->setClientId(existing_root->network()->oauth()->clientId());
m_oauth->setClientSecret(existing_root->network()->oauth()->clientSecret());
m_oauth->setRedirectUri(existing_root->network()->oauth()->redirectUri());
m_oauth->setRefreshToken(existing_root->network()->oauth()->refreshToken());
m_oauth->setAccessToken(existing_root->network()->oauth()->accessToken());
m_oauth->setTokensExpireIn(existing_root->network()->oauth()->tokensExpireIn());
m_ui.m_txtAppId->lineEdit()->setText(existing_root->network()->oauth()->clientId());
m_ui.m_txtAppKey->lineEdit()->setText(existing_root->network()->oauth()->clientSecret());
m_ui.m_txtRedirectUrl->lineEdit()->setText(existing_root->network()->oauth()->redirectUri());
m_ui.m_txtUsername->lineEdit()->setText(existing_root->network()->userName());
m_ui.m_spinLimitMessages->setValue(existing_root->network()->batchSize());
m_network = existing_root->network();
hookNetwork();
exec();
unhookNetwork();
}
void FormEditInoreaderAccount::checkOAuthValue(const QString& value) {

View File

@ -54,10 +54,9 @@ class FormEditInoreaderAccount : public QDialog {
private:
void hookNetwork();
void unhookNetwork();
Ui::FormEditInoreaderAccount m_ui;
InoreaderNetworkFactory* m_network;
OAuth2Service* m_oauth;
InoreaderServiceRoot* m_editableRoot;
};

View File

@ -67,6 +67,9 @@ void InoreaderServiceRoot::saveAccountDataToDatabase() {
if (accountId() != NO_PARENT_CATEGORY) {
if (DatabaseQueries::overwriteInoreaderAccount(database, m_network->userName(),
m_network->oauth()->clientId(),
m_network->oauth()->clientSecret(),
m_network->oauth()->redirectUri(),
m_network->oauth()->refreshToken(),
m_network->batchSize(),
accountId())) {