Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/oauth_login_verifier.h" | 5 #include "chrome/browser/chromeos/login/oauth1_login_verifier.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/metrics/histogram.h" | 8 #include "base/metrics/histogram.h" |
| 9 #include "chrome/browser/browser_process.h" | 9 #include "chrome/browser/browser_process.h" |
| 10 #include "chrome/browser/chromeos/cros/cros_library.h" | 10 #include "chrome/browser/chromeos/cros/cros_library.h" |
| 11 #include "chrome/browser/chromeos/cros/network_library.h" | 11 #include "chrome/browser/chromeos/cros/network_library.h" |
| 12 #include "chrome/browser/profiles/profile_manager.h" | 12 #include "chrome/browser/profiles/profile_manager.h" |
|
Joao da Silva
2013/01/11 16:45:07
Not needed
zel
2013/01/11 19:51:16
Done.
| |
| 13 #include "content/public/browser/browser_thread.h" | 13 #include "content/public/browser/browser_thread.h" |
| 14 #include "google_apis/gaia/gaia_constants.h" | 14 #include "google_apis/gaia/gaia_constants.h" |
| 15 #include "google_apis/gaia/google_service_auth_error.h" | 15 #include "google_apis/gaia/google_service_auth_error.h" |
| 16 | 16 |
| 17 using content::BrowserThread; | 17 using content::BrowserThread; |
| 18 | 18 |
| 19 namespace chromeos { | 19 namespace chromeos { |
| 20 | 20 |
| 21 namespace { | 21 namespace { |
| 22 | 22 |
| 23 // OAuth token verification max retry count. | 23 // OAuth token verification max retry count. |
| 24 const int kMaxOAuthTokenVerificationAttemptCount = 5; | 24 const int kMaxOAuthTokenVerificationAttemptCount = 5; |
| 25 // OAuth token verification retry delay in milliseconds. | 25 // OAuth token verification retry delay in milliseconds. |
| 26 const int kOAuthVerificationRestartDelay = 10000; | 26 const int kOAuthVerificationRestartDelay = 10000; |
| 27 | 27 |
| 28 // The service scope of the OAuth v2 token that ChromeOS login will be | 28 // The service scope of the OAuth v2 token that ChromeOS login will be |
| 29 // requesting. | 29 // requesting. |
| 30 const char kServiceScopeChromeOS[] = | 30 const char kServiceScopeChromeOS[] = |
| 31 "https://www.googleapis.com/auth/chromesync"; | 31 "https://www.googleapis.com/auth/chromesync"; |
| 32 | 32 |
| 33 } // namespace | 33 } // namespace |
| 34 | 34 |
| 35 OAuthLoginVerifier::OAuthLoginVerifier(OAuthLoginVerifier::Delegate* delegate, | 35 OAuth1LoginVerifier::OAuth1LoginVerifier( |
| 36 Profile* user_profile, | 36 OAuth1LoginVerifier::Delegate* delegate, |
| 37 const std::string& oauth1_token, | 37 net::URLRequestContextGetter* user_request_context, |
| 38 const std::string& oauth1_secret, | 38 const std::string& oauth1_token, |
| 39 const std::string& username) | 39 const std::string& oauth1_secret, |
| 40 const std::string& username) | |
| 40 : delegate_(delegate), | 41 : delegate_(delegate), |
| 41 oauth_fetcher_(this, | 42 oauth_fetcher_(this, |
| 42 g_browser_process->system_request_context(), | 43 g_browser_process->system_request_context(), |
| 43 kServiceScopeChromeOS), | 44 kServiceScopeChromeOS), |
| 44 gaia_fetcher_(this, | 45 gaia_fetcher_(this, |
| 45 std::string(GaiaConstants::kChromeOSSource), | 46 std::string(GaiaConstants::kChromeOSSource), |
| 46 user_profile->GetRequestContext()), | 47 user_request_context), |
| 47 oauth1_token_(oauth1_token), | 48 oauth1_token_(oauth1_token), |
| 48 oauth1_secret_(oauth1_secret), | 49 oauth1_secret_(oauth1_secret), |
| 49 username_(username), | 50 username_(username), |
| 50 user_profile_(user_profile), | |
| 51 verification_count_(0), | 51 verification_count_(0), |
| 52 step_(VERIFICATION_STEP_UNVERIFIED) { | 52 step_(VERIFICATION_STEP_UNVERIFIED) { |
| 53 } | 53 } |
| 54 | 54 |
| 55 OAuthLoginVerifier::~OAuthLoginVerifier() { | 55 OAuth1LoginVerifier::~OAuth1LoginVerifier() { |
| 56 } | 56 } |
| 57 | 57 |
| 58 void OAuthLoginVerifier::StartOAuthVerification() { | 58 void OAuth1LoginVerifier::StartOAuthVerification() { |
| 59 if (oauth1_token_.empty() || oauth1_secret_.empty()) { | 59 if (oauth1_token_.empty() || oauth1_secret_.empty()) { |
| 60 // Empty OAuth1 access token or secret probably means that we are | 60 // Empty OAuth1 access token or secret probably means that we are |
| 61 // dealing with a legacy ChromeOS account. This should be treated as | 61 // dealing with a legacy ChromeOS account. This should be treated as |
| 62 // invalid/expired token. | 62 // invalid/expired token. |
| 63 OnOAuthLoginFailure(GoogleServiceAuthError( | 63 OnOAuthLoginFailure(GoogleServiceAuthError( |
| 64 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS)); | 64 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS)); |
| 65 } else { | 65 } else { |
| 66 oauth_fetcher_.StartOAuthLogin(GaiaConstants::kChromeOSSource, | 66 oauth_fetcher_.StartOAuthLogin(GaiaConstants::kChromeOSSource, |
| 67 GaiaConstants::kPicasaService, | 67 GaiaConstants::kSyncService, |
| 68 oauth1_token_, | 68 oauth1_token_, |
| 69 oauth1_secret_); | 69 oauth1_secret_); |
| 70 } | 70 } |
| 71 } | 71 } |
| 72 | 72 |
| 73 void OAuthLoginVerifier::ContinueVerification() { | 73 void OAuth1LoginVerifier::ContinueVerification() { |
| 74 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 74 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 75 // Check if we have finished with this one already. | 75 // Check if we have finished with this one already. |
| 76 if (is_done()) | 76 if (is_done()) |
| 77 return; | 77 return; |
| 78 | 78 |
| 79 if (user_profile_ != ProfileManager::GetDefaultProfile()) | |
| 80 return; | |
| 81 | |
| 82 // Check if we currently trying to fetch something. | 79 // Check if we currently trying to fetch something. |
| 83 if (oauth_fetcher_.HasPendingFetch() || gaia_fetcher_.HasPendingFetch()) | 80 if (oauth_fetcher_.HasPendingFetch() || gaia_fetcher_.HasPendingFetch()) |
| 84 return; | 81 return; |
| 85 | 82 |
| 86 if (CrosLibrary::Get()->libcros_loaded()) { | 83 if (CrosLibrary::Get()->libcros_loaded()) { |
| 87 // Delay the verification if the network is not connected or on a captive | 84 // Delay the verification if the network is not connected or on a captive |
| 88 // portal. | 85 // portal. |
| 89 const Network* network = | 86 const Network* network = |
| 90 CrosLibrary::Get()->GetNetworkLibrary()->active_network(); | 87 CrosLibrary::Get()->GetNetworkLibrary()->active_network(); |
| 91 if (!network || !network->connected() || network->restricted_pool()) { | 88 if (!network || !network->connected() || network->restricted_pool()) { |
| 92 BrowserThread::PostDelayedTask( | 89 BrowserThread::PostDelayedTask( |
| 93 BrowserThread::UI, FROM_HERE, | 90 BrowserThread::UI, FROM_HERE, |
| 94 base::Bind(&OAuthLoginVerifier::ContinueVerification, AsWeakPtr()), | 91 base::Bind(&OAuth1LoginVerifier::ContinueVerification, AsWeakPtr()), |
| 95 base::TimeDelta::FromMilliseconds(kOAuthVerificationRestartDelay)); | 92 base::TimeDelta::FromMilliseconds(kOAuthVerificationRestartDelay)); |
| 96 return; | 93 return; |
| 97 } | 94 } |
| 98 } | 95 } |
| 99 | 96 |
| 100 verification_count_++; | 97 verification_count_++; |
| 101 if (step_ == VERIFICATION_STEP_UNVERIFIED) { | 98 if (step_ == VERIFICATION_STEP_UNVERIFIED) { |
| 102 DVLOG(1) << "Retrying to verify OAuth1 access tokens."; | 99 DVLOG(1) << "Retrying to verify OAuth1 access tokens."; |
| 103 StartOAuthVerification(); | 100 StartOAuthVerification(); |
| 104 } else { | 101 } else { |
| 105 DVLOG(1) << "Retrying to fetch user cookies."; | 102 DVLOG(1) << "Retrying to fetch user cookies."; |
| 106 StartCookiesRetrieval(); | 103 StartCookiesRetrieval(); |
| 107 } | 104 } |
| 108 } | 105 } |
| 109 | 106 |
| 110 void OAuthLoginVerifier::StartCookiesRetrieval() { | 107 void OAuth1LoginVerifier::StartCookiesRetrieval() { |
| 111 DCHECK(!sid_.empty()); | 108 DCHECK(!sid_.empty()); |
| 112 DCHECK(!lsid_.empty()); | 109 DCHECK(!lsid_.empty()); |
| 113 gaia_fetcher_.StartIssueAuthToken(sid_, lsid_, GaiaConstants::kGaiaService); | 110 gaia_fetcher_.StartIssueAuthToken(sid_, lsid_, GaiaConstants::kGaiaService); |
| 114 } | 111 } |
| 115 | 112 |
| 116 bool OAuthLoginVerifier::RetryOnError(const GoogleServiceAuthError& error) { | 113 bool OAuth1LoginVerifier::RetryOnError(const GoogleServiceAuthError& error) { |
| 117 if (error.state() == GoogleServiceAuthError::CONNECTION_FAILED || | 114 if (error.state() == GoogleServiceAuthError::CONNECTION_FAILED || |
| 118 error.state() == GoogleServiceAuthError::SERVICE_UNAVAILABLE || | 115 error.state() == GoogleServiceAuthError::SERVICE_UNAVAILABLE || |
| 119 error.state() == GoogleServiceAuthError::REQUEST_CANCELED) { | 116 error.state() == GoogleServiceAuthError::REQUEST_CANCELED) { |
| 120 if (verification_count_ < kMaxOAuthTokenVerificationAttemptCount) { | 117 if (verification_count_ < kMaxOAuthTokenVerificationAttemptCount) { |
| 121 BrowserThread::PostDelayedTask( | 118 BrowserThread::PostDelayedTask( |
| 122 BrowserThread::UI, FROM_HERE, | 119 BrowserThread::UI, FROM_HERE, |
| 123 base::Bind(&OAuthLoginVerifier::ContinueVerification, AsWeakPtr()), | 120 base::Bind(&OAuth1LoginVerifier::ContinueVerification, AsWeakPtr()), |
| 124 base::TimeDelta::FromMilliseconds(kOAuthVerificationRestartDelay)); | 121 base::TimeDelta::FromMilliseconds(kOAuthVerificationRestartDelay)); |
| 125 return true; | 122 return true; |
| 126 } | 123 } |
| 127 } | 124 } |
| 128 step_ = VERIFICATION_STEP_FAILED; | 125 step_ = VERIFICATION_STEP_FAILED; |
| 129 return false; | 126 return false; |
| 130 } | 127 } |
| 131 | 128 |
| 132 void OAuthLoginVerifier::OnOAuthLoginSuccess(const std::string& sid, | 129 void OAuth1LoginVerifier::OnOAuthLoginSuccess(const std::string& sid, |
| 133 const std::string& lsid, | 130 const std::string& lsid, |
| 134 const std::string& auth) { | 131 const std::string& auth) { |
| 135 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 132 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 136 step_ = VERIFICATION_STEP_OAUTH_VERIFIED; | 133 step_ = VERIFICATION_STEP_OAUTH_VERIFIED; |
| 137 verification_count_ = 0; | 134 verification_count_ = 0; |
| 138 sid_ = sid; | 135 sid_ = sid; |
| 139 lsid_ = lsid; | 136 lsid_ = lsid; |
| 140 delegate_->OnOAuthVerificationSucceeded(username_, sid, lsid, auth); | 137 delegate_->OnOAuth1VerificationSucceeded(username_, sid, lsid, auth); |
| 141 StartCookiesRetrieval(); | 138 StartCookiesRetrieval(); |
| 142 } | 139 } |
| 143 | 140 |
| 144 void OAuthLoginVerifier::OnOAuthLoginFailure( | 141 void OAuth1LoginVerifier::OnOAuthLoginFailure( |
| 145 const GoogleServiceAuthError& error) { | 142 const GoogleServiceAuthError& error) { |
| 146 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 143 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 147 LOG(WARNING) << "Failed to verify OAuth1 access tokens," | 144 LOG(WARNING) << "Failed to verify OAuth1 access tokens," |
| 148 << " error.state=" << error.state(); | 145 << " error: " << error.state(); |
| 149 | 146 |
| 150 if (!RetryOnError(error)) { | 147 if (!RetryOnError(error)) { |
| 151 UMA_HISTOGRAM_ENUMERATION("LoginVerifier.LoginFailureWithNoRetry", | 148 UMA_HISTOGRAM_ENUMERATION("LoginVerifier.LoginFailureWithNoRetry", |
| 152 error.state(), | 149 error.state(), |
| 153 GoogleServiceAuthError::NUM_STATES); | 150 GoogleServiceAuthError::NUM_STATES); |
| 154 delegate_->OnOAuthVerificationFailed(username_); | 151 delegate_->OnOAuth1VerificationFailed(username_); |
| 155 } else { | 152 } else { |
| 156 UMA_HISTOGRAM_ENUMERATION("LoginVerifier.LoginFailureWithRetry", | 153 UMA_HISTOGRAM_ENUMERATION("LoginVerifier.LoginFailureWithRetry", |
| 157 error.state(), | 154 error.state(), |
| 158 GoogleServiceAuthError::NUM_STATES); | 155 GoogleServiceAuthError::NUM_STATES); |
| 159 } | 156 } |
| 160 } | 157 } |
| 161 | 158 |
| 162 void OAuthLoginVerifier::OnCookieFetchFailed( | 159 void OAuth1LoginVerifier::OnCookieFetchFailed( |
| 163 const GoogleServiceAuthError& error) { | 160 const GoogleServiceAuthError& error) { |
| 164 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 161 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 165 | 162 |
| 166 if (!RetryOnError(error)) { | 163 if (!RetryOnError(error)) { |
| 167 UMA_HISTOGRAM_ENUMERATION("LoginVerifier.CookieFetchFailureWithNoRetry", | 164 UMA_HISTOGRAM_ENUMERATION("LoginVerifier.CookieFetchFailureWithNoRetry", |
| 168 error.state(), | 165 error.state(), |
| 169 GoogleServiceAuthError::NUM_STATES); | 166 GoogleServiceAuthError::NUM_STATES); |
| 170 delegate_->OnUserCookiesFetchFailed(username_); | 167 delegate_->OnCookiesFetchWithOAuth1Failed(username_); |
| 171 } else { | 168 } else { |
| 172 UMA_HISTOGRAM_ENUMERATION("LoginVerifier.CookieFetchFailureWithRetry", | 169 UMA_HISTOGRAM_ENUMERATION("LoginVerifier.CookieFetchFailureWithRetry", |
| 173 error.state(), | 170 error.state(), |
| 174 GoogleServiceAuthError::NUM_STATES); | 171 GoogleServiceAuthError::NUM_STATES); |
| 175 } | 172 } |
| 176 } | 173 } |
| 177 | 174 |
| 178 void OAuthLoginVerifier::OnIssueAuthTokenSuccess( | 175 void OAuth1LoginVerifier::OnIssueAuthTokenSuccess( |
| 179 const std::string& service, | 176 const std::string& service, |
| 180 const std::string& auth_token) { | 177 const std::string& auth_token) { |
| 181 gaia_fetcher_.StartMergeSession(auth_token); | 178 gaia_fetcher_.StartMergeSession(auth_token); |
| 182 } | 179 } |
| 183 | 180 |
| 184 void OAuthLoginVerifier::OnIssueAuthTokenFailure( | 181 void OAuth1LoginVerifier::OnIssueAuthTokenFailure( |
| 185 const std::string& service, | 182 const std::string& service, |
| 186 const GoogleServiceAuthError& error) { | 183 const GoogleServiceAuthError& error) { |
| 187 DVLOG(1) << "Failed IssueAuthToken request," | 184 LOG(WARNING) << "Failed IssueAuthToken request," |
| 188 << " error.state=" << error.state(); | 185 << " error: " << error.state(); |
| 189 OnCookieFetchFailed(error); | 186 OnCookieFetchFailed(error); |
| 190 } | 187 } |
| 191 | 188 |
| 192 void OAuthLoginVerifier::OnMergeSessionSuccess(const std::string& data) { | 189 void OAuth1LoginVerifier::OnMergeSessionSuccess(const std::string& data) { |
| 193 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 190 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 194 DVLOG(1) << "MergeSession successful."; | 191 LOG(WARNING) << "MergeSession successful."; |
|
Joao da Silva
2013/01/11 16:45:07
warning on success?
zel
2013/01/11 19:51:16
Yeah. I want to make sure we explicitly see this e
| |
| 195 step_ = VERIFICATION_STEP_COOKIES_FETCHED; | 192 step_ = VERIFICATION_STEP_COOKIES_FETCHED; |
| 196 delegate_->OnUserCookiesFetchSucceeded(username_); | 193 delegate_->OnCookiesFetchWithOAuth1Succeeded(username_); |
| 197 } | 194 } |
| 198 | 195 |
| 199 void OAuthLoginVerifier::OnMergeSessionFailure( | 196 void OAuth1LoginVerifier::OnMergeSessionFailure( |
| 200 const GoogleServiceAuthError& error) { | 197 const GoogleServiceAuthError& error) { |
| 201 DVLOG(1) << "Failed MergeSession request," | 198 LOG(WARNING) << "Failed MergeSession request," |
| 202 << " error.state=" << error.state(); | 199 << " error: " << error.state(); |
| 203 OnCookieFetchFailed(error); | 200 OnCookieFetchFailed(error); |
| 204 } | 201 } |
| 205 | 202 |
| 206 } // namespace chromeos | 203 } // namespace chromeos |
| OLD | NEW |