gaia_oauth_client.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef GOOGLE_APIS_GAIA_GAIA_OAUTH_CLIENT_H_
  5. #define GOOGLE_APIS_GAIA_GAIA_OAUTH_CLIENT_H_
  6. #include <memory>
  7. #include <string>
  8. #include <vector>
  9. #include "base/memory/ref_counted.h"
  10. #include "base/values.h"
  11. namespace network {
  12. class SharedURLLoaderFactory;
  13. }
  14. // A helper class to get and refresh OAuth2 refresh and access tokens.
  15. // Also exposes utility methods for fetching user email and token information.
  16. //
  17. // Supports one request at a time; for parallel requests, create multiple
  18. // instances.
  19. namespace gaia {
  20. struct OAuthClientInfo {
  21. std::string client_id;
  22. std::string client_secret;
  23. std::string redirect_uri;
  24. };
  25. class GaiaOAuthClient {
  26. public:
  27. class Delegate {
  28. public:
  29. // Invoked on a successful response to the GetTokensFromAuthCode request.
  30. virtual void OnGetTokensResponse(const std::string& refresh_token,
  31. const std::string& access_token,
  32. int expires_in_seconds) {}
  33. // Invoked on a successful response to the RefreshToken request.
  34. virtual void OnRefreshTokenResponse(const std::string& access_token,
  35. int expires_in_seconds) {}
  36. // Invoked on a successful response to the GetUserInfo request.
  37. virtual void OnGetUserEmailResponse(const std::string& user_email) {}
  38. // Invoked on a successful response to the GetUserId request.
  39. virtual void OnGetUserIdResponse(const std::string& user_id) {}
  40. // Invoked on a successful response to the GetUserInfo request.
  41. virtual void OnGetUserInfoResponse(
  42. std::unique_ptr<base::DictionaryValue> user_info) {}
  43. // Invoked on a successful response to the GetTokenInfo request.
  44. virtual void OnGetTokenInfoResponse(
  45. std::unique_ptr<base::DictionaryValue> token_info) {}
  46. virtual void OnGetAccountCapabilitiesResponse(
  47. std::unique_ptr<base::Value> account_capabilities) {}
  48. // Invoked when there is an OAuth error with one of the requests.
  49. virtual void OnOAuthError() = 0;
  50. // Invoked when there is a network error or upon receiving an invalid
  51. // response. This is invoked when the maximum number of retries have been
  52. // exhausted. If max_retries is -1, this is never invoked.
  53. virtual void OnNetworkError(int response_code) = 0;
  54. protected:
  55. virtual ~Delegate() {}
  56. };
  57. GaiaOAuthClient(
  58. scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory);
  59. GaiaOAuthClient(const GaiaOAuthClient&) = delete;
  60. GaiaOAuthClient& operator=(const GaiaOAuthClient&) = delete;
  61. ~GaiaOAuthClient();
  62. // In the below methods, |max_retries| specifies the maximum number of times
  63. // we should retry on a network error in invalid response. This does not
  64. // apply in the case of an OAuth error (i.e. there was something wrong with
  65. // the input arguments). Setting |max_retries| to -1 implies infinite retries.
  66. // Given an OAuth2 authorization code, fetch the long-lived refresh token
  67. // and a valid access token. After the access token expires, RefreshToken()
  68. // can be used to fetch a fresh access token. See |max_retries| docs above.
  69. void GetTokensFromAuthCode(const OAuthClientInfo& oauth_client_info,
  70. const std::string& auth_code,
  71. int max_retries,
  72. Delegate* delegate);
  73. // Given a valid refresh token (usually fetched via
  74. // |GetTokensFromAuthCode()|), fetch a fresh access token that can be used
  75. // to authenticate an API call. If |scopes| is non-empty, then fetch an
  76. // access token for those specific scopes (assuming the refresh token has the
  77. // appropriate permissions). See |max_retries| docs above.
  78. void RefreshToken(const OAuthClientInfo& oauth_client_info,
  79. const std::string& refresh_token,
  80. const std::vector<std::string>& scopes,
  81. int max_retries,
  82. Delegate* delegate);
  83. // Call the userinfo API, returning the user email address associated
  84. // with the given access token. The provided access token must have
  85. // https://www.googleapis.com/auth/userinfo.email as one of its scopes.
  86. // See |max_retries| docs above.
  87. void GetUserEmail(const std::string& oauth_access_token,
  88. int max_retries,
  89. Delegate* delegate);
  90. // Call the userinfo API, returning the user gaia ID associated
  91. // with the given access token. The provided access token must have
  92. // https://www.googleapis.com/auth/userinfo as one of its scopes.
  93. // See |max_retries| docs above.
  94. void GetUserId(const std::string& oauth_access_token,
  95. int max_retries,
  96. Delegate* delegate);
  97. // Call the userinfo API, returning all the user info associated
  98. // with the given access token. The provided access token must have
  99. // https://www.googleapis.com/auth/userinfo.profile in its scopes. If
  100. // email addresses are also to be retrieved, then
  101. // https://www.googleapis.com/auth/userinfo.email must also be specified.
  102. // See |max_retries| docs above.
  103. void GetUserInfo(const std::string& oauth_access_token,
  104. int max_retries,
  105. Delegate* delegate);
  106. // Call the tokeninfo API, returning a dictionary of response values. The
  107. // provided access token may have any scope, and basic results will be
  108. // returned: issued_to, audience, scope, expires_in, access_type. In
  109. // addition, if the https://www.googleapis.com/auth/userinfo.email scope is
  110. // present, the email and verified_email fields will be returned. If the
  111. // https://www.googleapis.com/auth/userinfo.profile scope is present, the
  112. // user_id field will be returned. See |max_retries| docs above.
  113. void GetTokenInfo(const std::string& oauth_access_token,
  114. int max_retries,
  115. Delegate* delegate);
  116. // Call the tokeninfo API for given |token_handle|, returning a dictionary of
  117. // response values. Basic results will be returned via
  118. // |OnGetTokenInfoResponse| call: audience, expires_in, user_id. See
  119. // |max_retries| docs above.
  120. void GetTokenHandleInfo(const std::string& token_handle,
  121. int max_retries,
  122. Delegate* delegate);
  123. // Call the account capabilities API, returning a dictionary of response
  124. // values. Only fetches values for capabilities listed in
  125. // |capabilities_names|. The provided access token must have
  126. // https://www.googleapis.com/auth/account.capabilities in its scopes. See
  127. // |max_retries| docs above.
  128. void GetAccountCapabilities(
  129. const std::string& oauth_access_token,
  130. const std::vector<std::string>& capabilities_names,
  131. int max_retries,
  132. Delegate* delegate);
  133. private:
  134. // The guts of the implementation live in this class.
  135. class Core;
  136. scoped_refptr<Core> core_;
  137. };
  138. }
  139. #endif // GOOGLE_APIS_GAIA_GAIA_OAUTH_CLIENT_H_