LatitudeUpdater 0.1, fix the previous commit
[googlelatitude] / libkqoauth / kqoauthrequest.h
1 /**
2  * KQOAuth - An OAuth authentication library for Qt.
3  *
4  * Author: Johan Paul (johan.paul@d-pointer.com)
5  *         http://www.d-pointer.com
6  *
7  *  KQOAuth is free software: you can redistribute it and/or modify
8  *  it under the terms of the GNU Lesser General Public License as published by
9  *  the Free Software Foundation, either version 3 of the License, or
10  *  (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
27 #include "kqoauthglobals.h"
28
29 typedef QMultiMap<QString, QString> KQOAuthParameters;
30
31 class KQOAuthRequestPrivate;
32 class KQOAUTH_EXPORT KQOAuthRequest : public QObject
33 {
34     Q_OBJECT
35 public:
36     explicit KQOAuthRequest(QObject *parent = 0);
37     ~KQOAuthRequest();
38
39     enum RequestType {
40         TemporaryCredentials = 0,
41         AccessToken,
42         AuthorizedRequest
43     };
44
45     enum RequestSignatureMethod {
46         PLAINTEXT = 0,
47         HMAC_SHA1,
48         RSA_SHA1
49     };
50
51     enum RequestHttpMethod {
52         GET = 0,
53         POST
54     };
55
56     /**
57      * These methods can be overridden in child classes which are different types of
58      * OAuth requests.
59      */
60     // Validate the request of this type.
61     virtual bool isValid() const;
62
63     /**
64      * These methods are OAuth request type specific and not overridden in child
65      * classes.
66      * NOTE: Refactorting still a TODO
67      */
68     // Initialize the request of this type.
69     void initRequest(KQOAuthRequest::RequestType type, const QUrl &requestEndpoint);
70
71     void setConsumerKey(const QString &consumerKey);
72     void setConsumerSecretKey(const QString &consumerSecretKey);
73
74     // Mandatory methods for acquiring a request token
75     void setCallbackUrl(const QUrl &callbackUrl);
76
77     // Mandator methods for acquiring a access token
78     void setTokenSecret(const QString &tokenSecret);
79     void setToken(const QString &token);
80     void setVerifier(const QString &verifier);
81
82     // Request signature method to use - HMAC_SHA1 currently only supported
83     void setSignatureMethod(KQOAuthRequest::RequestSignatureMethod = KQOAuthRequest::HMAC_SHA1);
84
85     // Request's HTTP method.
86     void setHttpMethod(KQOAuthRequest::RequestHttpMethod = KQOAuthRequest::POST);
87     KQOAuthRequest::RequestHttpMethod httpMethod() const;
88
89     // Sets the timeout for this request. If the timeout expires, signal "requestTimedout" will be
90     // emitted from the manager.
91     // 0 = If set to zero, timeout is disabled.
92     // TODO: Do we need some request ID now?
93     void setTimeout(int timeoutMilliseconds);
94
95     // Additional optional parameters to the request.
96     void setAdditionalParameters(const KQOAuthParameters &additionalParams);
97     KQOAuthParameters additionalParameters() const;
98     QList<QByteArray> requestParameters();  // This will return all request's parameters in the raw format given
99                                             // to the QNetworkRequest.
100     QByteArray requestBody() const;         // This will return the POST body as given to the QNetworkRequest.
101
102     KQOAuthRequest::RequestType requestType() const;
103     QUrl requestEndpoint() const;
104
105     void setContentType(const QString &contentType);
106     QString contentType();
107
108     void setRawData(const QByteArray &rawData);
109     QByteArray rawData();
110
111     void clearRequest();
112
113     // Enable verbose debug output for request content.
114     void setEnableDebugOutput(bool enabled);
115
116 Q_SIGNALS:
117     // This signal is emited if the request is not completed before the request's timeout
118     // value has expired.
119     void requestTimedout();
120
121 protected:
122     bool validateXAuthRequest() const;
123
124 private:
125     KQOAuthRequestPrivate * const d_ptr;
126     Q_DECLARE_PRIVATE(KQOAuthRequest);
127     Q_DISABLE_COPY(KQOAuthRequest);
128
129     // These classes are only for the internal use of KQOAuthManager so it can
130     // work with the opaque request.
131     QString consumerKeyForManager() const;
132     QString consumerKeySecretForManager() const;
133     QUrl callbackUrlForManager() const;
134
135     // This method is for timeout handling by the KQOAuthManager.
136     void requestTimerStart();
137     void requestTimerStop();
138
139     friend class KQOAuthManager;
140 #ifdef UNIT_TEST
141     friend class Ut_KQOAuth;
142 #endif
143 };
144
145 #endif // KQOAUTHREQUEST_H