File indexing completed on 2025-01-19 03:55:39
0001 #ifndef O2_H 0002 #define O2_H 0003 0004 #include <QNetworkAccessManager> 0005 #include <QNetworkRequest> 0006 #include <QNetworkReply> 0007 #include <QPair> 0008 0009 #include "o0export.h" 0010 #include "o0baseauth.h" 0011 #include "o2reply.h" 0012 #include "o0abstractstore.h" 0013 0014 /// Simple OAuth2 authenticator. 0015 class O0_EXPORT O2: public O0BaseAuth 0016 { 0017 Q_OBJECT 0018 public: 0019 Q_ENUMS(GrantFlow) 0020 0021 public: 0022 /// Authorization flow types. 0023 enum GrantFlow { 0024 GrantFlowAuthorizationCode, ///< @see http://tools.ietf.org/html/draft-ietf-oauth-v2-15#section-4.1 0025 GrantFlowImplicit, ///< @see http://tools.ietf.org/html/draft-ietf-oauth-v2-15#section-4.2 0026 GrantFlowResourceOwnerPasswordCredentials, 0027 GrantFlowDevice ///< @see https://tools.ietf.org/html/rfc8628#section-1 0028 }; 0029 0030 /// Authorization flow. 0031 Q_PROPERTY(GrantFlow grantFlow READ grantFlow WRITE setGrantFlow NOTIFY grantFlowChanged) 0032 GrantFlow grantFlow(); 0033 void setGrantFlow(GrantFlow value); 0034 0035 /// Resource owner username. 0036 /// O2 instances with the same (username, password) share the same "linked" and "token" properties. 0037 Q_PROPERTY(QString username READ username WRITE setUsername NOTIFY usernameChanged) 0038 QString username(); 0039 void setUsername(const QString &value); 0040 0041 /// Resource owner password. 0042 /// O2 instances with the same (username, password) share the same "linked" and "token" properties. 0043 Q_PROPERTY(QString password READ password WRITE setPassword NOTIFY passwordChanged) 0044 QString password(); 0045 void setPassword(const QString &value); 0046 0047 /// Scope of authentication. 0048 Q_PROPERTY(QString scope READ scope WRITE setScope NOTIFY scopeChanged) 0049 QString scope(); 0050 void setScope(const QString &value); 0051 0052 /// Localhost policy. By default it's value is http://127.0.0.1:%1/, however some services may 0053 /// require the use of http://localhost:%1/ or any other value. 0054 Q_PROPERTY(QString localhostPolicy READ localhostPolicy WRITE setLocalhostPolicy) 0055 QString localhostPolicy() const; 0056 void setLocalhostPolicy(const QString &value); 0057 0058 /// API key. 0059 Q_PROPERTY(QString apiKey READ apiKey WRITE setApiKey) 0060 QString apiKey(); 0061 void setApiKey(const QString &value); 0062 0063 /// Allow ignoring SSL errors? 0064 /// E.g. SurveyMonkey fails on Mac due to SSL error. Ignoring the error circumvents the problem 0065 Q_PROPERTY(bool ignoreSslErrors READ ignoreSslErrors WRITE setIgnoreSslErrors) 0066 bool ignoreSslErrors(); 0067 void setIgnoreSslErrors(bool ignoreSslErrors); 0068 0069 /// Request URL. 0070 Q_PROPERTY(QString requestUrl READ requestUrl WRITE setRequestUrl NOTIFY requestUrlChanged) 0071 QString requestUrl(); 0072 void setRequestUrl(const QString &value); 0073 0074 /// User-defined extra parameters to append to request URL 0075 Q_PROPERTY(QVariantMap extraRequestParams READ extraRequestParams WRITE setExtraRequestParams NOTIFY extraRequestParamsChanged) 0076 QVariantMap extraRequestParams(); 0077 void setExtraRequestParams(const QVariantMap &value); 0078 0079 /// Token request URL. 0080 Q_PROPERTY(QString tokenUrl READ tokenUrl WRITE setTokenUrl NOTIFY tokenUrlChanged) 0081 QString tokenUrl(); 0082 void setTokenUrl(const QString &value); 0083 0084 /// Token refresh URL. 0085 Q_PROPERTY(QString refreshTokenUrl READ refreshTokenUrl WRITE setRefreshTokenUrl NOTIFY refreshTokenUrlChanged) 0086 QString refreshTokenUrl(); 0087 void setRefreshTokenUrl(const QString &value); 0088 0089 /// Grant type (if non-standard) 0090 Q_PROPERTY(QString grantType READ grantType WRITE setGrantType) 0091 QString grantType(); 0092 void setGrantType(const QString &value); 0093 0094 public: 0095 /// Constructor. 0096 /// @param parent Parent object. 0097 explicit O2(QObject *parent = 0, QNetworkAccessManager *manager = 0, O0AbstractStore *store = 0); 0098 0099 /// Get authentication code. 0100 QString code(); 0101 0102 /// Get refresh token. 0103 QString refreshToken(); 0104 0105 /// Get token expiration time (seconds from Epoch). 0106 int expires(); 0107 0108 public Q_SLOTS: 0109 /// Authenticate. 0110 Q_INVOKABLE virtual void link(); 0111 0112 /// De-authenticate. 0113 Q_INVOKABLE virtual void unlink(); 0114 0115 /// Refresh token. 0116 Q_INVOKABLE void refresh(); 0117 0118 /// Handle situation where reply server has opted to close its connection 0119 void serverHasClosed(bool paramsfound = false); 0120 0121 Q_SIGNALS: 0122 /// Emitted when a token refresh has been completed or failed. 0123 void refreshFinished(QNetworkReply::NetworkError error); 0124 0125 // Property change signals 0126 void grantFlowChanged(); 0127 void scopeChanged(); 0128 void usernameChanged(); 0129 void passwordChanged(); 0130 void requestUrlChanged(); 0131 void extraRequestParamsChanged(); 0132 void refreshTokenUrlChanged(); 0133 void tokenUrlChanged(); 0134 0135 public Q_SLOTS: 0136 /// Handle verification response. 0137 virtual void onVerificationReceived(QMap<QString, QString>); 0138 0139 protected Q_SLOTS: 0140 /// Handle completion of a token request. 0141 virtual void onTokenReplyFinished(); 0142 0143 /// Handle failure of a token request. 0144 virtual void onTokenReplyError(QNetworkReply::NetworkError error); 0145 0146 /// Handle completion of a refresh request. 0147 virtual void onRefreshFinished(); 0148 0149 /// Handle failure of a refresh request. 0150 virtual void onRefreshError(QNetworkReply::NetworkError error); 0151 0152 /// Handle completion of a Device Authorization Request 0153 virtual void onDeviceAuthReplyFinished(); 0154 0155 protected: 0156 /// Build HTTP request body. 0157 QByteArray buildRequestBody(const QMap<QString, QString> ¶meters); 0158 0159 /// Set authentication code. 0160 void setCode(const QString &v); 0161 0162 /// Set refresh token. 0163 void setRefreshToken(const QString &v); 0164 0165 /// Set token expiration time. 0166 void setExpires(int v); 0167 0168 /// Start polling authorization server 0169 void startPollServer(const QVariantMap ¶ms); 0170 0171 protected: 0172 QString username_; 0173 QString password_; 0174 QUrl requestUrl_; 0175 QVariantMap extraReqParams_; 0176 QUrl tokenUrl_; 0177 QUrl refreshTokenUrl_; 0178 QString scope_; 0179 QString code_; 0180 QString localhostPolicy_; 0181 QString apiKey_; 0182 QNetworkAccessManager *manager_; 0183 O2ReplyList timedReplies_; 0184 GrantFlow grantFlow_; 0185 QString grantType_; 0186 }; 0187 0188 #endif // O2_H