Chromium Code Reviews| 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/arc_auth_code_fetcher.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | |
| 8 #include "base/callback_helpers.h" | |
| 7 #include "base/json/json_string_value_serializer.h" | 9 #include "base/json/json_string_value_serializer.h" |
| 8 #include "base/json/json_writer.h" | 10 #include "base/json/json_writer.h" |
| 11 #include "base/logging.h" | |
| 9 #include "base/values.h" | 12 #include "base/values.h" |
| 10 #include "chrome/browser/chromeos/arc/arc_auth_code_fetcher_delegate.h" | |
| 11 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | 13 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" |
| 12 #include "chrome/browser/signin/signin_manager_factory.h" | 14 #include "chrome/browser/signin/signin_manager_factory.h" |
| 13 #include "chrome/browser/ui/ash/multi_user/multi_user_util.h" | 15 #include "chrome/browser/ui/ash/multi_user/multi_user_util.h" |
| 14 #include "components/signin/core/account_id/account_id.h" | 16 #include "components/signin/core/account_id/account_id.h" |
| 15 #include "components/signin/core/browser/profile_oauth2_token_service.h" | 17 #include "components/signin/core/browser/profile_oauth2_token_service.h" |
| 16 #include "components/signin/core/browser/signin_manager_base.h" | 18 #include "components/signin/core/browser/signin_manager_base.h" |
| 17 #include "components/user_manager/known_user.h" | 19 #include "components/user_manager/known_user.h" |
| 18 #include "content/public/browser/browser_context.h" | 20 #include "content/public/browser/browser_context.h" |
| 19 #include "content/public/common/url_constants.h" | 21 #include "content/public/common/url_constants.h" |
| 20 #include "google_apis/gaia/gaia_auth_fetcher.h" | 22 #include "google_apis/gaia/gaia_auth_fetcher.h" |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 34 constexpr char kDeviceId[] = "device_id"; | 36 constexpr char kDeviceId[] = "device_id"; |
| 35 constexpr char kDeviceType[] = "device_type"; | 37 constexpr char kDeviceType[] = "device_type"; |
| 36 constexpr char kDeviceTypeArc[] = "arc_plus_plus"; | 38 constexpr char kDeviceTypeArc[] = "arc_plus_plus"; |
| 37 constexpr char kLoginScopedToken[] = "login_scoped_token"; | 39 constexpr char kLoginScopedToken[] = "login_scoped_token"; |
| 38 constexpr char kGetAuthCodeHeaders[] = | 40 constexpr char kGetAuthCodeHeaders[] = |
| 39 "Content-Type: application/json; charset=utf-8"; | 41 "Content-Type: application/json; charset=utf-8"; |
| 40 constexpr char kContentTypeJSON[] = "application/json"; | 42 constexpr char kContentTypeJSON[] = "application/json"; |
| 41 | 43 |
| 42 } // namespace | 44 } // namespace |
| 43 | 45 |
| 44 ArcAuthCodeFetcher::ArcAuthCodeFetcher( | 46 ArcAuthCodeFetcher::ArcAuthCodeFetcher(Profile* profile, |
| 45 ArcAuthCodeFetcherDelegate* delegate, | 47 ArcAuthContext* context, |
| 46 net::URLRequestContextGetter* request_context_getter, | 48 const std::string& auth_endpoint) |
| 47 Profile* profile, | |
| 48 const std::string& auth_endpoint) | |
| 49 : OAuth2TokenService::Consumer(kConsumerName), | 49 : OAuth2TokenService::Consumer(kConsumerName), |
| 50 delegate_(delegate), | |
| 51 request_context_getter_(request_context_getter), | |
| 52 profile_(profile), | 50 profile_(profile), |
| 53 auth_endpoint_(auth_endpoint) { | 51 context_(context), |
| 52 auth_endpoint_(auth_endpoint), | |
| 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 | |
| 54 // Get token service and account ID to fetch auth tokens. | 75 // Get token service and account ID to fetch auth tokens. |
| 55 ProfileOAuth2TokenService* const token_service = | 76 ProfileOAuth2TokenService* const token_service = context_->token_service(); |
| 56 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_); | 77 const std::string& account_id = context_->account_id(); |
| 57 const SigninManagerBase* const signin_manager = | |
| 58 SigninManagerFactory::GetForProfile(profile_); | |
| 59 CHECK(token_service && signin_manager); | |
| 60 const std::string& account_id = signin_manager->GetAuthenticatedAccountId(); | |
| 61 DCHECK(!account_id.empty()); | |
| 62 DCHECK(token_service->RefreshTokenIsAvailable(account_id)); | 78 DCHECK(token_service->RefreshTokenIsAvailable(account_id)); |
| 63 | 79 |
| 64 OAuth2TokenService::ScopeSet scopes; | 80 OAuth2TokenService::ScopeSet scopes; |
| 65 scopes.insert(GaiaConstants::kOAuth1LoginScope); | 81 scopes.insert(GaiaConstants::kOAuth1LoginScope); |
| 66 login_token_request_.reset( | 82 login_token_request_.reset( |
|
Luis Héctor Chávez
2016/11/15 23:27:37
optional: can this be
login_token_request_ = tok
hidehiko
2016/11/16 01:00:34
Oh, right. Done.
| |
| 67 token_service->StartRequest(account_id, scopes, this).release()); | 83 token_service->StartRequest(account_id, scopes, this).release()); |
| 68 } | 84 } |
| 69 | 85 |
| 70 ArcAuthCodeFetcher::~ArcAuthCodeFetcher() {} | |
| 71 | |
| 72 void ArcAuthCodeFetcher::OnGetTokenSuccess( | 86 void ArcAuthCodeFetcher::OnGetTokenSuccess( |
| 73 const OAuth2TokenService::Request* request, | 87 const OAuth2TokenService::Request* request, |
| 74 const std::string& access_token, | 88 const std::string& access_token, |
| 75 const base::Time& expiration_time) { | 89 const base::Time& expiration_time) { |
| 76 ResetFetchers(); | 90 ResetFetchers(); |
| 77 | 91 |
| 78 const std::string device_id = user_manager::known_user::GetDeviceId( | 92 const std::string device_id = user_manager::known_user::GetDeviceId( |
| 79 multi_user_util::GetAccountIdFromProfile(profile_)); | 93 multi_user_util::GetAccountIdFromProfile(profile_)); |
| 80 DCHECK(!device_id.empty()); | 94 DCHECK(!device_id.empty()); |
| 81 | 95 |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 97 kGetAuthCodeNetworkRetry); | 111 kGetAuthCodeNetworkRetry); |
| 98 auth_code_fetcher_->SetExtraRequestHeaders(kGetAuthCodeHeaders); | 112 auth_code_fetcher_->SetExtraRequestHeaders(kGetAuthCodeHeaders); |
| 99 auth_code_fetcher_->Start(); | 113 auth_code_fetcher_->Start(); |
| 100 } | 114 } |
| 101 | 115 |
| 102 void ArcAuthCodeFetcher::OnGetTokenFailure( | 116 void ArcAuthCodeFetcher::OnGetTokenFailure( |
| 103 const OAuth2TokenService::Request* request, | 117 const OAuth2TokenService::Request* request, |
| 104 const GoogleServiceAuthError& error) { | 118 const GoogleServiceAuthError& error) { |
| 105 VLOG(2) << "Failed to get LST " << error.ToString() << "."; | 119 VLOG(2) << "Failed to get LST " << error.ToString() << "."; |
| 106 ResetFetchers(); | 120 ResetFetchers(); |
| 107 | 121 base::ResetAndReturn(&callback_).Run(std::string()); |
| 108 delegate_->OnAuthCodeFailed(); | |
| 109 } | 122 } |
| 110 | 123 |
| 111 void ArcAuthCodeFetcher::OnURLFetchComplete(const net::URLFetcher* source) { | 124 void ArcAuthCodeFetcher::OnURLFetchComplete(const net::URLFetcher* source) { |
| 112 const int response_code = source->GetResponseCode(); | 125 const int response_code = source->GetResponseCode(); |
| 113 std::string json_string; | 126 std::string json_string; |
| 114 source->GetResponseAsString(&json_string); | 127 source->GetResponseAsString(&json_string); |
| 115 | 128 |
| 116 ResetFetchers(); | 129 ResetFetchers(); |
| 117 | 130 |
| 118 if (response_code != net::HTTP_OK) { | 131 if (response_code != net::HTTP_OK) { |
| 119 VLOG(2) << "Server returned wrong response code: " << response_code << "."; | 132 VLOG(2) << "Server returned wrong response code: " << response_code << "."; |
| 120 delegate_->OnAuthCodeFailed(); | 133 base::ResetAndReturn(&callback_).Run(std::string()); |
| 121 return; | 134 return; |
| 122 } | 135 } |
| 123 | 136 |
| 124 JSONStringValueDeserializer deserializer(json_string); | 137 JSONStringValueDeserializer deserializer(json_string); |
| 125 std::string error_msg; | 138 std::string error_msg; |
| 126 std::unique_ptr<base::Value> auth_code_info = | 139 std::unique_ptr<base::Value> auth_code_info = |
| 127 deserializer.Deserialize(nullptr, &error_msg); | 140 deserializer.Deserialize(nullptr, &error_msg); |
| 128 if (!auth_code_info) { | 141 if (!auth_code_info) { |
| 129 VLOG(2) << "Unable to deserialize auth code json data: " << error_msg | 142 VLOG(2) << "Unable to deserialize auth code json data: " << error_msg |
| 130 << "."; | 143 << "."; |
| 131 delegate_->OnAuthCodeFailed(); | 144 base::ResetAndReturn(&callback_).Run(std::string()); |
| 132 return; | 145 return; |
| 133 } | 146 } |
| 134 | 147 |
| 135 std::unique_ptr<base::DictionaryValue> auth_code_dictionary = | 148 std::unique_ptr<base::DictionaryValue> auth_code_dictionary = |
| 136 base::DictionaryValue::From(std::move(auth_code_info)); | 149 base::DictionaryValue::From(std::move(auth_code_info)); |
| 137 if (!auth_code_dictionary) { | 150 if (!auth_code_dictionary) { |
| 138 NOTREACHED(); | 151 NOTREACHED(); |
| 139 delegate_->OnAuthCodeFailed(); | 152 base::ResetAndReturn(&callback_).Run(std::string()); |
| 140 return; | 153 return; |
| 141 } | 154 } |
| 142 | 155 |
| 143 std::string auth_code; | 156 std::string auth_code; |
| 144 if (!auth_code_dictionary->GetString(kToken, &auth_code) || | 157 if (!auth_code_dictionary->GetString(kToken, &auth_code) || |
| 145 auth_code.empty()) { | 158 auth_code.empty()) { |
| 146 VLOG(2) << "Response does not contain auth code."; | 159 VLOG(2) << "Response does not contain auth code."; |
| 147 delegate_->OnAuthCodeFailed(); | 160 base::ResetAndReturn(&callback_).Run(std::string()); |
| 148 return; | 161 return; |
| 149 } | 162 } |
| 150 | 163 |
| 151 delegate_->OnAuthCodeSuccess(auth_code); | 164 base::ResetAndReturn(&callback_).Run(auth_code); |
| 152 } | 165 } |
| 153 | 166 |
| 154 void ArcAuthCodeFetcher::ResetFetchers() { | 167 void ArcAuthCodeFetcher::ResetFetchers() { |
| 155 login_token_request_.reset(); | 168 login_token_request_.reset(); |
| 156 auth_code_fetcher_.reset(); | 169 auth_code_fetcher_.reset(); |
| 157 } | 170 } |
| 158 | 171 |
| 159 } // namespace arc | 172 } // namespace arc |
| OLD | NEW |