oauth_http_fetcher.cc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // Copyright 2021 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. #include "ash/quick_pair/repository/oauth_http_fetcher.h"
  5. #include "ash/quick_pair/common/fast_pair/fast_pair_http_result.h"
  6. #include "ash/quick_pair/common/logging.h"
  7. #include "ash/quick_pair/common/quick_pair_browser_delegate.h"
  8. #include "components/signin/public/base/consent_level.h"
  9. #include "components/signin/public/identity_manager/access_token_info.h"
  10. #include "components/signin/public/identity_manager/identity_manager.h"
  11. #include "components/signin/public/identity_manager/primary_account_access_token_fetcher.h"
  12. #include "google_apis/gaia/gaia_constants.h"
  13. #include "net/base/net_errors.h"
  14. #include "services/network/public/cpp/resource_request.h"
  15. #include "services/network/public/cpp/shared_url_loader_factory.h"
  16. #include "services/network/public/cpp/simple_url_loader.h"
  17. #include "services/network/public/mojom/url_response_head.mojom.h"
  18. namespace ash {
  19. namespace quick_pair {
  20. OAuthHttpFetcher::OAuthHttpFetcher(
  21. const net::PartialNetworkTrafficAnnotationTag& traffic_annotation,
  22. const std::string& oauth_scope)
  23. : traffic_annotation_(traffic_annotation) {
  24. oauth_scopes_.insert(oauth_scope);
  25. }
  26. OAuthHttpFetcher::~OAuthHttpFetcher() = default;
  27. void OAuthHttpFetcher::ExecuteGetRequest(const GURL& url,
  28. FetchCompleteCallback callback) {
  29. request_type_ = RequestType::GET;
  30. StartRequest(url, std::move(callback));
  31. }
  32. void OAuthHttpFetcher::ExecutePostRequest(const GURL& url,
  33. const std::string& body,
  34. FetchCompleteCallback callback) {
  35. request_type_ = RequestType::POST;
  36. body_ = body;
  37. StartRequest(url, std::move(callback));
  38. }
  39. void OAuthHttpFetcher::ExecuteDeleteRequest(const GURL& url,
  40. FetchCompleteCallback callback) {
  41. request_type_ = RequestType::DELETE;
  42. StartRequest(url, std::move(callback));
  43. }
  44. void OAuthHttpFetcher::StartRequest(const GURL& url,
  45. FetchCompleteCallback callback) {
  46. QP_LOG(VERBOSE) << __func__ << ": executing request to: " << url;
  47. if (has_call_started_) {
  48. QP_LOG(ERROR) << __func__
  49. << ": Attempted to make an API call, but there is already a "
  50. "request in progress.";
  51. NOTREACHED();
  52. return;
  53. }
  54. signin::IdentityManager* identity_manager =
  55. QuickPairBrowserDelegate::Get()->GetIdentityManager();
  56. if (!identity_manager) {
  57. QP_LOG(ERROR) << __func__ << ": IdentityManager is not available.";
  58. NOTREACHED();
  59. return;
  60. }
  61. has_call_started_ = true;
  62. url_ = url;
  63. callback_ = std::move(callback);
  64. access_token_fetcher_ =
  65. std::make_unique<signin::PrimaryAccountAccessTokenFetcher>(
  66. "fastpair_client", identity_manager, oauth_scopes_,
  67. base::BindOnce(&OAuthHttpFetcher::OnAccessTokenFetched,
  68. weak_ptr_factory_.GetWeakPtr()),
  69. signin::PrimaryAccountAccessTokenFetcher::Mode::kImmediate,
  70. signin::ConsentLevel::kSignin);
  71. }
  72. void OAuthHttpFetcher::OnAccessTokenFetched(
  73. GoogleServiceAuthError error,
  74. signin::AccessTokenInfo access_token_info) {
  75. access_token_fetcher_.reset();
  76. if (error.state() != GoogleServiceAuthError::NONE) {
  77. QP_LOG(WARNING) << __func__ << ": Failed to retrieve access token. "
  78. << error.ToString();
  79. std::move(callback_).Run(nullptr, nullptr);
  80. return;
  81. }
  82. scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory =
  83. QuickPairBrowserDelegate::Get()->GetURLLoaderFactory();
  84. if (!url_loader_factory) {
  85. QP_LOG(WARNING) << __func__ << ": URLLoaderFactory is not available.";
  86. std::move(callback_).Run(nullptr, nullptr);
  87. return;
  88. }
  89. QP_LOG(VERBOSE) << "Access token fetched successfully.";
  90. OAuth2ApiCallFlow::Start(std::move(url_loader_factory),
  91. access_token_info.token);
  92. }
  93. GURL OAuthHttpFetcher::CreateApiCallUrl() {
  94. return url_;
  95. }
  96. std::string OAuthHttpFetcher::CreateApiCallBody() {
  97. switch (request_type_) {
  98. case RequestType::GET:
  99. case RequestType::DELETE:
  100. return std::string();
  101. case RequestType::POST:
  102. return body_;
  103. }
  104. }
  105. std::string OAuthHttpFetcher::CreateApiCallBodyContentType() {
  106. switch (request_type_) {
  107. case RequestType::GET:
  108. case RequestType::DELETE:
  109. return std::string();
  110. case RequestType::POST:
  111. return "application/x-protobuf";
  112. }
  113. }
  114. std::string OAuthHttpFetcher::GetRequestTypeForBody(const std::string& body) {
  115. switch (request_type_) {
  116. case RequestType::GET:
  117. return "GET";
  118. case RequestType::POST:
  119. return "POST";
  120. case RequestType::DELETE:
  121. return "DELETE";
  122. }
  123. }
  124. void OAuthHttpFetcher::ProcessApiCallSuccess(
  125. const network::mojom::URLResponseHead* head,
  126. std::unique_ptr<std::string> body) {
  127. QP_LOG(INFO) << __func__;
  128. std::move(callback_).Run(
  129. std::move(body),
  130. std::make_unique<FastPairHttpResult>(/*net_error=*/net::OK,
  131. /*head=*/head));
  132. }
  133. void OAuthHttpFetcher::ProcessApiCallFailure(
  134. int net_error,
  135. const network::mojom::URLResponseHead* head,
  136. std::unique_ptr<std::string> body) {
  137. QP_LOG(WARNING) << __func__ << ": net_err=" << net_error;
  138. std::move(callback_).Run(
  139. nullptr, std::make_unique<FastPairHttpResult>(/*net_error=*/net_error,
  140. /*head=*/head));
  141. }
  142. net::PartialNetworkTrafficAnnotationTag
  143. OAuthHttpFetcher::GetNetworkTrafficAnnotationTag() {
  144. return traffic_annotation_;
  145. }
  146. } // namespace quick_pair
  147. } // namespace ash