PageRenderTime 25ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/thirdparty/kQOAuth/kqoauthmanager.h

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