File indexing completed on 2024-12-22 04:57:52

0001 /*
0002     SPDX-License-Identifier: BSD-2-Clause
0003 */
0004 
0005 #pragma once
0006 
0007 #include <QByteArray>
0008 #include <QObject>
0009 #include <QString>
0010 #include <QUrl>
0011 #include <QVariantMap>
0012 
0013 #include "o2/o0abstractstore.h"
0014 #include "o2/o0requestparameter.h"
0015 
0016 /// Base class of OAuth authenticators
0017 class O0BaseAuth : public QObject
0018 {
0019     Q_OBJECT
0020 
0021 public:
0022     explicit O0BaseAuth(QObject *parent = nullptr);
0023 
0024 public:
0025     /// Are we authenticated?
0026     Q_PROPERTY(bool linked READ linked WRITE setLinked NOTIFY linkedChanged)
0027     bool linked();
0028 
0029     /// Authentication token.
0030     Q_PROPERTY(QString token READ token NOTIFY tokenChanged)
0031     QString token() const;
0032 
0033     /// Authentication token secret.
0034     Q_PROPERTY(QString tokenSecret READ tokenSecret NOTIFY tokenSecretChanged)
0035     QString tokenSecret();
0036 
0037     /// Provider-specific extra tokens, available after a successful authentication
0038     Q_PROPERTY(QVariantMap extraTokens READ extraTokens NOTIFY extraTokensChanged)
0039     QVariantMap extraTokens();
0040 
0041     /// Client application ID.
0042     /// O1 instances with the same (client ID, client secret) share the same "linked", "token" and "tokenSecret" properties.
0043     Q_PROPERTY(QString clientId READ clientId WRITE setClientId NOTIFY clientIdChanged)
0044     QString clientId() const;
0045     void setClientId(const QString &value);
0046 
0047     /// Client application secret.
0048     /// O1 instances with the same (client ID, client secret) share the same "linked", "token" and "tokenSecret" properties.
0049     Q_PROPERTY(QString clientSecret READ clientSecret WRITE setClientSecret NOTIFY clientSecretChanged)
0050     QString clientSecret() const;
0051     void setClientSecret(const QString &value);
0052 
0053     /// TCP port number to use in local redirections.
0054     /// The OAuth "redirect_uri" will be set to "http://localhost:<localPort>/".
0055     /// If localPort is set to 0 (default), O2 will replace it with a free one.
0056     Q_PROPERTY(int localPort READ localPort WRITE setLocalPort NOTIFY localPortChanged)
0057     int localPort() const;
0058     void setLocalPort(int value);
0059 
0060     /// Sets the storage object to use for storing the OAuth tokens on a persistent medium
0061     void setStore(O0AbstractStore *store);
0062 
0063     /// Construct query string from list of headers
0064     static QByteArray createQueryParameters(const QList<O0RequestParameter> &parameters);
0065 
0066 public Q_SLOTS:
0067     /// Authenticate.
0068     Q_INVOKABLE virtual void link() = 0;
0069 
0070     /// De-authenticate.
0071     Q_INVOKABLE virtual void unlink() = 0;
0072 
0073 Q_SIGNALS:
0074     /// Emitted when client needs to open a web browser window, with the given URL.
0075     void openBrowser(const QUrl &url);
0076 
0077     /// Emitted when client can close the browser window.
0078     void closeBrowser();
0079 
0080     /// Emitted when authentication/deauthentication succeeded.
0081     void linkingSucceeded();
0082 
0083     /// Emitted when authentication/deauthentication failed.
0084     void linkingFailed();
0085 
0086     // Property change signals
0087 
0088     void linkedChanged();
0089     void clientIdChanged();
0090     void clientSecretChanged();
0091     void localPortChanged();
0092     void tokenChanged();
0093     void tokenSecretChanged();
0094     void extraTokensChanged();
0095 
0096 protected:
0097     /// Set authentication token.
0098     void setToken(const QString &v);
0099 
0100     /// Set authentication token secret.
0101     void setTokenSecret(const QString &v);
0102 
0103     /// Set the linked state
0104     void setLinked(bool v);
0105 
0106     /// Set extra tokens found in OAuth response
0107     void setExtraTokens(const QVariantMap &extraTokens);
0108 
0109 protected:
0110     QString clientId_;
0111     QString clientSecret_;
0112     QString redirectUri_;
0113     QString requestToken_;
0114     QString requestTokenSecret_;
0115     QUrl requestTokenUrl_;
0116     QUrl authorizeUrl_;
0117     QUrl accessTokenUrl_;
0118     quint16 localPort_;
0119     O0AbstractStore *store_ = nullptr;
0120     QVariantMap extraTokens_;
0121 };