| OLD | NEW |
| (Empty) |
| 1 // Copyright 2016 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 | |
| 5 #include "chrome/browser/chromeos/arc/arc_auth_code_fetcher.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/callback_helpers.h" | |
| 9 #include "base/json/json_string_value_serializer.h" | |
| 10 #include "base/json/json_writer.h" | |
| 11 #include "base/logging.h" | |
| 12 #include "base/values.h" | |
| 13 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | |
| 14 #include "chrome/browser/signin/signin_manager_factory.h" | |
| 15 #include "chrome/browser/ui/ash/multi_user/multi_user_util.h" | |
| 16 #include "components/signin/core/account_id/account_id.h" | |
| 17 #include "components/signin/core/browser/profile_oauth2_token_service.h" | |
| 18 #include "components/signin/core/browser/signin_manager_base.h" | |
| 19 #include "components/user_manager/known_user.h" | |
| 20 #include "content/public/browser/browser_context.h" | |
| 21 #include "content/public/common/url_constants.h" | |
| 22 #include "google_apis/gaia/gaia_auth_fetcher.h" | |
| 23 #include "google_apis/gaia/gaia_constants.h" | |
| 24 #include "net/base/load_flags.h" | |
| 25 #include "net/http/http_status_code.h" | |
| 26 #include "net/url_request/url_fetcher.h" | |
| 27 | |
| 28 namespace arc { | |
| 29 | |
| 30 namespace { | |
| 31 | |
| 32 constexpr int kGetAuthCodeNetworkRetry = 3; | |
| 33 | |
| 34 constexpr char kConsumerName[] = "ArcAuthContext"; | |
| 35 constexpr char kToken[] = "token"; | |
| 36 constexpr char kDeviceId[] = "device_id"; | |
| 37 constexpr char kDeviceType[] = "device_type"; | |
| 38 constexpr char kDeviceTypeArc[] = "arc_plus_plus"; | |
| 39 constexpr char kLoginScopedToken[] = "login_scoped_token"; | |
| 40 constexpr char kGetAuthCodeHeaders[] = | |
| 41 "Content-Type: application/json; charset=utf-8"; | |
| 42 constexpr char kContentTypeJSON[] = "application/json"; | |
| 43 constexpr char kEndPoint[] = | |
| 44 "https://www.googleapis.com/oauth2/v4/ExchangeToken"; | |
| 45 | |
| 46 } // namespace | |
| 47 | |
| 48 ArcAuthCodeFetcher::ArcAuthCodeFetcher(Profile* profile, | |
| 49 ArcAuthContext* context) | |
| 50 : OAuth2TokenService::Consumer(kConsumerName), | |
| 51 profile_(profile), | |
| 52 context_(context), | |
| 53 weak_ptr_factory_(this) {} | |
| 54 | |
| 55 ArcAuthCodeFetcher::~ArcAuthCodeFetcher() = default; | |
| 56 | |
| 57 void ArcAuthCodeFetcher::Fetch(const FetchCallback& callback) { | |
| 58 DCHECK(callback_.is_null()); | |
| 59 callback_ = callback; | |
| 60 | |
| 61 context_->Prepare(base::Bind(&ArcAuthCodeFetcher::OnPrepared, | |
| 62 weak_ptr_factory_.GetWeakPtr())); | |
| 63 } | |
| 64 | |
| 65 void ArcAuthCodeFetcher::OnPrepared( | |
| 66 net::URLRequestContextGetter* request_context_getter) { | |
| 67 if (!request_context_getter) { | |
| 68 base::ResetAndReturn(&callback_).Run(std::string()); | |
| 69 return; | |
| 70 } | |
| 71 | |
| 72 DCHECK(!request_context_getter_); | |
| 73 request_context_getter_ = request_context_getter; | |
| 74 | |
| 75 // Get token service and account ID to fetch auth tokens. | |
| 76 ProfileOAuth2TokenService* const token_service = context_->token_service(); | |
| 77 const std::string& account_id = context_->account_id(); | |
| 78 DCHECK(token_service->RefreshTokenIsAvailable(account_id)); | |
| 79 | |
| 80 OAuth2TokenService::ScopeSet scopes; | |
| 81 scopes.insert(GaiaConstants::kOAuth1LoginScope); | |
| 82 login_token_request_ = token_service->StartRequest(account_id, scopes, this); | |
| 83 } | |
| 84 | |
| 85 void ArcAuthCodeFetcher::OnGetTokenSuccess( | |
| 86 const OAuth2TokenService::Request* request, | |
| 87 const std::string& access_token, | |
| 88 const base::Time& expiration_time) { | |
| 89 ResetFetchers(); | |
| 90 | |
| 91 const std::string device_id = user_manager::known_user::GetDeviceId( | |
| 92 multi_user_util::GetAccountIdFromProfile(profile_)); | |
| 93 DCHECK(!device_id.empty()); | |
| 94 | |
| 95 base::DictionaryValue request_data; | |
| 96 request_data.SetString(kLoginScopedToken, access_token); | |
| 97 request_data.SetString(kDeviceType, kDeviceTypeArc); | |
| 98 request_data.SetString(kDeviceId, device_id); | |
| 99 std::string request_string; | |
| 100 base::JSONWriter::Write(request_data, &request_string); | |
| 101 | |
| 102 auth_code_fetcher_ = | |
| 103 net::URLFetcher::Create(0, GURL(kEndPoint), net::URLFetcher::POST, this); | |
| 104 auth_code_fetcher_->SetRequestContext(request_context_getter_); | |
| 105 auth_code_fetcher_->SetUploadData(kContentTypeJSON, request_string); | |
| 106 auth_code_fetcher_->SetLoadFlags(net::LOAD_DISABLE_CACHE | | |
| 107 net::LOAD_BYPASS_CACHE); | |
| 108 auth_code_fetcher_->SetAutomaticallyRetryOnNetworkChanges( | |
| 109 kGetAuthCodeNetworkRetry); | |
| 110 auth_code_fetcher_->SetExtraRequestHeaders(kGetAuthCodeHeaders); | |
| 111 auth_code_fetcher_->Start(); | |
| 112 } | |
| 113 | |
| 114 void ArcAuthCodeFetcher::OnGetTokenFailure( | |
| 115 const OAuth2TokenService::Request* request, | |
| 116 const GoogleServiceAuthError& error) { | |
| 117 VLOG(2) << "Failed to get LST " << error.ToString() << "."; | |
| 118 ResetFetchers(); | |
| 119 base::ResetAndReturn(&callback_).Run(std::string()); | |
| 120 } | |
| 121 | |
| 122 void ArcAuthCodeFetcher::OnURLFetchComplete(const net::URLFetcher* source) { | |
| 123 const int response_code = source->GetResponseCode(); | |
| 124 std::string json_string; | |
| 125 source->GetResponseAsString(&json_string); | |
| 126 | |
| 127 ResetFetchers(); | |
| 128 | |
| 129 if (response_code != net::HTTP_OK) { | |
| 130 VLOG(2) << "Server returned wrong response code: " << response_code << "."; | |
| 131 base::ResetAndReturn(&callback_).Run(std::string()); | |
| 132 return; | |
| 133 } | |
| 134 | |
| 135 JSONStringValueDeserializer deserializer(json_string); | |
| 136 std::string error_msg; | |
| 137 std::unique_ptr<base::Value> auth_code_info = | |
| 138 deserializer.Deserialize(nullptr, &error_msg); | |
| 139 if (!auth_code_info) { | |
| 140 VLOG(2) << "Unable to deserialize auth code json data: " << error_msg | |
| 141 << "."; | |
| 142 base::ResetAndReturn(&callback_).Run(std::string()); | |
| 143 return; | |
| 144 } | |
| 145 | |
| 146 std::unique_ptr<base::DictionaryValue> auth_code_dictionary = | |
| 147 base::DictionaryValue::From(std::move(auth_code_info)); | |
| 148 if (!auth_code_dictionary) { | |
| 149 NOTREACHED(); | |
| 150 base::ResetAndReturn(&callback_).Run(std::string()); | |
| 151 return; | |
| 152 } | |
| 153 | |
| 154 std::string auth_code; | |
| 155 if (!auth_code_dictionary->GetString(kToken, &auth_code) || | |
| 156 auth_code.empty()) { | |
| 157 VLOG(2) << "Response does not contain auth code."; | |
| 158 base::ResetAndReturn(&callback_).Run(std::string()); | |
| 159 return; | |
| 160 } | |
| 161 | |
| 162 base::ResetAndReturn(&callback_).Run(auth_code); | |
| 163 } | |
| 164 | |
| 165 void ArcAuthCodeFetcher::ResetFetchers() { | |
| 166 login_token_request_.reset(); | |
| 167 auth_code_fetcher_.reset(); | |
| 168 } | |
| 169 | |
| 170 } // namespace arc | |
| OLD | NEW |