| 1 |
/** |
| 2 |
* KQOAuth - An OAuth authentication library for Qt. |
| 3 |
* |
| 4 |
* Author: Johan Paul ([email protected]) |
| 5 |
* http://www.johanpaul.com |
| 6 |
* |
| 7 |
* This library is free software; you can redistribute it and/or |
| 8 |
* modify it under the terms of the GNU Lesser General Public |
| 9 |
* License as published by the Free Software Foundation; either |
| 10 |
* version 2.1 of the License, or (at your option) any later version. |
| 11 |
* |
| 12 |
* KQOAuth is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU Lesser General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU Lesser General Public License |
| 18 |
* along with KQOAuth. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
*/ |
| 20 |
#ifndef KQOAUTHREQUEST_H |
| 21 |
#define KQOAUTHREQUEST_H |
| 22 |
|
| 23 |
#include <QObject> |
| 24 |
#include <QUrl> |
| 25 |
#include <QMultiMap> |
| 26 |
#include <QHttpMultiPart> |
| 27 |
|
| 28 |
#include "kqoauthglobals.h" |
| 29 |
|
| 30 |
typedef QMultiMap<QString, QString> KQOAuthParameters; |
| 31 |
|
| 32 |
class KQOAuthRequestPrivate; |
| 33 |
class KQOAuthRequest : public QObject |
| 34 |
{ |
| 35 |
Q_OBJECT |
| 36 |
public: |
| 37 |
explicit KQOAuthRequest(QObject *parent = 0); |
| 38 |
~KQOAuthRequest(); |
| 39 |
|
| 40 |
enum RequestType { |
| 41 |
TemporaryCredentials = 0, |
| 42 |
AccessToken, |
| 43 |
AuthorizedRequest |
| 44 |
}; |
| 45 |
|
| 46 |
enum RequestSignatureMethod { |
| 47 |
PLAINTEXT = 0, |
| 48 |
HMAC_SHA1 |
| 49 |
}; |
| 50 |
#undef DELETE |
| 51 |
enum RequestHttpMethod { |
| 52 |
GET = 0, |
| 53 |
POST, |
| 54 |
HEAD, |
| 55 |
DELETE |
| 56 |
}; |
| 57 |
|
| 58 |
/** |
| 59 |
* These methods can be overridden in child classes which are different types of |
| 60 |
* OAuth requests. |
| 61 |
*/ |
| 62 |
// Validate the request of this type. |
| 63 |
virtual bool isValid() const; |
| 64 |
|
| 65 |
/** |
| 66 |
* These methods are OAuth request type specific and not overridden in child |
| 67 |
* classes. |
| 68 |
* NOTE: Refactorting still a TODO |
| 69 |
*/ |
| 70 |
// Initialize the request of this type. |
| 71 |
void initRequest(KQOAuthRequest::RequestType type, const QUrl &requestEndpoint); |
| 72 |
|
| 73 |
void setConsumerKey(const QString &consumerKey); |
| 74 |
void setConsumerSecretKey(const QString &consumerSecretKey); |
| 75 |
|
| 76 |
// Mandatory methods for acquiring a request token |
| 77 |
void setCallbackUrl(const QUrl &callbackUrl); |
| 78 |
|
| 79 |
// Mandator methods for acquiring a access token |
| 80 |
void setTokenSecret(const QString &tokenSecret); |
| 81 |
void setToken(const QString &token); |
| 82 |
void setVerifier(const QString &verifier); |
| 83 |
|
| 84 |
// Request signature method to use - HMAC_SHA1 currently only supported |
| 85 |
void setSignatureMethod(KQOAuthRequest::RequestSignatureMethod = KQOAuthRequest::HMAC_SHA1); |
| 86 |
|
| 87 |
// Request's HTTP method. |
| 88 |
void setHttpMethod(KQOAuthRequest::RequestHttpMethod = KQOAuthRequest::POST); |
| 89 |
KQOAuthRequest::RequestHttpMethod httpMethod() const; |
| 90 |
|
| 91 |
// Sets the timeout for this request. If the timeout expires, the signal "requestTimedout" will be |
| 92 |
// emitted. The KQOAuthManager will then call the abort() function from QNetworkReply associated with this request |
| 93 |
// 0 = If set to zero, timeout is disabled. |
| 94 |
// TODO: Do we need some request ID now? |
| 95 |
void setTimeout(int timeoutMilliseconds); |
| 96 |
|
| 97 |
// Additional optional parameters to the request. |
| 98 |
void setAdditionalParameters(const KQOAuthParameters &additionalParams); |
| 99 |
KQOAuthParameters additionalParameters() const; |
| 100 |
QList<QByteArray> requestParameters(); // This will return all request's parameters in the raw format given |
| 101 |
// to the QNetworkRequest. |
| 102 |
QByteArray requestBody() const; // This will return the POST body as given to the QNetworkRequest. |
| 103 |
|
| 104 |
KQOAuthRequest::RequestType requestType() const; |
| 105 |
QUrl requestEndpoint() const; |
| 106 |
|
| 107 |
void setContentType(const QString &contentType); |
| 108 |
QString contentType(); |
| 109 |
|
| 110 |
void setRawData(const QByteArray &rawData); |
| 111 |
QByteArray rawData(); |
| 112 |
|
| 113 |
void setHttpMultiPart(QHttpMultiPart* httpMultiPart); |
| 114 |
QHttpMultiPart* httpMultiPart(); |
| 115 |
|
| 116 |
void clearRequest(); |
| 117 |
|
| 118 |
// Enable verbose debug output for request content. |
| 119 |
void setEnableDebugOutput(bool enabled); |
| 120 |
|
| 121 |
Q_SIGNALS: |
| 122 |
// This signal is emited if the request is not completed before the request's timeout |
| 123 |
// value has expired. |
| 124 |
void requestTimedout(); |
| 125 |
|
| 126 |
protected: |
| 127 |
bool validateXAuthRequest() const; |
| 128 |
|
| 129 |
private: |
| 130 |
KQOAuthRequestPrivate * const d_ptr; |
| 131 |
Q_DECLARE_PRIVATE(KQOAuthRequest); |
| 132 |
Q_DISABLE_COPY(KQOAuthRequest); |
| 133 |
|
| 134 |
// These classes are only for the internal use of KQOAuthManager so it can |
| 135 |
// work with the opaque request. |
| 136 |
QString consumerKeyForManager() const; |
| 137 |
QString consumerKeySecretForManager() const; |
| 138 |
KQOAuthRequest::RequestSignatureMethod requestSignatureMethodForManager() const; |
| 139 |
QUrl callbackUrlForManager() const; |
| 140 |
|
| 141 |
// This method is for timeout handling by the KQOAuthManager. |
| 142 |
void requestTimerStart(); |
| 143 |
void requestTimerStop(); |
| 144 |
|
| 145 |
friend class KQOAuthManager; |
| 146 |
#ifdef UNIT_TEST |
| 147 |
friend class Ut_KQOAuth; |
| 148 |
#endif |
| 149 |
}; |
| 150 |
|
| 151 |
#endif // KQOAUTHREQUEST_H |