2015-07-16 15:21:51 +03:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 by Christian Kamm <kamm@incasoftware.de>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
2016-10-25 12:00:07 +03:00
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
2015-07-16 15:21:51 +03:00
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "owncloudgui.h"
|
|
|
|
#include <QObject>
|
|
|
|
#include <QString>
|
|
|
|
#include <QNetworkProxy>
|
|
|
|
#include <QAuthenticator>
|
|
|
|
#include <QPointer>
|
|
|
|
#include <QScopedPointer>
|
|
|
|
#include <QSettings>
|
2015-07-17 13:46:04 +03:00
|
|
|
#include <QSet>
|
2015-07-16 15:21:51 +03:00
|
|
|
|
|
|
|
namespace QKeychain {
|
|
|
|
class Job;
|
|
|
|
class ReadPasswordJob;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace OCC {
|
|
|
|
|
|
|
|
class ConfigFile;
|
|
|
|
class ProxyAuthDialog;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Handle proxyAuthenticationRequired signals from our QNetworkAccessManagers.
|
|
|
|
*
|
|
|
|
* The main complication here is that the slot needs to return credential information
|
|
|
|
* synchronously - but running a dialog or getting password data from synchronous
|
|
|
|
* storage are asynchronous operations. This leads to reentrant calls that are
|
|
|
|
* fairly complicated to handle.
|
|
|
|
*/
|
|
|
|
class ProxyAuthHandler : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
2017-05-17 11:55:42 +03:00
|
|
|
static ProxyAuthHandler *instance();
|
2015-07-16 15:21:51 +03:00
|
|
|
|
2021-08-17 13:39:31 +03:00
|
|
|
~ProxyAuthHandler() override;
|
2015-07-16 15:21:51 +03:00
|
|
|
|
|
|
|
public slots:
|
|
|
|
/// Intended for QNetworkAccessManager::proxyAuthenticationRequired()
|
2017-05-17 11:55:42 +03:00
|
|
|
void handleProxyAuthenticationRequired(const QNetworkProxy &proxy,
|
|
|
|
QAuthenticator *authenticator);
|
2015-07-16 15:21:51 +03:00
|
|
|
|
|
|
|
private slots:
|
2017-05-17 11:55:42 +03:00
|
|
|
void slotSenderDestroyed(QObject *);
|
2015-07-16 15:21:51 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
ProxyAuthHandler();
|
|
|
|
|
|
|
|
/// Runs the ProxyAuthDialog and returns true if new credentials were entered.
|
|
|
|
bool getCredsFromDialog();
|
|
|
|
|
|
|
|
/// Checks the keychain for credentials of the current proxy.
|
|
|
|
bool getCredsFromKeychain();
|
|
|
|
|
|
|
|
/// Stores the current credentials in the keychain.
|
|
|
|
void storeCredsInKeychain();
|
|
|
|
|
2020-06-30 07:02:58 +03:00
|
|
|
template<class T, typename PointerToMemberFunction>
|
|
|
|
void execAwait(const T *sender,
|
|
|
|
PointerToMemberFunction signal,
|
|
|
|
int &counter,
|
|
|
|
const QEventLoop::ProcessEventsFlags flags = QEventLoop::AllEvents);
|
|
|
|
|
2022-09-30 20:12:08 +03:00
|
|
|
[[nodiscard]] QString keychainUsernameKey() const;
|
|
|
|
[[nodiscard]] QString keychainPasswordKey() const;
|
2015-07-16 15:21:51 +03:00
|
|
|
|
2015-10-05 07:21:19 +03:00
|
|
|
/// The hostname:port of the current proxy, used for detecting switches
|
2015-07-16 15:21:51 +03:00
|
|
|
/// to a different proxy.
|
|
|
|
QString _proxy;
|
|
|
|
|
|
|
|
QString _username;
|
|
|
|
QString _password;
|
|
|
|
|
|
|
|
/// If the user cancels the credential dialog, blocked will be set to
|
|
|
|
/// true and we won't bother him again.
|
2020-05-28 17:59:24 +03:00
|
|
|
bool _blocked = false;
|
2015-07-16 15:21:51 +03:00
|
|
|
|
|
|
|
/// In several instances handleProxyAuthenticationRequired() can be called
|
|
|
|
/// while it is still running. These counters detect what we're currently
|
|
|
|
/// waiting for.
|
2020-05-28 17:59:24 +03:00
|
|
|
int _waitingForDialog = 0;
|
|
|
|
int _waitingForKeychain = 0;
|
2015-07-16 15:21:51 +03:00
|
|
|
|
|
|
|
QPointer<ProxyAuthDialog> _dialog;
|
|
|
|
|
|
|
|
/// The QSettings instance to securely store username/password in the keychain.
|
|
|
|
QScopedPointer<QSettings> _settings;
|
|
|
|
|
|
|
|
/// Pointer to the most-recently-run ReadPasswordJob, needed due to reentrancy.
|
|
|
|
QScopedPointer<QKeychain::ReadPasswordJob> _readPasswordJob;
|
|
|
|
|
|
|
|
/// For checking the proxy config settings.
|
|
|
|
QScopedPointer<ConfigFile> _configFile;
|
2015-07-17 13:46:04 +03:00
|
|
|
|
|
|
|
/// To distinguish between a new QNAM asking for credentials and credentials
|
|
|
|
/// failing for an existing QNAM, we keep track of the senders of the
|
|
|
|
/// proxyAuthRequired signal here.
|
2017-05-17 11:55:42 +03:00
|
|
|
QSet<QObject *> _gaveCredentialsTo;
|
2015-07-16 15:21:51 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace OCC
|