| 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" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 #include "components/signin/core/browser/profile_oauth2_token_service.h" | 21 #include "components/signin/core/browser/profile_oauth2_token_service.h" |
| 22 #include "components/signin/core/browser/signin_client.h" | 22 #include "components/signin/core/browser/signin_client.h" |
| 23 #include "components/signin/core/browser/signin_manager.h" | 23 #include "components/signin/core/browser/signin_manager.h" |
| 24 #include "components/user_manager/user.h" | 24 #include "components/user_manager/user.h" |
| 25 #include "components/user_manager/user_manager.h" | 25 #include "components/user_manager/user_manager.h" |
| 26 #include "google_apis/gaia/gaia_auth_util.h" | 26 #include "google_apis/gaia/gaia_auth_util.h" |
| 27 #include "google_apis/gaia/gaia_urls.h" | 27 #include "google_apis/gaia/gaia_urls.h" |
| 28 | 28 |
| 29 namespace chromeos { | 29 namespace chromeos { |
| 30 | 30 |
| 31 namespace { | |
| 32 | |
| 33 static const char kServiceScopeGetUserInfo[] = | |
| 34 "https://www.googleapis.com/auth/userinfo.email"; | |
| 35 static const int kMaxRetries = 5; | |
| 36 | |
| 37 } // namespace | |
| 38 | |
| 39 OAuth2LoginManager::OAuth2LoginManager(Profile* user_profile) | 31 OAuth2LoginManager::OAuth2LoginManager(Profile* user_profile) |
| 40 : user_profile_(user_profile), | 32 : user_profile_(user_profile), |
| 41 restore_strategy_(RESTORE_FROM_COOKIE_JAR), | 33 restore_strategy_(RESTORE_FROM_COOKIE_JAR), |
| 42 state_(SESSION_RESTORE_NOT_STARTED) { | 34 state_(SESSION_RESTORE_NOT_STARTED) { |
| 43 GetTokenService()->AddObserver(this); | 35 GetTokenService()->AddObserver(this); |
| 44 | 36 |
| 45 // For telemetry, we mark session restore completed to avoid warnings from | 37 // For telemetry, we mark session restore completed to avoid warnings from |
| 46 // MergeSessionThrottle. | 38 // MergeSessionThrottle. |
| 47 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | 39 if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 48 chromeos::switches::kDisableGaiaServices)) { | 40 chromeos::switches::kDisableGaiaServices)) { |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 96 } | 88 } |
| 97 | 89 |
| 98 void OAuth2LoginManager::RestoreSessionFromSavedTokens() { | 90 void OAuth2LoginManager::RestoreSessionFromSavedTokens() { |
| 99 // Just return if there is a pending TokenService::LoadCredentials call. | 91 // Just return if there is a pending TokenService::LoadCredentials call. |
| 100 // Session restore continues in OnRefreshTokenAvailable when the call | 92 // Session restore continues in OnRefreshTokenAvailable when the call |
| 101 // finishes. | 93 // finishes. |
| 102 if (pending_token_service_load_) | 94 if (pending_token_service_load_) |
| 103 return; | 95 return; |
| 104 | 96 |
| 105 ProfileOAuth2TokenService* token_service = GetTokenService(); | 97 ProfileOAuth2TokenService* token_service = GetTokenService(); |
| 106 const std::string& primary_account_id = GetPrimaryAccountId(); | 98 const std::string primary_account_id = GetPrimaryAccountId(); |
| 107 if (token_service->RefreshTokenIsAvailable(primary_account_id)) { | 99 if (token_service->RefreshTokenIsAvailable(primary_account_id)) { |
| 108 VLOG(1) << "OAuth2 refresh token is already loaded."; | 100 VLOG(1) << "OAuth2 refresh token is already loaded."; |
| 109 FireRefreshTokensLoaded(); | 101 FireRefreshTokensLoaded(); |
| 110 VerifySessionCookies(); | 102 VerifySessionCookies(); |
| 111 } else { | 103 } else { |
| 112 VLOG(1) << "Loading OAuth2 refresh token from database."; | 104 VLOG(1) << "Loading OAuth2 refresh token from database."; |
| 113 | 105 |
| 114 // Flag user with unknown token status in case there are no saved tokens | 106 // Flag user with unknown token status in case there are no saved tokens |
| 115 // and OnRefreshTokenAvailable is not called. Flagging it here would | 107 // and OnRefreshTokenAvailable is not called. Flagging it here would |
| 116 // cause user to go through Gaia in next login to obtain a new refresh | 108 // cause user to go through Gaia in next login to obtain a new refresh |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 163 | 155 |
| 164 pending_token_service_load_ = false; | 156 pending_token_service_load_ = false; |
| 165 VerifySessionCookies(); | 157 VerifySessionCookies(); |
| 166 } | 158 } |
| 167 } | 159 } |
| 168 | 160 |
| 169 ProfileOAuth2TokenService* OAuth2LoginManager::GetTokenService() { | 161 ProfileOAuth2TokenService* OAuth2LoginManager::GetTokenService() { |
| 170 return ProfileOAuth2TokenServiceFactory::GetForProfile(user_profile_); | 162 return ProfileOAuth2TokenServiceFactory::GetForProfile(user_profile_); |
| 171 } | 163 } |
| 172 | 164 |
| 173 const std::string& OAuth2LoginManager::GetPrimaryAccountId() { | 165 std::string OAuth2LoginManager::GetPrimaryAccountId() { |
| 174 SigninManagerBase* signin_manager = | 166 SigninManagerBase* signin_manager = |
| 175 SigninManagerFactory::GetForProfile(user_profile_); | 167 SigninManagerFactory::GetForProfile(user_profile_); |
| 176 return signin_manager->GetAuthenticatedAccountId(); | 168 const std::string primary_account_id = |
| 169 signin_manager->GetAuthenticatedAccountId(); |
| 170 LOG_IF(ERROR, primary_account_id.empty()) << "Primary account id is empty."; |
| 171 return primary_account_id; |
| 177 } | 172 } |
| 178 | 173 |
| 179 void OAuth2LoginManager::StoreOAuth2Token() { | 174 void OAuth2LoginManager::StoreOAuth2Token() { |
| 180 const std::string& primary_account_id = GetPrimaryAccountId(); | 175 UpdateCredentials(GetPrimaryAccountId()); |
| 181 if (primary_account_id.empty()) { | |
| 182 GetAccountInfoOfRefreshToken(refresh_token_); | |
| 183 return; | |
| 184 } | |
| 185 | |
| 186 UpdateCredentials(primary_account_id); | |
| 187 } | |
| 188 | |
| 189 void OAuth2LoginManager::GetAccountInfoOfRefreshToken( | |
| 190 const std::string& refresh_token) { | |
| 191 gaia::OAuthClientInfo client_info; | |
| 192 GaiaUrls* gaia_urls = GaiaUrls::GetInstance(); | |
| 193 client_info.client_id = gaia_urls->oauth2_chrome_client_id(); | |
| 194 client_info.client_secret = gaia_urls->oauth2_chrome_client_secret(); | |
| 195 | |
| 196 account_info_fetcher_.reset(new gaia::GaiaOAuthClient( | |
| 197 auth_request_context_.get())); | |
| 198 account_info_fetcher_->RefreshToken(client_info, refresh_token, | |
| 199 std::vector<std::string>(1, kServiceScopeGetUserInfo), kMaxRetries, | |
| 200 this); | |
| 201 } | 176 } |
| 202 | 177 |
| 203 void OAuth2LoginManager::UpdateCredentials(const std::string& account_id) { | 178 void OAuth2LoginManager::UpdateCredentials(const std::string& account_id) { |
| 204 DCHECK(!account_id.empty()); | 179 DCHECK(!account_id.empty()); |
| 205 DCHECK(!refresh_token_.empty()); | 180 DCHECK(!refresh_token_.empty()); |
| 206 // |account_id| is assumed to be already canonicalized if it's an email. | 181 // |account_id| is assumed to be already canonicalized if it's an email. |
| 207 GetTokenService()->UpdateCredentials(account_id, refresh_token_); | 182 GetTokenService()->UpdateCredentials(account_id, refresh_token_); |
| 208 FireRefreshTokensLoaded(); | 183 FireRefreshTokensLoaded(); |
| 209 | 184 |
| 210 for (auto& observer : observer_list_) | 185 for (auto& observer : observer_list_) |
| 211 observer.OnNewRefreshTokenAvaiable(user_profile_); | 186 observer.OnNewRefreshTokenAvaiable(user_profile_); |
| 212 } | 187 } |
| 213 | 188 |
| 214 void OAuth2LoginManager::FireRefreshTokensLoaded() { | 189 void OAuth2LoginManager::FireRefreshTokensLoaded() { |
| 215 // TODO(570218): Figure out the right way to plumb this. | 190 // TODO(570218): Figure out the right way to plumb this. |
| 216 GetTokenService()->LoadCredentials(std::string()); | 191 GetTokenService()->LoadCredentials(std::string()); |
| 217 } | 192 } |
| 218 | 193 |
| 219 void OAuth2LoginManager::OnRefreshTokenResponse( | |
| 220 const std::string& access_token, | |
| 221 int expires_in_seconds) { | |
| 222 account_info_fetcher_->GetUserInfo(access_token, kMaxRetries, this); | |
| 223 } | |
| 224 | |
| 225 void OAuth2LoginManager::OnGetUserInfoResponse( | |
| 226 std::unique_ptr<base::DictionaryValue> user_info) { | |
| 227 account_info_fetcher_.reset(); | |
| 228 | |
| 229 std::string gaia_id; | |
| 230 std::string email; | |
| 231 user_info->GetString("id", &gaia_id); | |
| 232 user_info->GetString("email", &email); | |
| 233 | |
| 234 AccountTrackerService* account_tracker = | |
| 235 AccountTrackerServiceFactory::GetForProfile(user_profile_); | |
| 236 account_tracker->SeedAccountInfo(gaia_id, email); | |
| 237 UpdateCredentials(account_tracker->PickAccountIdForAccount(gaia_id, email)); | |
| 238 } | |
| 239 | |
| 240 void OAuth2LoginManager::OnOAuthError() { | |
| 241 account_info_fetcher_.reset(); | |
| 242 LOG(ERROR) << "Account info fetch failed!"; | |
| 243 SetSessionRestoreState(OAuth2LoginManager::SESSION_RESTORE_FAILED); | |
| 244 } | |
| 245 | |
| 246 void OAuth2LoginManager::OnNetworkError(int response_code) { | |
| 247 account_info_fetcher_.reset(); | |
| 248 LOG(ERROR) << "Account info fetch failed! response_code=" << response_code; | |
| 249 SetSessionRestoreState(OAuth2LoginManager::SESSION_RESTORE_FAILED); | |
| 250 } | |
| 251 | |
| 252 void OAuth2LoginManager::FetchOAuth2Tokens() { | 194 void OAuth2LoginManager::FetchOAuth2Tokens() { |
| 253 DCHECK(auth_request_context_.get()); | 195 DCHECK(auth_request_context_.get()); |
| 254 if (restore_strategy_ != RESTORE_FROM_COOKIE_JAR) { | 196 if (restore_strategy_ != RESTORE_FROM_COOKIE_JAR) { |
| 255 NOTREACHED(); | 197 NOTREACHED(); |
| 256 SetSessionRestoreState(SESSION_RESTORE_FAILED); | 198 SetSessionRestoreState(SESSION_RESTORE_FAILED); |
| 257 return; | 199 return; |
| 258 } | 200 } |
| 259 | 201 |
| 260 // If we have authenticated cookie jar, get OAuth1 token first, then fetch | 202 // If we have authenticated cookie jar, get OAuth1 token first, then fetch |
| 261 // SID/LSID cookies through OAuthLogin call. | 203 // SID/LSID cookies through OAuthLogin call. |
| (...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 428 for (auto& observer : observer_list_) | 370 for (auto& observer : observer_list_) |
| 429 observer.OnSessionRestoreStateChanged(user_profile_, state_); | 371 observer.OnSessionRestoreStateChanged(user_profile_, state_); |
| 430 } | 372 } |
| 431 | 373 |
| 432 void OAuth2LoginManager::SetSessionRestoreStartForTesting( | 374 void OAuth2LoginManager::SetSessionRestoreStartForTesting( |
| 433 const base::Time& time) { | 375 const base::Time& time) { |
| 434 session_restore_start_ = time; | 376 session_restore_start_ = time; |
| 435 } | 377 } |
| 436 | 378 |
| 437 } // namespace chromeos | 379 } // namespace chromeos |
| OLD | NEW |