More fixes for OAuth.

This commit is contained in:
Martin Rotter 2020-07-24 08:52:41 +02:00
parent 0d77d5481f
commit 9d6b40e476
6 changed files with 41 additions and 36 deletions

View File

@ -1733,8 +1733,8 @@ QList<ServiceRoot*> DatabaseQueries::getGmailAccounts(const QSqlDatabase& db, bo
root->network()->setUsername(query.value(1).toString());
root->network()->oauth()->setClientId(query.value(2).toString());
root->network()->oauth()->setClientSecret(query.value(3).toString());
root->network()->oauth()->setRedirectUrl(query.value(4).toString());
root->network()->oauth()->setRefreshToken(query.value(5).toString());
root->network()->oauth()->setRedirectUrl(query.value(4).toString());
root->network()->setBatchSize(query.value(6).toInt());
root->updateTitle();
roots.append(root);
@ -1804,8 +1804,8 @@ QList<ServiceRoot*> DatabaseQueries::getInoreaderAccounts(const QSqlDatabase& db
root->network()->setUsername(query.value(1).toString());
root->network()->oauth()->setClientId(query.value(2).toString());
root->network()->oauth()->setClientSecret(query.value(3).toString());
root->network()->oauth()->setRedirectUrl(query.value(4).toString());
root->network()->oauth()->setRefreshToken(query.value(5).toString());
root->network()->oauth()->setRedirectUrl(query.value(4).toString());
root->network()->setBatchSize(query.value(6).toInt());
root->updateTitle();
roots.append(root);

View File

@ -257,6 +257,11 @@ void OAuth2Service::setRefreshToken(const QString& refresh_token) {
}
bool OAuth2Service::login() {
if (!m_redirectionHandler->isListening()) {
qCritical("Cannot log-in because OAuth redirection handler is not listening.");
return false;
}
bool did_token_expire = tokensExpireIn().isNull() || tokensExpireIn() < QDateTime::currentDateTime().addSecs(-120);
bool does_token_exist = !refreshToken().isEmpty();
@ -308,7 +313,7 @@ void OAuth2Service::retrieveAuthCode() {
m_id);
// We run login URL in external browser, response is caught by light HTTP server.
if (qApp->web()->openUrlInExternalBrowser(auth_url)) {
if (!qApp->web()->openUrlInExternalBrowser(auth_url)) {
QInputDialog::getText(qApp->mainFormWidget(),
tr("Navigate to website"),
tr("To login, you need to navigate to this website:"),

View File

@ -24,9 +24,14 @@ OAuthHttpHandler::~OAuthHttpHandler() {
}
}
bool OAuthHttpHandler::isListening() const {
return m_httpServer.isListening();
}
void OAuthHttpHandler::setListenAddressPort(const QString& full_uri) {
QUrl url = QUrl::fromUserInput(full_uri);
QHostAddress listen_address;
quint16 listen_port = quint16(url.port(80));
if (url.host() == QL1S("localhost")) {
listen_address = QHostAddress(QHostAddress::SpecialAddress::LocalHost);
@ -35,12 +40,12 @@ void OAuthHttpHandler::setListenAddressPort(const QString& full_uri) {
listen_address = QHostAddress(url.host());
}
if (listen_address == m_listenAddress || m_listenPort == url.port()) {
if (listen_address == m_listenAddress && m_listenPort == url.port()) {
return;
}
m_listenAddress = listen_address;
m_listenPort = quint16(url.port());
m_listenPort = listen_port;
m_listenAddressPort = full_uri;
if (m_httpServer.isListening()) {

View File

@ -15,6 +15,8 @@ class OAuthHttpHandler : public QObject {
explicit OAuthHttpHandler(QObject* parent = nullptr);
virtual ~OAuthHttpHandler();
bool isListening() const;
quint16 listenPort() const;
QHostAddress listenAddress() const;
QString listenAddressPort() const;
@ -62,11 +64,9 @@ class OAuthHttpHandler : public QObject {
quint16 m_port = 0;
QByteArray m_fragment;
QUrl m_url;
QPair<quint8, quint8> m_version;
QMap<QByteArray, QByteArray> m_headers;
};
QMap<QTcpSocket*, QHttpRequest> m_connectedClients;
QTcpServer m_httpServer;
QHostAddress m_listenAddress;

View File

@ -9,9 +9,8 @@
#include "services/gmail/definitions.h"
#include "services/gmail/gmailserviceroot.h"
FormEditGmailAccount::FormEditGmailAccount(QWidget* parent) : QDialog(parent),
m_oauth(new OAuth2Service(GMAIL_OAUTH_AUTH_URL, GMAIL_OAUTH_TOKEN_URL,
QString(), QString(), GMAIL_OAUTH_SCOPE)), m_editableRoot(nullptr) {
FormEditGmailAccount::FormEditGmailAccount(QWidget* parent)
: QDialog(parent), m_oauth(nullptr), m_editableRoot(nullptr) {
m_ui.setupUi(this);
GuiUtilities::applyDialogProperties(*this, qApp->icons()->miscIcon(QSL("gmail")));
@ -42,7 +41,6 @@ FormEditGmailAccount::FormEditGmailAccount(QWidget* parent) : QDialog(parent),
m_ui.m_spinLimitMessages->setMaximum(GMAIL_MAX_BATCH_SIZE);
checkUsername(m_ui.m_txtUsername->lineEdit()->text());
hookNetwork();
}
FormEditGmailAccount::~FormEditGmailAccount() = default;
@ -140,9 +138,14 @@ void FormEditGmailAccount::hookNetwork() {
GmailServiceRoot* FormEditGmailAccount::execForCreate() {
setWindowTitle(tr("Add new Gmail account"));
m_oauth = new OAuth2Service(GMAIL_OAUTH_AUTH_URL, GMAIL_OAUTH_TOKEN_URL,
QString(), QString(), GMAIL_OAUTH_SCOPE, this);
hookNetwork();
m_ui.m_txtAppId->lineEdit()->clear();
m_ui.m_txtAppKey->lineEdit()->clear();
m_ui.m_txtRedirectUrl->lineEdit()->setText(OAUTH_REDIRECT_URI);
m_ui.m_txtRedirectUrl->lineEdit()->setText(m_oauth->redirectUrl());
exec();
@ -153,18 +156,13 @@ void FormEditGmailAccount::execForEdit(GmailServiceRoot* existing_root) {
setWindowTitle(tr("Edit existing Gmail 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->setRedirectUrl(existing_root->network()->oauth()->redirectUrl());
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_oauth = m_editableRoot->network()->oauth();
hookNetwork();
// Setup the GUI.
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()->redirectUrl());
m_ui.m_txtAppId->lineEdit()->setText(m_oauth->clientId());
m_ui.m_txtAppKey->lineEdit()->setText(m_oauth->clientSecret());
m_ui.m_txtRedirectUrl->lineEdit()->setText(m_oauth->redirectUrl());
m_ui.m_txtUsername->lineEdit()->setText(existing_root->network()->username());
m_ui.m_spinLimitMessages->setValue(existing_root->network()->batchSize());

View File

@ -9,9 +9,8 @@
#include "services/inoreader/definitions.h"
#include "services/inoreader/inoreaderserviceroot.h"
FormEditInoreaderAccount::FormEditInoreaderAccount(QWidget* parent) : QDialog(parent),
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) {
FormEditInoreaderAccount::FormEditInoreaderAccount(QWidget* parent)
: QDialog(parent), m_oauth(nullptr), m_editableRoot(nullptr) {
m_ui.setupUi(this);
GuiUtilities::applyDialogProperties(*this, qApp->icons()->miscIcon(QSL("inoreader")));
@ -42,7 +41,6 @@ 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() = default;
@ -138,6 +136,10 @@ void FormEditInoreaderAccount::hookNetwork() {
InoreaderServiceRoot* FormEditInoreaderAccount::execForCreate() {
setWindowTitle(tr("Add new Inoreader account"));
m_oauth = new OAuth2Service(INOREADER_OAUTH_AUTH_URL, INOREADER_OAUTH_TOKEN_URL,
INOREADER_OAUTH_CLI_ID, INOREADER_OAUTH_CLI_KEY, INOREADER_OAUTH_SCOPE, this);
hookNetwork();
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(OAUTH_REDIRECT_URI);
@ -151,18 +153,13 @@ 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->setRedirectUrl(existing_root->network()->oauth()->redirectUrl());
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_oauth = m_editableRoot->network()->oauth();
hookNetwork();
// Setup the GUI.
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()->redirectUrl());
m_ui.m_txtAppId->lineEdit()->setText(m_oauth->clientId());
m_ui.m_txtAppKey->lineEdit()->setText(m_oauth->clientSecret());
m_ui.m_txtRedirectUrl->lineEdit()->setText(m_oauth->redirectUrl());
m_ui.m_txtUsername->lineEdit()->setText(existing_root->network()->userName());
m_ui.m_spinLimitMessages->setValue(existing_root->network()->batchSize());