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