PageRenderTime 70ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/deprecated/kqoauth/kqoauthmanager.h

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