OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 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/ui/webui/signin/inline_login_handler_impl.h" | 5 #include "chrome/browser/ui/webui/signin/inline_login_handler_impl.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
(...skipping 15 matching lines...) Expand all Loading... |
26 #include "chrome/browser/ui/sync/one_click_signin_histogram.h" | 26 #include "chrome/browser/ui/sync/one_click_signin_histogram.h" |
27 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 27 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
28 #include "chrome/browser/ui/webui/signin/inline_login_ui.h" | 28 #include "chrome/browser/ui/webui/signin/inline_login_ui.h" |
29 #include "chrome/browser/ui/webui/signin/login_ui_service.h" | 29 #include "chrome/browser/ui/webui/signin/login_ui_service.h" |
30 #include "chrome/browser/ui/webui/signin/login_ui_service_factory.h" | 30 #include "chrome/browser/ui/webui/signin/login_ui_service_factory.h" |
31 #include "chrome/common/url_constants.h" | 31 #include "chrome/common/url_constants.h" |
32 #include "components/signin/core/browser/about_signin_internals.h" | 32 #include "components/signin/core/browser/about_signin_internals.h" |
33 #include "components/signin/core/browser/account_tracker_service.h" | 33 #include "components/signin/core/browser/account_tracker_service.h" |
34 #include "components/signin/core/browser/profile_oauth2_token_service.h" | 34 #include "components/signin/core/browser/profile_oauth2_token_service.h" |
35 #include "components/signin/core/browser/signin_error_controller.h" | 35 #include "components/signin/core/browser/signin_error_controller.h" |
36 #include "components/signin/core/browser/signin_oauth_helper.h" | |
37 #include "components/signin/core/common/profile_management_switches.h" | 36 #include "components/signin/core/common/profile_management_switches.h" |
38 #include "content/public/browser/storage_partition.h" | 37 #include "content/public/browser/storage_partition.h" |
39 #include "content/public/browser/web_ui.h" | 38 #include "content/public/browser/web_ui.h" |
| 39 #include "google_apis/gaia/gaia_auth_consumer.h" |
40 #include "google_apis/gaia/gaia_auth_fetcher.h" | 40 #include "google_apis/gaia/gaia_auth_fetcher.h" |
41 #include "google_apis/gaia/gaia_auth_util.h" | 41 #include "google_apis/gaia/gaia_auth_util.h" |
42 #include "google_apis/gaia/gaia_constants.h" | 42 #include "google_apis/gaia/gaia_constants.h" |
43 #include "google_apis/gaia/gaia_urls.h" | 43 #include "google_apis/gaia/gaia_urls.h" |
44 #include "net/base/url_util.h" | 44 #include "net/base/url_util.h" |
45 | 45 |
46 namespace { | 46 namespace { |
47 | 47 |
48 class InlineSigninHelper : public SigninOAuthHelper::Consumer { | 48 class InlineSigninHelper : public GaiaAuthConsumer { |
49 public: | 49 public: |
50 InlineSigninHelper( | 50 InlineSigninHelper( |
51 base::WeakPtr<InlineLoginHandlerImpl> handler, | 51 base::WeakPtr<InlineLoginHandlerImpl> handler, |
52 net::URLRequestContextGetter* getter, | 52 net::URLRequestContextGetter* getter, |
53 Profile* profile, | 53 Profile* profile, |
54 const GURL& current_url, | 54 const GURL& current_url, |
55 const std::string& email, | 55 const std::string& email, |
| 56 const std::string& gaia_id, |
56 const std::string& password, | 57 const std::string& password, |
57 const std::string& session_index, | 58 const std::string& session_index, |
58 const std::string& signin_scoped_device_id, | 59 const std::string& signin_scoped_device_id, |
59 bool choose_what_to_sync, | 60 bool choose_what_to_sync, |
60 bool confirm_untrusted_signin); | 61 bool confirm_untrusted_signin); |
61 | 62 |
62 private: | 63 private: |
63 // Overriden from SigninOAuthHelper::Consumer. | 64 // Overridden from GaiaAuthConsumer. |
64 void OnSigninOAuthInformationAvailable( | 65 void OnClientOAuthSuccess(const ClientOAuthResult& result) override; |
65 const std::string& email, | 66 void OnClientOAuthFailure(const GoogleServiceAuthError& error) |
66 const std::string& display_email, | 67 override; |
67 const std::string& refresh_token) override; | |
68 void OnSigninOAuthInformationFailure( | |
69 const GoogleServiceAuthError& error) override; | |
70 | 68 |
71 SigninOAuthHelper signin_oauth_helper_; | 69 GaiaAuthFetcher gaia_auth_fetcher_; |
72 base::WeakPtr<InlineLoginHandlerImpl> handler_; | 70 base::WeakPtr<InlineLoginHandlerImpl> handler_; |
73 Profile* profile_; | 71 Profile* profile_; |
74 GURL current_url_; | 72 GURL current_url_; |
75 std::string email_; | 73 std::string email_; |
| 74 std::string gaia_id_; |
76 std::string password_; | 75 std::string password_; |
77 std::string session_index_; | 76 std::string session_index_; |
78 bool choose_what_to_sync_; | 77 bool choose_what_to_sync_; |
79 bool confirm_untrusted_signin_; | 78 bool confirm_untrusted_signin_; |
80 | 79 |
81 DISALLOW_COPY_AND_ASSIGN(InlineSigninHelper); | 80 DISALLOW_COPY_AND_ASSIGN(InlineSigninHelper); |
82 }; | 81 }; |
83 | 82 |
84 InlineSigninHelper::InlineSigninHelper( | 83 InlineSigninHelper::InlineSigninHelper( |
85 base::WeakPtr<InlineLoginHandlerImpl> handler, | 84 base::WeakPtr<InlineLoginHandlerImpl> handler, |
86 net::URLRequestContextGetter* getter, | 85 net::URLRequestContextGetter* getter, |
87 Profile* profile, | 86 Profile* profile, |
88 const GURL& current_url, | 87 const GURL& current_url, |
89 const std::string& email, | 88 const std::string& email, |
| 89 const std::string& gaia_id, |
90 const std::string& password, | 90 const std::string& password, |
91 const std::string& session_index, | 91 const std::string& session_index, |
92 const std::string& signin_scoped_device_id, | 92 const std::string& signin_scoped_device_id, |
93 bool choose_what_to_sync, | 93 bool choose_what_to_sync, |
94 bool confirm_untrusted_signin) | 94 bool confirm_untrusted_signin) |
95 : signin_oauth_helper_(getter, session_index, signin_scoped_device_id, | 95 : gaia_auth_fetcher_(this, GaiaConstants::kChromeSource, getter), |
96 this), | |
97 handler_(handler), | 96 handler_(handler), |
98 profile_(profile), | 97 profile_(profile), |
99 current_url_(current_url), | 98 current_url_(current_url), |
100 email_(email), | 99 email_(email), |
| 100 gaia_id_(gaia_id), |
101 password_(password), | 101 password_(password), |
102 session_index_(session_index), | 102 session_index_(session_index), |
103 choose_what_to_sync_(choose_what_to_sync), | 103 choose_what_to_sync_(choose_what_to_sync), |
104 confirm_untrusted_signin_(confirm_untrusted_signin) { | 104 confirm_untrusted_signin_(confirm_untrusted_signin) { |
105 DCHECK(profile_); | 105 DCHECK(profile_); |
106 DCHECK(!email_.empty()); | 106 DCHECK(!email_.empty()); |
| 107 gaia_auth_fetcher_.StartCookieForOAuthLoginTokenExchangeWithDeviceId( |
| 108 session_index, signin_scoped_device_id); |
107 } | 109 } |
108 | 110 |
109 void InlineSigninHelper::OnSigninOAuthInformationAvailable( | 111 void InlineSigninHelper::OnClientOAuthSuccess(const ClientOAuthResult& result) { |
110 const std::string& email, | |
111 const std::string& display_email, | |
112 const std::string& refresh_token) { | |
113 content::WebContents* contents = NULL; | 112 content::WebContents* contents = NULL; |
114 Browser* browser = NULL; | 113 Browser* browser = NULL; |
115 if (handler_) { | 114 if (handler_) { |
116 contents = handler_->web_ui()->GetWebContents(); | 115 contents = handler_->web_ui()->GetWebContents(); |
117 browser = handler_->GetDesktopBrowser(); | 116 browser = handler_->GetDesktopBrowser(); |
118 } | 117 } |
119 | 118 |
120 AboutSigninInternals* about_signin_internals = | 119 AboutSigninInternals* about_signin_internals = |
121 AboutSigninInternalsFactory::GetForProfile(profile_); | 120 AboutSigninInternalsFactory::GetForProfile(profile_); |
122 about_signin_internals->OnRefreshTokenReceived("Successful"); | 121 about_signin_internals->OnRefreshTokenReceived("Successful"); |
123 | 122 |
| 123 AccountTrackerService* account_tracker = |
| 124 AccountTrackerServiceFactory::GetForProfile(profile_); |
| 125 std::string account_id = |
| 126 account_tracker->PickAccountIdForAccount(gaia_id_, email_); |
| 127 |
| 128 // Prime the account tracker with this combination of gaia id/display email. |
| 129 account_tracker->SeedAccountInfo(gaia_id_, email_); |
| 130 |
124 signin::Source source = signin::GetSourceForPromoURL(current_url_); | 131 signin::Source source = signin::GetSourceForPromoURL(current_url_); |
125 | 132 |
126 std::string primary_email = | 133 std::string primary_email = |
127 SigninManagerFactory::GetForProfile(profile_)->GetAuthenticatedUsername(); | 134 SigninManagerFactory::GetForProfile(profile_)->GetAuthenticatedUsername(); |
128 if (gaia::AreEmailsSame(email, primary_email) && | 135 if (gaia::AreEmailsSame(email_, primary_email) && |
129 source == signin::SOURCE_REAUTH && | 136 source == signin::SOURCE_REAUTH && |
130 switches::IsNewProfileManagement()) { | 137 switches::IsNewProfileManagement()) { |
131 chrome::SetLocalAuthCredentials(profile_, password_); | 138 chrome::SetLocalAuthCredentials(profile_, password_); |
132 } | 139 } |
133 | 140 |
134 if (source == signin::SOURCE_AVATAR_BUBBLE_ADD_ACCOUNT || | 141 if (source == signin::SOURCE_AVATAR_BUBBLE_ADD_ACCOUNT || |
135 source == signin::SOURCE_REAUTH) { | 142 source == signin::SOURCE_REAUTH) { |
136 // TODO(rogerta): the javascript code will need to pass in the gaia-id | |
137 // of the account instead of the email when chrome uses gaia-id as key. | |
138 DCHECK_EQ(AccountTrackerService::MIGRATION_NOT_STARTED, | |
139 AccountTrackerServiceFactory::GetForProfile(profile_)-> | |
140 GetMigrationState()); | |
141 const std::string account_id = gaia::CanonicalizeEmail(email); | |
142 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_)-> | 143 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_)-> |
143 UpdateCredentials(account_id, refresh_token); | 144 UpdateCredentials(account_id, result.refresh_token); |
144 | 145 |
145 if (signin::IsAutoCloseEnabledInURL(current_url_)) { | 146 if (signin::IsAutoCloseEnabledInURL(current_url_)) { |
146 // Close the gaia sign in tab via a task to make sure we aren't in the | 147 // Close the gaia sign in tab via a task to make sure we aren't in the |
147 // middle of any webui handler code. | 148 // middle of any webui handler code. |
148 base::MessageLoop::current()->PostTask( | 149 base::MessageLoop::current()->PostTask( |
149 FROM_HERE, | 150 FROM_HERE, |
150 base::Bind(&InlineLoginHandlerImpl::CloseTab, | 151 base::Bind(&InlineLoginHandlerImpl::CloseTab, |
151 handler_, | 152 handler_, |
152 signin::ShouldShowAccountManagement(current_url_))); | 153 signin::ShouldShowAccountManagement(current_url_))); |
153 } | 154 } |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
185 confirmation_required = | 186 confirmation_required = |
186 source == signin::SOURCE_SETTINGS || | 187 source == signin::SOURCE_SETTINGS || |
187 choose_what_to_sync_ ? | 188 choose_what_to_sync_ ? |
188 OneClickSigninSyncStarter::NO_CONFIRMATION : | 189 OneClickSigninSyncStarter::NO_CONFIRMATION : |
189 OneClickSigninSyncStarter::CONFIRM_AFTER_SIGNIN; | 190 OneClickSigninSyncStarter::CONFIRM_AFTER_SIGNIN; |
190 } | 191 } |
191 | 192 |
192 bool start_signin = | 193 bool start_signin = |
193 !OneClickSigninHelper::HandleCrossAccountError( | 194 !OneClickSigninHelper::HandleCrossAccountError( |
194 profile_, "", | 195 profile_, "", |
195 email, password_, refresh_token, | 196 email_, password_, result.refresh_token, |
196 OneClickSigninHelper::AUTO_ACCEPT_EXPLICIT, | 197 OneClickSigninHelper::AUTO_ACCEPT_EXPLICIT, |
197 source, start_mode, | 198 source, start_mode, |
198 base::Bind(&InlineLoginHandlerImpl::SyncStarterCallback, | 199 base::Bind(&InlineLoginHandlerImpl::SyncStarterCallback, |
199 handler_)); | 200 handler_)); |
200 if (start_signin) { | 201 if (start_signin) { |
201 // Call OneClickSigninSyncStarter to exchange oauth code for tokens. | 202 // Call OneClickSigninSyncStarter to exchange oauth code for tokens. |
202 // OneClickSigninSyncStarter will delete itself once the job is done. | 203 // OneClickSigninSyncStarter will delete itself once the job is done. |
203 new OneClickSigninSyncStarter( | 204 new OneClickSigninSyncStarter( |
204 profile_, browser, | 205 profile_, browser, |
205 email, password_, refresh_token, | 206 account_id, password_, result.refresh_token, |
206 start_mode, | 207 start_mode, |
207 contents, | 208 contents, |
208 confirmation_required, | 209 confirmation_required, |
209 signin::GetNextPageURLForPromoURL(current_url_), | 210 signin::GetNextPageURLForPromoURL(current_url_), |
210 base::Bind(&InlineLoginHandlerImpl::SyncStarterCallback, handler_)); | 211 base::Bind(&InlineLoginHandlerImpl::SyncStarterCallback, handler_)); |
211 } | 212 } |
212 } | 213 } |
213 | 214 |
214 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this); | 215 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this); |
215 } | 216 } |
216 | 217 |
217 void InlineSigninHelper::OnSigninOAuthInformationFailure( | 218 void InlineSigninHelper::OnClientOAuthFailure( |
218 const GoogleServiceAuthError& error) { | 219 const GoogleServiceAuthError& error) { |
219 if (handler_) | 220 if (handler_) |
220 handler_->HandleLoginError(error.ToString()); | 221 handler_->HandleLoginError(error.ToString()); |
221 | 222 |
222 AboutSigninInternals* about_signin_internals = | 223 AboutSigninInternals* about_signin_internals = |
223 AboutSigninInternalsFactory::GetForProfile(profile_); | 224 AboutSigninInternalsFactory::GetForProfile(profile_); |
224 about_signin_internals->OnRefreshTokenReceived("Failure"); | 225 about_signin_internals->OnRefreshTokenReceived("Failure"); |
225 | 226 |
226 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this); | 227 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this); |
227 } | 228 } |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
305 | 306 |
306 base::string16 email_string16; | 307 base::string16 email_string16; |
307 dict->GetString("email", &email_string16); | 308 dict->GetString("email", &email_string16); |
308 DCHECK(!email_string16.empty()); | 309 DCHECK(!email_string16.empty()); |
309 std::string email(base::UTF16ToASCII(email_string16)); | 310 std::string email(base::UTF16ToASCII(email_string16)); |
310 | 311 |
311 base::string16 password_string16; | 312 base::string16 password_string16; |
312 dict->GetString("password", &password_string16); | 313 dict->GetString("password", &password_string16); |
313 std::string password(base::UTF16ToASCII(password_string16)); | 314 std::string password(base::UTF16ToASCII(password_string16)); |
314 | 315 |
| 316 base::string16 gaia_id_string16; |
| 317 dict->GetString("gaiaId", &gaia_id_string16); |
| 318 DCHECK(!gaia_id_string16.empty()); |
| 319 std::string gaia_id = base::UTF16ToASCII(gaia_id_string16); |
| 320 |
315 // When doing a SAML sign in, this email check may result in a false | 321 // When doing a SAML sign in, this email check may result in a false |
316 // positive. This happens when the user types one email address in the | 322 // positive. This happens when the user types one email address in the |
317 // gaia sign in page, but signs in to a different account in the SAML sign in | 323 // gaia sign in page, but signs in to a different account in the SAML sign in |
318 // page. | 324 // page. |
319 std::string default_email; | 325 std::string default_email; |
320 std::string validate_email; | 326 std::string validate_email; |
321 if (net::GetValueForKeyInQuery(current_url, "email", &default_email) && | 327 if (net::GetValueForKeyInQuery(current_url, "email", &default_email) && |
322 net::GetValueForKeyInQuery(current_url, "validateEmail", | 328 net::GetValueForKeyInQuery(current_url, "validateEmail", |
323 &validate_email) && | 329 &validate_email) && |
324 validate_email == "1") { | 330 validate_email == "1") { |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
383 contents->GetBrowserContext(), | 389 contents->GetBrowserContext(), |
384 GURL(chrome::kChromeUIChromeSigninURL)); | 390 GURL(chrome::kChromeUIChromeSigninURL)); |
385 | 391 |
386 SigninClient* signin_client = | 392 SigninClient* signin_client = |
387 ChromeSigninClientFactory::GetForProfile(Profile::FromWebUI(web_ui())); | 393 ChromeSigninClientFactory::GetForProfile(Profile::FromWebUI(web_ui())); |
388 std::string signin_scoped_device_id = | 394 std::string signin_scoped_device_id = |
389 signin_client->GetSigninScopedDeviceId(); | 395 signin_client->GetSigninScopedDeviceId(); |
390 // InlineSigninHelper will delete itself. | 396 // InlineSigninHelper will delete itself. |
391 new InlineSigninHelper(GetWeakPtr(), partition->GetURLRequestContext(), | 397 new InlineSigninHelper(GetWeakPtr(), partition->GetURLRequestContext(), |
392 Profile::FromWebUI(web_ui()), current_url, | 398 Profile::FromWebUI(web_ui()), current_url, |
393 email, password, session_index, | 399 email, gaia_id, password, session_index, |
394 signin_scoped_device_id, choose_what_to_sync, | 400 signin_scoped_device_id, choose_what_to_sync, |
395 confirm_untrusted_signin_); | 401 confirm_untrusted_signin_); |
396 | 402 |
397 web_ui()->CallJavascriptFunction("inline.login.closeDialog"); | 403 web_ui()->CallJavascriptFunction("inline.login.closeDialog"); |
398 } | 404 } |
399 | 405 |
400 void InlineLoginHandlerImpl::HandleLoginError(const std::string& error_msg) { | 406 void InlineLoginHandlerImpl::HandleLoginError(const std::string& error_msg) { |
401 SyncStarterCallback(OneClickSigninSyncStarter::SYNC_SETUP_FAILURE); | 407 SyncStarterCallback(OneClickSigninSyncStarter::SYNC_SETUP_FAILURE); |
402 | 408 |
403 Browser* browser = GetDesktopBrowser(); | 409 Browser* browser = GetDesktopBrowser(); |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
458 } | 464 } |
459 } | 465 } |
460 | 466 |
461 if (show_account_management) { | 467 if (show_account_management) { |
462 browser->window()->ShowAvatarBubbleFromAvatarButton( | 468 browser->window()->ShowAvatarBubbleFromAvatarButton( |
463 BrowserWindow::AVATAR_BUBBLE_MODE_ACCOUNT_MANAGEMENT, | 469 BrowserWindow::AVATAR_BUBBLE_MODE_ACCOUNT_MANAGEMENT, |
464 signin::ManageAccountsParams()); | 470 signin::ManageAccountsParams()); |
465 } | 471 } |
466 } | 472 } |
467 } | 473 } |
OLD | NEW |