| 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 KQOAUTHMANAGER_H |
| 21 | #define KQOAUTHMANAGER_H |
| 22 | |
| 23 | #include <QObject> |
| 24 | #include <QMultiMap> |
| 25 | #include <QNetworkReply> |
| 26 | |
| 27 | #include "kqoauthrequest.h" |
| 28 | |
| 29 | class KQOAuthRequest; |
| 30 | class KQOAuthManagerThread; |
| 31 | class KQOAuthManagerPrivate; |
| 32 | class QNetworkAccessManager; |
| 33 | class QUrl; |
| 34 | class QByteArray; |
| 35 | class KQOAUTH_EXPORT KQOAuthManager : public QObject |
| 36 | { |
| 37 | Q_OBJECT |
| 38 | public: |
| 39 | |
| 40 | enum KQOAuthError { |
| 41 | NoError, // No error |
| 42 | NetworkError, // Network error: timeout, cannot connect. |
| 43 | RequestEndpointError, // Request endpoint is not valid. |
| 44 | RequestValidationError, // Request is not valid: some parameter missing? |
| 45 | RequestUnauthorized, // Authorization error: trying to access a resource without tokens. |
| 46 | RequestError, // The given request to KQOAuthManager is invalid: NULL?, |
| 47 | ManagerError // Manager error, cannot use for sending requests. |
| 48 | }; |
| 49 | |
| 50 | explicit KQOAuthManager(QObject *parent = 0); |
| 51 | ~KQOAuthManager(); |
| 52 | |
| 53 | KQOAuthError lastError(); |
| 54 | |
| 55 | /** |
| 56 | * The manager executes the given request. It takes the HTTP parameters from the |
| 57 | * request and uses QNetworkAccessManager to submit the HTTP request to the net. |
| 58 | * When the request is done it will emit signal requestReady(QByteArray networkReply). |
| 59 | * NOTE: At the moment there is no timeout for the request. |
| 60 | */ |
| 61 | void executeRequest(KQOAuthRequest *request); |
| 62 | void executeAuthorizedRequest(KQOAuthRequest *request, int id); |
| 63 | /** |
| 64 | * Indicates to the user that KQOAuthManager should handle user authorization by |
| 65 | * opening the user's default browser and parsing the reply from the service. |
| 66 | * By setting the parameter to true, KQOAuthManager will store intermediate results |
| 67 | * of the OAuth 1.0 process in its own opaque request. This information is used in |
| 68 | * the user authorization process and also when calling sendAuthorizedRequest(). |
| 69 | * NOTE: You need to set this to true if you want to use getUserAccessTokens() or |
| 70 | * sendAuthorizedRequest(). |
| 71 | */ |
| 72 | void setHandleUserAuthorization(bool set); |
| 73 | |
| 74 | /** Indicates whether the KQOAuthManager should launch the browser with the user |
| 75 | * authorization page itself. |
| 76 | * |
| 77 | * If set to true (the default), the KQOAuthManager uses QDesktopServices::openUrl() |
| 78 | * for opening the browser. Otherwise it emits the authorizationPageRequested() |
| 79 | * signal which must then be handled by the calling code. |
| 80 | */ |
| 81 | void setHandleAuthorizationPageOpening(bool set); |
| 82 | |
| 83 | /** |
| 84 | * Returns true if the KQOAuthManager has retrieved the oauth_token value. Otherwise |
| 85 | * return false. |
| 86 | */ |
| 87 | bool hasTemporaryToken(); |
| 88 | /** |
| 89 | * Returns true if the user has authorized us to use the protected resources. Otherwise |
| 90 | * returns false. |
| 91 | * NOTE: In order for KQOAuthManager to know if the user has authorized us to use the |
| 92 | * protected resources, KQOAuthManager must be in control of the user authorization |
| 93 | * process. Hence, this returns true if setHandleUserAuthorization() is set to true |
| 94 | * and the user is authorized with getUserAuthorization(). |
| 95 | */ |
| 96 | bool isVerified(); |
| 97 | /** |
| 98 | * Returns true if KQOAuthManager has the access token and hence can access the protected |
| 99 | * resources. Otherwise returns false. |
| 100 | * NOTE: In order for KQOAuthManager to know if we have access to protected resource |
| 101 | * KQOAuthManager must be in control of the user authorization process and requesting |
| 102 | * the acess token. Hence, this returns true if setHandleUserAuthorization() is set to true |
| 103 | * and the user is authorized with getUserAuthorization() and the access token must be retrieved |
| 104 | * with getUserAccessTokens. |
| 105 | */ |
| 106 | bool isAuthorized(); |
| 107 | |
| 108 | /** |
| 109 | * This is a convenience API for authorizing the user. |
| 110 | * The call will open the user's default browser, setup a local HTTP server and parse the reply from the |
| 111 | * service after the user has authorized us to access protected resources. If the user authorizes |
| 112 | * us to access protected resources, the verifier token is stored in KQOAuthManager for further use. |
| 113 | * In order to use this method, you must set setHandleUserAuthorization() to true. |
| 114 | */ |
| 115 | void getUserAuthorization(QUrl authorizationEndpoint); |
| 116 | /** |
| 117 | * This is a convenience API for retrieving the access token in exchange for the temporary token and the |
| 118 | * verifier. |
| 119 | * This call will create a KQOAuthRequest and use the previously stored temporary token and verifier to |
| 120 | * exchange for the access token, which will be used to access the protected resources. |
| 121 | * Note that in order to use this method, KQOAuthManager must be in control of the user authorization process. |
| 122 | * Set setHandleUserAuthorization() to true and retrieve user authorization with void getUserAuthorization. |
| 123 | */ |
| 124 | void getUserAccessTokens(QUrl accessTokenEndpoint); |
| 125 | /** |
| 126 | * This is a conveience API for setting the token verifier. |
| 127 | * If setHandleUserAuthorization() is set to false you need to call this function before calling |
| 128 | * getUserAccessTokens() |
| 129 | */ |
| 130 | void verifyToken(const QString &token, const QString &verifier); |
| 131 | /** |
| 132 | * Sends a request to the protected resources. Parameters for the request are service specific and |
| 133 | * are given to the 'requestParameters' as parameters. |
| 134 | * Note that in order to use this method, KQOAuthManager must be in control of the user authorization process. |
| 135 | * Set setHandleUserAuthorization() to true and retrieve user authorization with void getUserAuthorization. |
| 136 | */ |
| 137 | void sendAuthorizedRequest(QUrl requestEndpoint, const KQOAuthParameters &requestParameters); |
| 138 | |
| 139 | /** |
| 140 | * Sets a custom QNetworkAccessManager to handle network requests. This method can be useful if the |
| 141 | * application is using some proxy settings for example. |
| 142 | * The application is responsible for deleting this manager. KQOAuthManager will not delete any |
| 143 | * previously given manager. |
| 144 | * If the manager is NULL, the manager will not be set and the KQOAuthManager::Error. |
| 145 | * If no manager is given, KQOAuthManager will use the default one it will create by itself. |
| 146 | */ |
| 147 | void setNetworkManager(QNetworkAccessManager *manager); |
| 148 | |
| 149 | /** |
| 150 | * Returns the given QNetworkAccessManager. Returns NULL if none is given. |
| 151 | */ |
| 152 | QNetworkAccessManager* networkManager() const; |
| 153 | |
| 154 | Q_SIGNALS: |
| 155 | // This signal will be emitted after each request has got a reply. |
| 156 | // Parameter is the raw response from the service. |
| 157 | void requestReady(QByteArray networkReply); |
| 158 | |
| 159 | void authorizedRequestReady(QByteArray networkReply, int id); |
| 160 | |
| 161 | // This signal will be emitted when the authorization page should be opened if |
| 162 | // setHandleAuthorizationPageOpening() is set to false. |
| 163 | void authorizationPageRequested(QUrl pageUrl); |
| 164 | |
| 165 | // This signal will be emited when we have an request tokens available |
| 166 | // (either temporary resource tokens, or authorization tokens). |
| 167 | void receivedToken(QString oauth_token, QString oauth_token_secret); // oauth_token, oauth_token_secret |
| 168 | |
| 169 | // This signal is emited when temporary tokens are returned from the service. |
| 170 | // Note that this signal is also emited in case temporary tokens are not available. |
| 171 | void temporaryTokenReceived(QString oauth_token, QString oauth_token_secret); // oauth_token, oauth_token_secret |
| 172 | |
| 173 | // This signal is emited when the user has authenticated the application to |
| 174 | // communicate with the protected resources. Next we need to exchange the |
| 175 | // temporary tokens for access tokens. |
| 176 | // Note that this signal is also emited if user denies access. |
| 177 | void authorizationReceived(QString oauth_token, QString oauth_verifier); // oauth_token, oauth_verifier |
| 178 | |
| 179 | // This signal is emited when access tokens are received from the service. We are |
| 180 | // ready to start communicating with the protected resources. |
| 181 | void accessTokenReceived(QString oauth_token, QString oauth_token_secret); // oauth_token, oauth_token_secret |
| 182 | |
| 183 | // This signal is emited when the authorized request is done. |
| 184 | // This ends the kQOAuth interactions. |
| 185 | void authorizedRequestDone(); |
| 186 | |
| 187 | private Q_SLOTS: |
| 188 | void onRequestReplyReceived( QNetworkReply *reply ); |
| 189 | void onAuthorizedRequestReplyReceived( QNetworkReply *reply ); |
| 190 | void onVerificationReceived(QMultiMap<QString, QString> response); |
| 191 | void slotError(QNetworkReply::NetworkError error); |
| 192 | void requestTimeout(); |
| 193 | |
| 194 | private: |
| 195 | KQOAuthManagerPrivate *d_ptr; |
| 196 | Q_DECLARE_PRIVATE(KQOAuthManager); |
| 197 | Q_DISABLE_COPY(KQOAuthManager); |
| 198 | |
| 199 | }; |
| 200 | |
| 201 | #endif // KQOAUTHMANAGER_H |