File indexing completed on 2025-01-19 03:55:38

0001 #ifndef O0BASEAUTH_H
0002 #define O0BASEAUTH_H
0003 
0004 #include <QByteArray>
0005 #include <QObject>
0006 #include <QMap>
0007 #include <QString>
0008 #include <QUrl>
0009 #include <QVariantMap>
0010 
0011 #include "o0export.h"
0012 #include "o0abstractstore.h"
0013 #include "o0requestparameter.h"
0014 
0015 class O2ReplyServer;
0016 class O2PollServer;
0017 
0018 /// Base class of OAuth authenticators
0019 class O0_EXPORT O0BaseAuth : public QObject
0020 {
0021     Q_OBJECT
0022 public:
0023     explicit O0BaseAuth(QObject *parent = 0, O0AbstractStore *store = 0);
0024 
0025 public:
0026     /// Are we authenticated?
0027     Q_PROPERTY(bool linked READ linked WRITE setLinked NOTIFY linkedChanged)
0028     bool linked();
0029 
0030     /// Authentication token.
0031     Q_PROPERTY(QString token READ token NOTIFY tokenChanged)
0032     QString token();
0033 
0034     /// Authentication token secret.
0035     Q_PROPERTY(QString tokenSecret READ tokenSecret NOTIFY tokenSecretChanged)
0036     QString tokenSecret();
0037 
0038     /// Provider-specific extra tokens, available after a successful authentication
0039     Q_PROPERTY(QVariantMap extraTokens READ extraTokens NOTIFY extraTokensChanged)
0040     QVariantMap extraTokens();
0041 
0042     /// Client application ID.
0043     /// O1 instances with the same (client ID, client secret) share the same "linked", "token" and "tokenSecret" properties.
0044     Q_PROPERTY(QString clientId READ clientId WRITE setClientId NOTIFY clientIdChanged)
0045     QString clientId();
0046     void setClientId(const QString &value);
0047 
0048     /// Client application secret.
0049     /// O1 instances with the same (client ID, client secret) share the same "linked", "token" and "tokenSecret" properties.
0050     Q_PROPERTY(QString clientSecret READ clientSecret WRITE setClientSecret NOTIFY clientSecretChanged)
0051     QString clientSecret();
0052     void setClientSecret(const QString &value);
0053 
0054     /// Should we use a reply server (default) or an external web interceptor?
0055     Q_PROPERTY(bool useExternalWebInterceptor READ useExternalWebInterceptor WRITE setUseExternalWebInterceptor)
0056     bool useExternalWebInterceptor();
0057     void setUseExternalWebInterceptor(bool inUseExternalWebInterceptor);
0058 
0059     /// Page content on local host after successful oauth.
0060     /// Provide it in case you do not want to close the browser, but display something
0061     Q_PROPERTY(QByteArray replyContent READ replyContent WRITE setReplyContent)
0062     QByteArray replyContent() const;
0063     void setReplyContent(const QByteArray &value);
0064 
0065     /// TCP port number to use in local redirections.
0066     /// The OAuth "redirect_uri" will be set to "http://localhost:<localPort>/".
0067     /// If localPort is set to 0 (default), O2 will replace it with a free one.
0068     Q_PROPERTY(int localPort READ localPort WRITE setLocalPort NOTIFY localPortChanged)
0069     int localPort();
0070     void setLocalPort(int value);
0071 
0072     /// Sets the storage object to use for storing the OAuth tokens on a peristent medium
0073     void setStore(O0AbstractStore *store);
0074 
0075     /// Construct query string from list of headers
0076     static QByteArray createQueryParameters(const QList<O0RequestParameter> &parameters);
0077 
0078 public Q_SLOTS:
0079     /// Authenticate.
0080     Q_INVOKABLE virtual void link() = 0;
0081 
0082     /// De-authenticate.
0083     Q_INVOKABLE virtual void unlink() = 0;
0084 
0085 Q_SIGNALS:
0086     /// Emitted when client needs to open a web browser window, with the given URL.
0087     void openBrowser(const QUrl &url);
0088 
0089     /// Emitted when client can close the browser window.
0090     void closeBrowser();
0091 
0092     /// Emitted when client needs to show a verification uri and user code
0093     void showVerificationUriAndCode(const QUrl &uri, const QString &code);
0094 
0095     /// Emitted when authentication/deauthentication succeeded.
0096     void linkingSucceeded();
0097 
0098     /// Emitted when authentication/deauthentication failed.
0099     void linkingFailed();
0100 
0101     // Property change signals
0102 
0103     void linkedChanged();
0104     void clientIdChanged();
0105     void clientSecretChanged();
0106     void localPortChanged();
0107     void tokenChanged();
0108     void tokenSecretChanged();
0109     void extraTokensChanged();
0110 
0111 protected:
0112     /// Set authentication token.
0113     void setToken(const QString &v);
0114 
0115     /// Set authentication token secret.
0116     void setTokenSecret(const QString &v);
0117 
0118     /// Set the linked state
0119     void setLinked(bool v);
0120 
0121     /// Set extra tokens found in OAuth response
0122     void setExtraTokens(QVariantMap extraTokens);
0123 
0124     /// Set local reply server
0125     void setReplyServer(O2ReplyServer *server);
0126 
0127     O2ReplyServer * replyServer() const;
0128 
0129     /// Set local poll server
0130     void setPollServer(O2PollServer *server);
0131 
0132     O2PollServer * pollServer() const;
0133 
0134 protected:
0135     QString clientId_;
0136     QString clientSecret_;
0137     QString redirectUri_;
0138     QString requestToken_;
0139     QString requestTokenSecret_;
0140     QUrl requestTokenUrl_;
0141     QUrl authorizeUrl_;
0142     QUrl accessTokenUrl_;
0143     quint16 localPort_;
0144     O0AbstractStore *store_;
0145     QVariantMap extraTokens_;
0146     bool useExternalWebInterceptor_;
0147     QByteArray replyContent_;
0148 
0149 private:
0150     O2ReplyServer *replyServer_;
0151     O2PollServer *pollServer_;
0152 };
0153 
0154 #endif // O0BASEAUTH