File indexing completed on 2025-01-19 03:55:38
0001 #ifndef O1_H 0002 #define O1_H 0003 0004 #include <QNetworkAccessManager> 0005 #include <QUrl> 0006 #include <QNetworkReply> 0007 0008 #include "o0export.h" 0009 #include "o0baseauth.h" 0010 0011 /// Simple OAuth 1.0 authenticator. 0012 class O0_EXPORT O1: public O0BaseAuth { 0013 Q_OBJECT 0014 0015 public: 0016 /// HTTP User-Agent header 0017 /// Set user agent to a value unique for your application (https://tools.ietf.org/html/rfc7231#section-5.5.3) 0018 /// if you see the following error in the application log: 0019 /// O1::onTokenRequestError: 201 "Error transferring requestTokenUrl() - server replied: Forbidden" "Bad bot" 0020 Q_PROPERTY(QByteArray userAgent READ userAgent WRITE setUserAgent) 0021 QByteArray userAgent() const; 0022 void setUserAgent(const QByteArray &value); 0023 0024 /// Signature method 0025 Q_PROPERTY(QString signatureMethod READ signatureMethod WRITE setSignatureMethod NOTIFY signatureMethodChanged) 0026 QString signatureMethod(); 0027 void setSignatureMethod(const QString &value); 0028 0029 /// Token request URL. 0030 Q_PROPERTY(QUrl requestTokenUrl READ requestTokenUrl WRITE setRequestTokenUrl NOTIFY requestTokenUrlChanged) 0031 QUrl requestTokenUrl(); 0032 void setRequestTokenUrl(const QUrl &value); 0033 0034 /// Parameters to pass with request URL. 0035 Q_PROPERTY(QList<O0RequestParameter> requestParameters READ requestParameters WRITE setRequestParameters) 0036 QList<O0RequestParameter> requestParameters(); 0037 void setRequestParameters(const QList<O0RequestParameter> &value); 0038 0039 /// Callback URL. 0040 /// It should contain a `%1` place marker, to be replaced by `O0BaseAuth::localPort()`. 0041 /// Defaults to `O2_CALLBACK_URL`. 0042 Q_PROPERTY(QString callbackUrl READ callbackUrl WRITE setCallbackUrl) 0043 QString callbackUrl(); 0044 void setCallbackUrl(const QString &value); 0045 0046 /// Authorization URL. 0047 Q_PROPERTY(QUrl authorizeUrl READ authorizeUrl WRITE setAuthorizeUrl NOTIFY authorizeUrlChanged) 0048 QUrl authorizeUrl(); 0049 void setAuthorizeUrl(const QUrl &value); 0050 0051 /// Access token URL. 0052 Q_PROPERTY(QUrl accessTokenUrl READ accessTokenUrl WRITE setAccessTokenUrl NOTIFY accessTokenUrlChanged) 0053 QUrl accessTokenUrl(); 0054 void setAccessTokenUrl(const QUrl &value); 0055 0056 /// Constructor. 0057 explicit O1(QObject *parent = 0, QNetworkAccessManager *manager = 0, O0AbstractStore *store = 0); 0058 0059 /// Parse a URL-encoded response string. 0060 static QMap<QString, QString> parseResponse(const QByteArray &response); 0061 0062 /// Build the value of the "Authorization:" header. 0063 static QByteArray buildAuthorizationHeader(const QList<O0RequestParameter> &oauthParams); 0064 0065 /// Add common configuration (headers) to @p req. 0066 void decorateRequest(QNetworkRequest &req, const QList<O0RequestParameter> &oauthParams); 0067 0068 /// Create unique bytes to prevent replay attacks. 0069 static QByteArray nonce(); 0070 0071 /// Generate signature string depending on signature method type 0072 QByteArray generateSignature(const QList<O0RequestParameter> headers, const QNetworkRequest &req, const QList<O0RequestParameter> &signingParameters, QNetworkAccessManager::Operation operation); 0073 0074 /// Calculate the HMAC-SHA1 signature of a request. 0075 /// @param oauthParams OAuth parameters. 0076 /// @param otherParams Other parameters participating in signing. 0077 /// @param URL Request URL. May contain query parameters, but they will not be used for signing. 0078 /// @param op HTTP operation. 0079 /// @param consumerSecret Consumer (application) secret. 0080 /// @param tokenSecret Authorization token secret (empty if not yet available). 0081 /// @return Signature that can be used as the value of the "oauth_signature" parameter. 0082 static QByteArray sign(const QList<O0RequestParameter> &oauthParams, const QList<O0RequestParameter> &otherParams, const QUrl &url, QNetworkAccessManager::Operation op, const QString &consumerSecret, const QString &tokenSecret); 0083 0084 /// Build a base string for signing. 0085 static QByteArray getRequestBase(const QList<O0RequestParameter> &oauthParams, const QList<O0RequestParameter> &otherParams, const QUrl &url, QNetworkAccessManager::Operation op); 0086 0087 /// Build a concatenated/percent-encoded string from a list of headers. 0088 static QByteArray encodeHeaders(const QList<O0RequestParameter> &headers); 0089 0090 public Q_SLOTS: 0091 /// Authenticate. 0092 Q_INVOKABLE virtual void link(); 0093 0094 /// De-authenticate. 0095 Q_INVOKABLE virtual void unlink(); 0096 0097 Q_SIGNALS: 0098 void requestTokenUrlChanged(); 0099 void authorizeUrlChanged(); 0100 void accessTokenUrlChanged(); 0101 void signatureMethodChanged(); 0102 0103 public Q_SLOTS: 0104 /// Handle verification received from the reply server. 0105 virtual void onVerificationReceived(QMap<QString,QString> params); 0106 0107 protected Q_SLOTS: 0108 /// Handle token request error. 0109 virtual void onTokenRequestError(QNetworkReply::NetworkError error); 0110 0111 /// Handle token request finished. 0112 virtual void onTokenRequestFinished(); 0113 0114 /// Handle token exchange error. 0115 void onTokenExchangeError(QNetworkReply::NetworkError error); 0116 0117 /// Handle token exchange finished. 0118 void onTokenExchangeFinished(); 0119 0120 protected: 0121 /// Exchange temporary token to authentication token 0122 void exchangeToken(); 0123 0124 QByteArray userAgent_; 0125 QUrl requestUrl_; 0126 QList<O0RequestParameter> requestParameters_; 0127 QString callbackUrl_; 0128 QUrl tokenUrl_; 0129 QUrl refreshTokenUrl_; 0130 QString verifier_; 0131 QString signatureMethod_; 0132 QNetworkAccessManager *manager_; 0133 }; 0134 0135 #endif // O1_H