| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/chromeos/arc/arc_auth_code_fetcher.h" | 5 #include "chrome/browser/chromeos/arc/auth/arc_background_auth_code_fetcher.h" |
| 6 |
| 7 #include <utility> |
| 6 | 8 |
| 7 #include "base/bind.h" | 9 #include "base/bind.h" |
| 8 #include "base/callback_helpers.h" | 10 #include "base/callback_helpers.h" |
| 9 #include "base/json/json_string_value_serializer.h" | 11 #include "base/json/json_string_value_serializer.h" |
| 10 #include "base/json/json_writer.h" | 12 #include "base/json/json_writer.h" |
| 11 #include "base/logging.h" | 13 #include "base/logging.h" |
| 12 #include "base/values.h" | 14 #include "base/values.h" |
| 13 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | 15 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" |
| 14 #include "chrome/browser/signin/signin_manager_factory.h" | 16 #include "chrome/browser/signin/signin_manager_factory.h" |
| 15 #include "chrome/browser/ui/ash/multi_user/multi_user_util.h" | 17 #include "chrome/browser/ui/ash/multi_user/multi_user_util.h" |
| (...skipping 22 matching lines...) Expand all Loading... |
| 38 constexpr char kDeviceTypeArc[] = "arc_plus_plus"; | 40 constexpr char kDeviceTypeArc[] = "arc_plus_plus"; |
| 39 constexpr char kLoginScopedToken[] = "login_scoped_token"; | 41 constexpr char kLoginScopedToken[] = "login_scoped_token"; |
| 40 constexpr char kGetAuthCodeHeaders[] = | 42 constexpr char kGetAuthCodeHeaders[] = |
| 41 "Content-Type: application/json; charset=utf-8"; | 43 "Content-Type: application/json; charset=utf-8"; |
| 42 constexpr char kContentTypeJSON[] = "application/json"; | 44 constexpr char kContentTypeJSON[] = "application/json"; |
| 43 constexpr char kEndPoint[] = | 45 constexpr char kEndPoint[] = |
| 44 "https://www.googleapis.com/oauth2/v4/ExchangeToken"; | 46 "https://www.googleapis.com/oauth2/v4/ExchangeToken"; |
| 45 | 47 |
| 46 } // namespace | 48 } // namespace |
| 47 | 49 |
| 48 ArcAuthCodeFetcher::ArcAuthCodeFetcher(Profile* profile, | 50 ArcBackgroundAuthCodeFetcher::ArcBackgroundAuthCodeFetcher( |
| 49 ArcAuthContext* context) | 51 Profile* profile, |
| 52 ArcAuthContext* context) |
| 50 : OAuth2TokenService::Consumer(kConsumerName), | 53 : OAuth2TokenService::Consumer(kConsumerName), |
| 51 profile_(profile), | 54 profile_(profile), |
| 52 context_(context), | 55 context_(context), |
| 53 weak_ptr_factory_(this) {} | 56 weak_ptr_factory_(this) {} |
| 54 | 57 |
| 55 ArcAuthCodeFetcher::~ArcAuthCodeFetcher() = default; | 58 ArcBackgroundAuthCodeFetcher::~ArcBackgroundAuthCodeFetcher() = default; |
| 56 | 59 |
| 57 void ArcAuthCodeFetcher::Fetch(const FetchCallback& callback) { | 60 void ArcBackgroundAuthCodeFetcher::Fetch(const FetchCallback& callback) { |
| 58 DCHECK(callback_.is_null()); | 61 DCHECK(callback_.is_null()); |
| 59 callback_ = callback; | 62 callback_ = callback; |
| 60 | 63 |
| 61 context_->Prepare(base::Bind(&ArcAuthCodeFetcher::OnPrepared, | 64 context_->Prepare(base::Bind(&ArcBackgroundAuthCodeFetcher::OnPrepared, |
| 62 weak_ptr_factory_.GetWeakPtr())); | 65 weak_ptr_factory_.GetWeakPtr())); |
| 63 } | 66 } |
| 64 | 67 |
| 65 void ArcAuthCodeFetcher::OnPrepared( | 68 void ArcBackgroundAuthCodeFetcher::OnPrepared( |
| 66 net::URLRequestContextGetter* request_context_getter) { | 69 net::URLRequestContextGetter* request_context_getter) { |
| 67 if (!request_context_getter) { | 70 if (!request_context_getter) { |
| 68 base::ResetAndReturn(&callback_).Run(std::string()); | 71 base::ResetAndReturn(&callback_).Run(std::string()); |
| 69 return; | 72 return; |
| 70 } | 73 } |
| 71 | 74 |
| 72 DCHECK(!request_context_getter_); | 75 DCHECK(!request_context_getter_); |
| 73 request_context_getter_ = request_context_getter; | 76 request_context_getter_ = request_context_getter; |
| 74 | 77 |
| 75 // Get token service and account ID to fetch auth tokens. | 78 // Get token service and account ID to fetch auth tokens. |
| 76 ProfileOAuth2TokenService* const token_service = context_->token_service(); | 79 ProfileOAuth2TokenService* const token_service = context_->token_service(); |
| 77 const std::string& account_id = context_->account_id(); | 80 const std::string& account_id = context_->account_id(); |
| 78 DCHECK(token_service->RefreshTokenIsAvailable(account_id)); | 81 DCHECK(token_service->RefreshTokenIsAvailable(account_id)); |
| 79 | 82 |
| 80 OAuth2TokenService::ScopeSet scopes; | 83 OAuth2TokenService::ScopeSet scopes; |
| 81 scopes.insert(GaiaConstants::kOAuth1LoginScope); | 84 scopes.insert(GaiaConstants::kOAuth1LoginScope); |
| 82 login_token_request_ = token_service->StartRequest(account_id, scopes, this); | 85 login_token_request_ = token_service->StartRequest(account_id, scopes, this); |
| 83 } | 86 } |
| 84 | 87 |
| 85 void ArcAuthCodeFetcher::OnGetTokenSuccess( | 88 void ArcBackgroundAuthCodeFetcher::OnGetTokenSuccess( |
| 86 const OAuth2TokenService::Request* request, | 89 const OAuth2TokenService::Request* request, |
| 87 const std::string& access_token, | 90 const std::string& access_token, |
| 88 const base::Time& expiration_time) { | 91 const base::Time& expiration_time) { |
| 89 ResetFetchers(); | 92 ResetFetchers(); |
| 90 | 93 |
| 91 const std::string device_id = user_manager::known_user::GetDeviceId( | 94 const std::string device_id = user_manager::known_user::GetDeviceId( |
| 92 multi_user_util::GetAccountIdFromProfile(profile_)); | 95 multi_user_util::GetAccountIdFromProfile(profile_)); |
| 93 DCHECK(!device_id.empty()); | 96 DCHECK(!device_id.empty()); |
| 94 | 97 |
| 95 base::DictionaryValue request_data; | 98 base::DictionaryValue request_data; |
| 96 request_data.SetString(kLoginScopedToken, access_token); | 99 request_data.SetString(kLoginScopedToken, access_token); |
| 97 request_data.SetString(kDeviceType, kDeviceTypeArc); | 100 request_data.SetString(kDeviceType, kDeviceTypeArc); |
| 98 request_data.SetString(kDeviceId, device_id); | 101 request_data.SetString(kDeviceId, device_id); |
| 99 std::string request_string; | 102 std::string request_string; |
| 100 base::JSONWriter::Write(request_data, &request_string); | 103 base::JSONWriter::Write(request_data, &request_string); |
| 101 | 104 |
| 102 auth_code_fetcher_ = | 105 auth_code_fetcher_ = |
| 103 net::URLFetcher::Create(0, GURL(kEndPoint), net::URLFetcher::POST, this); | 106 net::URLFetcher::Create(0, GURL(kEndPoint), net::URLFetcher::POST, this); |
| 104 auth_code_fetcher_->SetRequestContext(request_context_getter_); | 107 auth_code_fetcher_->SetRequestContext(request_context_getter_); |
| 105 auth_code_fetcher_->SetUploadData(kContentTypeJSON, request_string); | 108 auth_code_fetcher_->SetUploadData(kContentTypeJSON, request_string); |
| 106 auth_code_fetcher_->SetLoadFlags(net::LOAD_DISABLE_CACHE | | 109 auth_code_fetcher_->SetLoadFlags(net::LOAD_DISABLE_CACHE | |
| 107 net::LOAD_BYPASS_CACHE); | 110 net::LOAD_BYPASS_CACHE); |
| 108 auth_code_fetcher_->SetAutomaticallyRetryOnNetworkChanges( | 111 auth_code_fetcher_->SetAutomaticallyRetryOnNetworkChanges( |
| 109 kGetAuthCodeNetworkRetry); | 112 kGetAuthCodeNetworkRetry); |
| 110 auth_code_fetcher_->SetExtraRequestHeaders(kGetAuthCodeHeaders); | 113 auth_code_fetcher_->SetExtraRequestHeaders(kGetAuthCodeHeaders); |
| 111 auth_code_fetcher_->Start(); | 114 auth_code_fetcher_->Start(); |
| 112 } | 115 } |
| 113 | 116 |
| 114 void ArcAuthCodeFetcher::OnGetTokenFailure( | 117 void ArcBackgroundAuthCodeFetcher::OnGetTokenFailure( |
| 115 const OAuth2TokenService::Request* request, | 118 const OAuth2TokenService::Request* request, |
| 116 const GoogleServiceAuthError& error) { | 119 const GoogleServiceAuthError& error) { |
| 117 VLOG(2) << "Failed to get LST " << error.ToString() << "."; | 120 VLOG(2) << "Failed to get LST " << error.ToString() << "."; |
| 118 ResetFetchers(); | 121 ResetFetchers(); |
| 119 base::ResetAndReturn(&callback_).Run(std::string()); | 122 base::ResetAndReturn(&callback_).Run(std::string()); |
| 120 } | 123 } |
| 121 | 124 |
| 122 void ArcAuthCodeFetcher::OnURLFetchComplete(const net::URLFetcher* source) { | 125 void ArcBackgroundAuthCodeFetcher::OnURLFetchComplete( |
| 126 const net::URLFetcher* source) { |
| 123 const int response_code = source->GetResponseCode(); | 127 const int response_code = source->GetResponseCode(); |
| 124 std::string json_string; | 128 std::string json_string; |
| 125 source->GetResponseAsString(&json_string); | 129 source->GetResponseAsString(&json_string); |
| 126 | 130 |
| 127 ResetFetchers(); | 131 ResetFetchers(); |
| 128 | 132 |
| 129 if (response_code != net::HTTP_OK) { | 133 if (response_code != net::HTTP_OK) { |
| 130 VLOG(2) << "Server returned wrong response code: " << response_code << "."; | 134 VLOG(2) << "Server returned wrong response code: " << response_code << "."; |
| 131 base::ResetAndReturn(&callback_).Run(std::string()); | 135 base::ResetAndReturn(&callback_).Run(std::string()); |
| 132 return; | 136 return; |
| (...skipping 22 matching lines...) Expand all Loading... |
| 155 if (!auth_code_dictionary->GetString(kToken, &auth_code) || | 159 if (!auth_code_dictionary->GetString(kToken, &auth_code) || |
| 156 auth_code.empty()) { | 160 auth_code.empty()) { |
| 157 VLOG(2) << "Response does not contain auth code."; | 161 VLOG(2) << "Response does not contain auth code."; |
| 158 base::ResetAndReturn(&callback_).Run(std::string()); | 162 base::ResetAndReturn(&callback_).Run(std::string()); |
| 159 return; | 163 return; |
| 160 } | 164 } |
| 161 | 165 |
| 162 base::ResetAndReturn(&callback_).Run(auth_code); | 166 base::ResetAndReturn(&callback_).Run(auth_code); |
| 163 } | 167 } |
| 164 | 168 |
| 165 void ArcAuthCodeFetcher::ResetFetchers() { | 169 void ArcBackgroundAuthCodeFetcher::ResetFetchers() { |
| 166 login_token_request_.reset(); | 170 login_token_request_.reset(); |
| 167 auth_code_fetcher_.reset(); | 171 auth_code_fetcher_.reset(); |
| 168 } | 172 } |
| 169 | 173 |
| 170 } // namespace arc | 174 } // namespace arc |
| OLD | NEW |