OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/login/signin/oauth2_login_manager.h" | 5 #include "chrome/browser/chromeos/login/signin/oauth2_login_manager.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
11 #include "base/metrics/histogram.h" | 11 #include "base/metrics/histogram.h" |
12 #include "base/prefs/pref_service.h" | 12 #include "base/prefs/pref_service.h" |
13 #include "base/strings/string_util.h" | 13 #include "base/strings/string_util.h" |
14 #include "chrome/browser/browser_process.h" | 14 #include "chrome/browser/browser_process.h" |
15 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
| 16 #include "chrome/browser/signin/account_tracker_service_factory.h" |
16 #include "chrome/browser/signin/chrome_signin_client_factory.h" | 17 #include "chrome/browser/signin/chrome_signin_client_factory.h" |
17 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | 18 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" |
18 #include "chrome/browser/signin/signin_manager_factory.h" | 19 #include "chrome/browser/signin/signin_manager_factory.h" |
19 #include "chrome/common/chrome_switches.h" | 20 #include "chrome/common/chrome_switches.h" |
20 #include "chromeos/chromeos_switches.h" | 21 #include "chromeos/chromeos_switches.h" |
| 22 #include "components/signin/core/browser/account_tracker_service.h" |
21 #include "components/signin/core/browser/profile_oauth2_token_service.h" | 23 #include "components/signin/core/browser/profile_oauth2_token_service.h" |
22 #include "components/signin/core/browser/signin_client.h" | 24 #include "components/signin/core/browser/signin_client.h" |
23 #include "components/signin/core/browser/signin_manager.h" | 25 #include "components/signin/core/browser/signin_manager.h" |
24 #include "components/user_manager/user_manager.h" | 26 #include "components/user_manager/user_manager.h" |
25 #include "google_apis/gaia/gaia_auth_util.h" | 27 #include "google_apis/gaia/gaia_auth_util.h" |
26 #include "google_apis/gaia/gaia_constants.h" | 28 #include "google_apis/gaia/gaia_constants.h" |
27 #include "google_apis/gaia/gaia_urls.h" | 29 #include "google_apis/gaia/gaia_urls.h" |
28 #include "net/url_request/url_request_context_getter.h" | 30 #include "net/url_request/url_request_context_getter.h" |
29 | 31 |
30 namespace chromeos { | 32 namespace chromeos { |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
159 | 161 |
160 const std::string& OAuth2LoginManager::GetPrimaryAccountId() { | 162 const std::string& OAuth2LoginManager::GetPrimaryAccountId() { |
161 SigninManagerBase* signin_manager = | 163 SigninManagerBase* signin_manager = |
162 SigninManagerFactory::GetForProfile(user_profile_); | 164 SigninManagerFactory::GetForProfile(user_profile_); |
163 return signin_manager->GetAuthenticatedAccountId(); | 165 return signin_manager->GetAuthenticatedAccountId(); |
164 } | 166 } |
165 | 167 |
166 void OAuth2LoginManager::StoreOAuth2Token() { | 168 void OAuth2LoginManager::StoreOAuth2Token() { |
167 const std::string& primary_account_id = GetPrimaryAccountId(); | 169 const std::string& primary_account_id = GetPrimaryAccountId(); |
168 if (primary_account_id.empty()) { | 170 if (primary_account_id.empty()) { |
169 GetAccountIdOfRefreshToken(refresh_token_); | 171 GetAccountInfoOfRefreshToken(refresh_token_); |
170 return; | 172 return; |
171 } | 173 } |
172 | 174 |
173 OnGetUserEmailResponse(primary_account_id); | 175 UpdateCredentials(primary_account_id); |
174 } | 176 } |
175 | 177 |
176 void OAuth2LoginManager::GetAccountIdOfRefreshToken( | 178 void OAuth2LoginManager::GetAccountInfoOfRefreshToken( |
177 const std::string& refresh_token) { | 179 const std::string& refresh_token) { |
178 gaia::OAuthClientInfo client_info; | 180 gaia::OAuthClientInfo client_info; |
179 GaiaUrls* gaia_urls = GaiaUrls::GetInstance(); | 181 GaiaUrls* gaia_urls = GaiaUrls::GetInstance(); |
180 client_info.client_id = gaia_urls->oauth2_chrome_client_id(); | 182 client_info.client_id = gaia_urls->oauth2_chrome_client_id(); |
181 client_info.client_secret = gaia_urls->oauth2_chrome_client_secret(); | 183 client_info.client_secret = gaia_urls->oauth2_chrome_client_secret(); |
182 | 184 |
183 account_id_fetcher_.reset(new gaia::GaiaOAuthClient( | 185 account_info_fetcher_.reset(new gaia::GaiaOAuthClient( |
184 auth_request_context_.get())); | 186 auth_request_context_.get())); |
185 account_id_fetcher_->RefreshToken(client_info, refresh_token, | 187 account_info_fetcher_->RefreshToken(client_info, refresh_token, |
186 std::vector<std::string>(1, kServiceScopeGetUserInfo), kMaxRetries, | 188 std::vector<std::string>(1, kServiceScopeGetUserInfo), kMaxRetries, |
187 this); | 189 this); |
188 } | 190 } |
189 | 191 |
| 192 void OAuth2LoginManager::UpdateCredentials(const std::string& account_id) { |
| 193 DCHECK(!account_id.empty()); |
| 194 DCHECK(!refresh_token_.empty()); |
| 195 // |account_id| is assumed to be already canonicalized if it's an email. |
| 196 GetTokenService()->UpdateCredentials(account_id, refresh_token_); |
| 197 |
| 198 FOR_EACH_OBSERVER(Observer, observer_list_, |
| 199 OnNewRefreshTokenAvaiable(user_profile_)); |
| 200 } |
| 201 |
190 void OAuth2LoginManager::OnRefreshTokenResponse( | 202 void OAuth2LoginManager::OnRefreshTokenResponse( |
191 const std::string& access_token, | 203 const std::string& access_token, |
192 int expires_in_seconds) { | 204 int expires_in_seconds) { |
193 account_id_fetcher_->GetUserEmail(access_token, kMaxRetries, this); | 205 account_info_fetcher_->GetUserInfo(access_token, kMaxRetries, this); |
194 } | 206 } |
195 | 207 |
196 void OAuth2LoginManager::OnGetUserEmailResponse( | 208 void OAuth2LoginManager::OnGetUserInfoResponse( |
197 const std::string& user_email) { | 209 scoped_ptr<base::DictionaryValue> user_info) { |
198 DCHECK(!refresh_token_.empty()); | 210 account_info_fetcher_.reset(); |
199 account_id_fetcher_.reset(); | |
200 std::string canonicalized = gaia::CanonicalizeEmail(user_email); | |
201 GetTokenService()->UpdateCredentials(canonicalized, refresh_token_); | |
202 | 211 |
203 FOR_EACH_OBSERVER(Observer, observer_list_, | 212 std::string gaia_id; |
204 OnNewRefreshTokenAvaiable(user_profile_)); | 213 std::string email; |
| 214 user_info->GetString("id", &gaia_id); |
| 215 user_info->GetString("email", &email); |
| 216 |
| 217 AccountTrackerService* account_tracker = |
| 218 AccountTrackerServiceFactory::GetForProfile(user_profile_); |
| 219 account_tracker->SeedAccountInfo(gaia_id, email); |
| 220 UpdateCredentials(account_tracker->PickAccountIdForAccount(gaia_id, email)); |
205 } | 221 } |
206 | 222 |
207 void OAuth2LoginManager::OnOAuthError() { | 223 void OAuth2LoginManager::OnOAuthError() { |
208 account_id_fetcher_.reset(); | 224 account_info_fetcher_.reset(); |
209 LOG(ERROR) << "Account id fetch failed!"; | 225 LOG(ERROR) << "Account info fetch failed!"; |
210 SetSessionRestoreState(OAuth2LoginManager::SESSION_RESTORE_FAILED); | 226 SetSessionRestoreState(OAuth2LoginManager::SESSION_RESTORE_FAILED); |
211 } | 227 } |
212 | 228 |
213 void OAuth2LoginManager::OnNetworkError(int response_code) { | 229 void OAuth2LoginManager::OnNetworkError(int response_code) { |
214 account_id_fetcher_.reset(); | 230 account_info_fetcher_.reset(); |
215 LOG(ERROR) << "Account id fetch failed! response_code=" << response_code; | 231 LOG(ERROR) << "Account info fetch failed! response_code=" << response_code; |
216 SetSessionRestoreState(OAuth2LoginManager::SESSION_RESTORE_FAILED); | 232 SetSessionRestoreState(OAuth2LoginManager::SESSION_RESTORE_FAILED); |
217 } | 233 } |
218 | 234 |
219 void OAuth2LoginManager::FetchOAuth2Tokens() { | 235 void OAuth2LoginManager::FetchOAuth2Tokens() { |
220 DCHECK(auth_request_context_.get()); | 236 DCHECK(auth_request_context_.get()); |
221 // If we have authenticated cookie jar, get OAuth1 token first, then fetch | 237 // If we have authenticated cookie jar, get OAuth1 token first, then fetch |
222 // SID/LSID cookies through OAuthLogin call. | 238 // SID/LSID cookies through OAuthLogin call. |
223 if (restore_strategy_ == RESTORE_FROM_COOKIE_JAR) { | 239 if (restore_strategy_ == RESTORE_FROM_COOKIE_JAR) { |
224 SigninClient* signin_client = | 240 SigninClient* signin_client = |
225 ChromeSigninClientFactory::GetForProfile(user_profile_); | 241 ChromeSigninClientFactory::GetForProfile(user_profile_); |
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
403 FOR_EACH_OBSERVER(Observer, observer_list_, | 419 FOR_EACH_OBSERVER(Observer, observer_list_, |
404 OnSessionRestoreStateChanged(user_profile_, state_)); | 420 OnSessionRestoreStateChanged(user_profile_, state_)); |
405 } | 421 } |
406 | 422 |
407 void OAuth2LoginManager::SetSessionRestoreStartForTesting( | 423 void OAuth2LoginManager::SetSessionRestoreStartForTesting( |
408 const base::Time& time) { | 424 const base::Time& time) { |
409 session_restore_start_ = time; | 425 session_restore_start_ = time; |
410 } | 426 } |
411 | 427 |
412 } // namespace chromeos | 428 } // namespace chromeos |
OLD | NEW |