File indexing completed on 2024-12-22 04:56:59

0001 /*
0002     SPDX-FileCopyrightText: 2018 Krzysztof Nowicki <krissn@op.pl>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 #pragma once
0007 
0008 #include <QObject>
0009 
0010 /**
0011  *  @brief  Abstract base class for authentication providers
0012  *
0013  *  This class forms an interface for authentication modules. It abstracts
0014  *  all actions needed to authenticate HTTP requests.
0015  *
0016  *  The goal is for the EwsResource class to instantiate a concrete class
0017  *  derived from this one depending on the configured authentication setting.
0018  *  Once the class is instantiated the resource class must connect the
0019  *  requestWalletPassword(), requestWalletMap(), walletPasswordReqiestFinished()
0020  *  and walletMapRequestFinished() methods to the appropriate signals/slots of
0021  *  the settings object. Once that is done the resource shall call the init()
0022  *  method which triggers retrieval of any stored credential state.
0023  *
0024  *  The main user of this class is the @e EwsRequest class, which uses the
0025  *  getAuthData() method on every HTTP request it issues. This method shall
0026  *  return all the necessary information needed to authenticate, which could
0027  *  be a username/password set and/or custom HTTP headers. At this stage the
0028  *  getAuthData() method must return immediately - it must not perform any
0029  *  long operations such as external requests. In case no authentication
0030  *  information is available the method returns false, which causes the request
0031  *  to be aborted.
0032  *
0033  *  In case the request fails due to a 401 Unauthorized error or is aborted due
0034  *  to abobe conditions the request calls the requestAuthFailed() method, signals
0035  *  the main resource class that authentication has failed and aborts the request.
0036  *
0037  *  The main resource class in response to an authentication failure sets the
0038  *  resource offline and attempts to reauthenticate (if the resource supports it)
0039  *  by calling the authenticate() method - first with the @e interactive argument
0040  *  set to @e false. If that fails (the authFailed() signal is received) the
0041  *  resource displays a notification message that interactive authentication is
0042  *  needed. The message to display is retrieved using the reauthPrompt() method.
0043  *  If the user chooses to authenticate the authenticate() method is called with
0044  *  @e interactive set to @e true. If that also fails, the failed authentication
0045  *  prompt is retrieved using the authFailedPrompt() method and the resource stays
0046  *  offline until the user updates the configuration. In case authentication
0047  *  succeeds at any stage the resource is set back to online. At this stage the
0048  *  authentication class also uses the setWalletPassword() and setWalletMap()
0049  *  signals to write the updated credentials to the wallet.
0050  */
0051 class EwsAbstractAuth : public QObject
0052 {
0053     Q_OBJECT
0054 public:
0055     explicit EwsAbstractAuth(QObject *parent = nullptr);
0056     ~EwsAbstractAuth() override = default;
0057     virtual void init() = 0;
0058     virtual bool getAuthData(QString &username, QString &password, QStringList &customHeaders) = 0;
0059     virtual void notifyRequestAuthFailed();
0060     virtual bool authenticate(bool interactive) = 0;
0061     virtual const QString &reauthPrompt() const = 0;
0062     virtual const QString &authFailedPrompt() const = 0;
0063 
0064     virtual void walletPasswordRequestFinished(const QString &password) = 0;
0065     virtual void walletMapRequestFinished(const QMap<QString, QString> &map) = 0;
0066 
0067     void setAuthParentWidget(QWidget *widget);
0068 
0069     void setPKeyAuthCertificateFiles(const QString &certFile, const QString &pkeyFile);
0070 Q_SIGNALS:
0071     void authSucceeded();
0072     void authFailed(const QString &error);
0073     void requestAuthFailed();
0074     void requestWalletPassword(bool ask);
0075     void requestWalletMap();
0076     void setWalletPassword(const QString &password);
0077     void setWalletMap(const QMap<QString, QString> &map);
0078 
0079 protected:
0080     QWidget *mAuthParentWidget = nullptr;
0081     QString mPKeyCertFile;
0082     QString mPKeyKeyFile;
0083 };