Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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/extensions/api/identity/gaia_web_auth_flow.h" | 5 #include "chrome/browser/extensions/api/identity/gaia_web_auth_flow.h" |
| 6 | 6 |
| 7 #include "base/strings/string_number_conversions.h" | 7 #include "base/strings/string_number_conversions.h" |
| 8 #include "base/strings/string_split.h" | 8 #include "base/strings/string_split.h" |
| 9 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
| 10 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
| 11 #include "base/trace_event/trace_event.h" | 11 #include "base/trace_event/trace_event.h" |
| 12 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
| 13 #include "chrome/browser/signin/chrome_signin_client_factory.h" | 13 #include "chrome/browser/signin/chrome_signin_client_factory.h" |
| 14 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | 14 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" |
| 15 #include "chrome/browser/signin/signin_manager_factory.h" | 15 #include "chrome/browser/signin/signin_manager_factory.h" |
| 16 #include "components/signin/core/browser/profile_oauth2_token_service.h" | 16 #include "components/signin/core/browser/profile_oauth2_token_service.h" |
| 17 #include "components/signin/core/browser/signin_manager.h" | 17 #include "components/signin/core/browser/signin_manager.h" |
| 18 #include "content/public/browser/browser_thread.h" | |
| 18 #include "google_apis/gaia/gaia_constants.h" | 19 #include "google_apis/gaia/gaia_constants.h" |
| 19 #include "google_apis/gaia/gaia_urls.h" | 20 #include "google_apis/gaia/gaia_urls.h" |
| 20 #include "net/base/escape.h" | 21 #include "net/base/escape.h" |
| 22 #include "net/ssl/channel_id_service.h" | |
| 23 #include "net/url_request/url_request_context.h" | |
| 24 #include "net/url_request/url_request_context_getter.h" | |
| 21 | 25 |
| 22 namespace extensions { | 26 namespace extensions { |
| 23 | 27 |
| 24 GaiaWebAuthFlow::GaiaWebAuthFlow(Delegate* delegate, | 28 GaiaWebAuthFlow::GaiaWebAuthFlow(Delegate* delegate, |
| 25 Profile* profile, | 29 Profile* profile, |
| 26 const ExtensionTokenKey* token_key, | 30 const ExtensionTokenKey* token_key, |
| 27 const std::string& oauth2_client_id, | 31 const std::string& oauth2_client_id, |
| 28 const std::string& locale) | 32 const std::string& locale) |
| 29 : delegate_(delegate), | 33 : delegate_(delegate), |
| 30 profile_(profile), | 34 profile_(profile), |
| 31 account_id_(token_key->account_id) { | 35 account_id_(token_key->account_id), |
| 36 weak_ptr_factory_(this) { | |
| 32 TRACE_EVENT_ASYNC_BEGIN2("identity", | 37 TRACE_EVENT_ASYNC_BEGIN2("identity", |
| 33 "GaiaWebAuthFlow", | 38 "GaiaWebAuthFlow", |
| 34 this, | 39 this, |
| 35 "extension_id", | 40 "extension_id", |
| 36 token_key->extension_id, | 41 token_key->extension_id, |
| 37 "account_id", | 42 "account_id", |
| 38 token_key->account_id); | 43 token_key->account_id); |
| 39 | 44 |
| 40 const char kOAuth2RedirectPathFormat[] = "/%s#"; | 45 const char kOAuth2RedirectPathFormat[] = "/%s#"; |
| 41 const char kOAuth2AuthorizeFormat[] = | 46 const char kOAuth2AuthorizeFormat[] = |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 79 kOAuth2AuthorizeFormatDeviceIdAddendum, | 84 kOAuth2AuthorizeFormatDeviceIdAddendum, |
| 80 net::EscapeUrlEncodedData(signin_scoped_device_id, true).c_str()); | 85 net::EscapeUrlEncodedData(signin_scoped_device_id, true).c_str()); |
| 81 } | 86 } |
| 82 auth_url_ = GaiaUrls::GetInstance()->oauth2_auth_url().Resolve( | 87 auth_url_ = GaiaUrls::GetInstance()->oauth2_auth_url().Resolve( |
| 83 oauth2_authorize_params); | 88 oauth2_authorize_params); |
| 84 } | 89 } |
| 85 | 90 |
| 86 GaiaWebAuthFlow::~GaiaWebAuthFlow() { | 91 GaiaWebAuthFlow::~GaiaWebAuthFlow() { |
| 87 TRACE_EVENT_ASYNC_END0("identity", "GaiaWebAuthFlow", this); | 92 TRACE_EVENT_ASYNC_END0("identity", "GaiaWebAuthFlow", this); |
| 88 | 93 |
| 94 content::BrowserThread::PostTask( | |
|
Michael Courage
2016/07/14 17:53:30
should check to see if io_helper_ has been set bef
anthonyvd
2016/07/14 19:27:07
Done.
| |
| 95 content::BrowserThread::IO, | |
| 96 FROM_HERE, | |
| 97 base::Bind(&GaiaWebAuthFlow::IOHelper::Cleanup, | |
| 98 base::Unretained(io_helper_.release()))); | |
| 99 | |
| 89 if (web_flow_) | 100 if (web_flow_) |
| 90 web_flow_.release()->DetachDelegateAndDelete(); | 101 web_flow_.release()->DetachDelegateAndDelete(); |
| 91 } | 102 } |
| 92 | 103 |
| 93 void GaiaWebAuthFlow::Start() { | 104 void GaiaWebAuthFlow::Start() { |
| 105 io_helper_.reset(new IOHelper(weak_ptr_factory_.GetWeakPtr(), | |
| 106 profile_->GetRequestContext())); | |
| 107 content::BrowserThread::PostTask( | |
| 108 content::BrowserThread::IO, | |
| 109 FROM_HERE, | |
| 110 base::Bind(&GaiaWebAuthFlow::IOHelper::PrepareRequestContext, | |
| 111 base::Unretained(io_helper_.get()))); | |
| 112 } | |
| 113 | |
| 114 void GaiaWebAuthFlow::StartUberTokenFetch() { | |
| 94 ProfileOAuth2TokenService* token_service = | 115 ProfileOAuth2TokenService* token_service = |
| 95 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_); | 116 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_); |
| 117 context_getter_ = new net::TrivialURLRequestContextGetter( | |
| 118 io_helper_->ubertoken_request_context(), | |
| 119 profile_->GetRequestContext()->GetNetworkTaskRunner()); | |
| 96 ubertoken_fetcher_.reset(new UbertokenFetcher(token_service, | 120 ubertoken_fetcher_.reset(new UbertokenFetcher(token_service, |
| 97 this, | 121 this, |
| 98 GaiaConstants::kChromeSource, | 122 GaiaConstants::kChromeSource, |
| 99 profile_->GetRequestContext())); | 123 context_getter_.get())); |
| 100 ubertoken_fetcher_->StartFetchingToken(account_id_); | 124 ubertoken_fetcher_->StartFetchingToken(account_id_); |
| 101 } | 125 } |
| 102 | 126 |
| 103 void GaiaWebAuthFlow::OnUbertokenSuccess(const std::string& token) { | 127 void GaiaWebAuthFlow::OnUbertokenSuccess(const std::string& token) { |
| 104 TRACE_EVENT_ASYNC_STEP_PAST0( | 128 TRACE_EVENT_ASYNC_STEP_PAST0( |
| 105 "identity", "GaiaWebAuthFlow", this, "OnUbertokenSuccess"); | 129 "identity", "GaiaWebAuthFlow", this, "OnUbertokenSuccess"); |
| 106 | 130 |
| 107 const char kMergeSessionQueryFormat[] = "?uberauth=%s&" | 131 const char kMergeSessionQueryFormat[] = "?uberauth=%s&" |
| 108 "continue=%s&" | 132 "continue=%s&" |
| 109 "source=appsv2"; | 133 "source=appsv2"; |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 229 if (url.is_valid()) | 253 if (url.is_valid()) |
| 230 OnAuthFlowURLChange(url); | 254 OnAuthFlowURLChange(url); |
| 231 } | 255 } |
| 232 } | 256 } |
| 233 | 257 |
| 234 std::unique_ptr<WebAuthFlow> GaiaWebAuthFlow::CreateWebAuthFlow(GURL url) { | 258 std::unique_ptr<WebAuthFlow> GaiaWebAuthFlow::CreateWebAuthFlow(GURL url) { |
| 235 return std::unique_ptr<WebAuthFlow>( | 259 return std::unique_ptr<WebAuthFlow>( |
| 236 new WebAuthFlow(this, profile_, url, WebAuthFlow::INTERACTIVE)); | 260 new WebAuthFlow(this, profile_, url, WebAuthFlow::INTERACTIVE)); |
| 237 } | 261 } |
| 238 | 262 |
| 263 GaiaWebAuthFlow::IOHelper::IOHelper( | |
| 264 base::WeakPtr<GaiaWebAuthFlow> gaia_web_auth_flow, | |
| 265 net::URLRequestContextGetter* main_context) | |
| 266 : gaia_web_auth_flow_(gaia_web_auth_flow), | |
| 267 main_context_(main_context) {} | |
| 268 | |
| 269 GaiaWebAuthFlow::IOHelper::~IOHelper() {} | |
| 270 | |
| 271 void GaiaWebAuthFlow::IOHelper::PrepareRequestContext() { | |
| 272 net::URLRequestContext* base_context = main_context_->GetURLRequestContext(); | |
| 273 // Create a temporary request context for the Uber Token Fetch that doesn't | |
| 274 // have a Channel ID. This way, the Uber Token will not be channel bound and | |
| 275 // will be useable for the MergeSession call that will take place in the app's | |
| 276 // request context. | |
| 277 ubertoken_request_context_.reset(new net::URLRequestContext); | |
| 278 | |
| 279 ubertoken_request_context_->CopyFrom(base_context); | |
| 280 ubertoken_request_context_->set_channel_id_service(nullptr); | |
| 281 | |
| 282 // Merely setting the Channel ID Service isn't enough, we also have to build a | |
| 283 // new HttpNetworkSession. | |
| 284 app_backend_ = net::HttpCache::DefaultBackend::InMemory(0); | |
| 285 net::HttpNetworkSession::Params network_params = | |
| 286 *base_context->GetNetworkSessionParams(); | |
| 287 network_params.channel_id_service = nullptr; | |
| 288 http_network_session_.reset(new net::HttpNetworkSession(network_params)); | |
| 289 app_http_cache_.reset(new net::HttpCache( | |
| 290 http_network_session_.get(), std::move(app_backend_), true)); | |
| 291 | |
| 292 ubertoken_request_context_->set_http_transaction_factory( | |
| 293 app_http_cache_.get()); | |
| 294 | |
| 295 content::BrowserThread::PostTask( | |
| 296 content::BrowserThread::UI, | |
| 297 FROM_HERE, | |
| 298 base::Bind(&GaiaWebAuthFlow::StartUberTokenFetch, | |
| 299 gaia_web_auth_flow_)); | |
| 300 } | |
| 301 | |
| 302 void GaiaWebAuthFlow::IOHelper::Cleanup() { | |
| 303 delete this; | |
| 304 } | |
| 305 | |
| 239 } // namespace extensions | 306 } // namespace extensions |
| OLD | NEW |