Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(206)

Side by Side Diff: chrome/browser/chromeos/login/oauth1_login_verifier.cc

Issue 11649055: OAuth2 sign-in flow for ChromeOS (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: clang fix Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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"
13 #include "content/public/browser/browser_thread.h" 12 #include "content/public/browser/browser_thread.h"
14 #include "google_apis/gaia/gaia_constants.h" 13 #include "google_apis/gaia/gaia_constants.h"
15 #include "google_apis/gaia/google_service_auth_error.h" 14 #include "google_apis/gaia/google_service_auth_error.h"
16 15
17 using content::BrowserThread; 16 using content::BrowserThread;
18 17
19 namespace chromeos { 18 namespace chromeos {
20 19
21 namespace { 20 namespace {
22 21
23 // OAuth token verification max retry count. 22 // OAuth token verification max retry count.
24 const int kMaxOAuthTokenVerificationAttemptCount = 5; 23 const int kMaxOAuthTokenVerificationAttemptCount = 5;
25 // OAuth token verification retry delay in milliseconds. 24 // OAuth token verification retry delay in milliseconds.
26 const int kOAuthVerificationRestartDelay = 10000; 25 const int kOAuthVerificationRestartDelay = 10000;
27 26
28 // The service scope of the OAuth v2 token that ChromeOS login will be 27 // The service scope of the OAuth v2 token that ChromeOS login will be
29 // requesting. 28 // requesting.
30 const char kServiceScopeChromeOS[] = 29 const char kServiceScopeChromeOS[] =
31 "https://www.googleapis.com/auth/chromesync"; 30 "https://www.googleapis.com/auth/chromesync";
32 31
33 } // namespace 32 } // namespace
34 33
35 OAuthLoginVerifier::OAuthLoginVerifier(OAuthLoginVerifier::Delegate* delegate, 34 OAuth1LoginVerifier::OAuth1LoginVerifier(
36 Profile* user_profile, 35 OAuth1LoginVerifier::Delegate* delegate,
37 const std::string& oauth1_token, 36 net::URLRequestContextGetter* user_request_context,
38 const std::string& oauth1_secret, 37 const std::string& oauth1_token,
39 const std::string& username) 38 const std::string& oauth1_secret,
39 const std::string& username)
40 : delegate_(delegate), 40 : delegate_(delegate),
41 oauth_fetcher_(this, 41 oauth_fetcher_(this,
42 g_browser_process->system_request_context(), 42 g_browser_process->system_request_context(),
43 kServiceScopeChromeOS), 43 kServiceScopeChromeOS),
44 gaia_fetcher_(this, 44 gaia_fetcher_(this,
45 std::string(GaiaConstants::kChromeOSSource), 45 std::string(GaiaConstants::kChromeOSSource),
46 user_profile->GetRequestContext()), 46 user_request_context),
47 oauth1_token_(oauth1_token), 47 oauth1_token_(oauth1_token),
48 oauth1_secret_(oauth1_secret), 48 oauth1_secret_(oauth1_secret),
49 username_(username), 49 username_(username),
50 user_profile_(user_profile),
51 verification_count_(0), 50 verification_count_(0),
52 step_(VERIFICATION_STEP_UNVERIFIED) { 51 step_(VERIFICATION_STEP_UNVERIFIED) {
53 } 52 }
54 53
55 OAuthLoginVerifier::~OAuthLoginVerifier() { 54 OAuth1LoginVerifier::~OAuth1LoginVerifier() {
56 } 55 }
57 56
58 void OAuthLoginVerifier::StartOAuthVerification() { 57 void OAuth1LoginVerifier::StartOAuthVerification() {
59 if (oauth1_token_.empty() || oauth1_secret_.empty()) { 58 if (oauth1_token_.empty() || oauth1_secret_.empty()) {
60 // Empty OAuth1 access token or secret probably means that we are 59 // Empty OAuth1 access token or secret probably means that we are
61 // dealing with a legacy ChromeOS account. This should be treated as 60 // dealing with a legacy ChromeOS account. This should be treated as
62 // invalid/expired token. 61 // invalid/expired token.
63 OnOAuthLoginFailure(GoogleServiceAuthError( 62 OnOAuthLoginFailure(GoogleServiceAuthError(
64 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS)); 63 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS));
65 } else { 64 } else {
66 oauth_fetcher_.StartOAuthLogin(GaiaConstants::kChromeOSSource, 65 oauth_fetcher_.StartOAuthLogin(GaiaConstants::kChromeOSSource,
67 GaiaConstants::kPicasaService, 66 GaiaConstants::kSyncService,
68 oauth1_token_, 67 oauth1_token_,
69 oauth1_secret_); 68 oauth1_secret_);
70 } 69 }
71 } 70 }
72 71
73 void OAuthLoginVerifier::ContinueVerification() { 72 void OAuth1LoginVerifier::ContinueVerification() {
74 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 73 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
75 // Check if we have finished with this one already. 74 // Check if we have finished with this one already.
76 if (is_done()) 75 if (is_done())
77 return; 76 return;
78 77
79 if (user_profile_ != ProfileManager::GetDefaultProfile())
80 return;
81
82 // Check if we currently trying to fetch something. 78 // Check if we currently trying to fetch something.
83 if (oauth_fetcher_.HasPendingFetch() || gaia_fetcher_.HasPendingFetch()) 79 if (oauth_fetcher_.HasPendingFetch() || gaia_fetcher_.HasPendingFetch())
84 return; 80 return;
85 81
86 if (CrosLibrary::Get()->libcros_loaded()) { 82 if (CrosLibrary::Get()->libcros_loaded()) {
87 // Delay the verification if the network is not connected or on a captive 83 // Delay the verification if the network is not connected or on a captive
88 // portal. 84 // portal.
89 const Network* network = 85 const Network* network =
90 CrosLibrary::Get()->GetNetworkLibrary()->active_network(); 86 CrosLibrary::Get()->GetNetworkLibrary()->active_network();
91 if (!network || !network->connected() || network->restricted_pool()) { 87 if (!network || !network->connected() || network->restricted_pool()) {
92 BrowserThread::PostDelayedTask( 88 BrowserThread::PostDelayedTask(
93 BrowserThread::UI, FROM_HERE, 89 BrowserThread::UI, FROM_HERE,
94 base::Bind(&OAuthLoginVerifier::ContinueVerification, AsWeakPtr()), 90 base::Bind(&OAuth1LoginVerifier::ContinueVerification, AsWeakPtr()),
95 base::TimeDelta::FromMilliseconds(kOAuthVerificationRestartDelay)); 91 base::TimeDelta::FromMilliseconds(kOAuthVerificationRestartDelay));
96 return; 92 return;
97 } 93 }
98 } 94 }
99 95
100 verification_count_++; 96 verification_count_++;
101 if (step_ == VERIFICATION_STEP_UNVERIFIED) { 97 if (step_ == VERIFICATION_STEP_UNVERIFIED) {
102 DVLOG(1) << "Retrying to verify OAuth1 access tokens."; 98 LOG(INFO) << "Retrying to verify OAuth1 access tokens.";
103 StartOAuthVerification(); 99 StartOAuthVerification();
104 } else { 100 } else {
105 DVLOG(1) << "Retrying to fetch user cookies."; 101 LOG(INFO) << "Retrying to fetch user cookies.";
106 StartCookiesRetrieval(); 102 StartCookiesRetrieval();
107 } 103 }
108 } 104 }
109 105
110 void OAuthLoginVerifier::StartCookiesRetrieval() { 106 void OAuth1LoginVerifier::StartCookiesRetrieval() {
111 DCHECK(!sid_.empty()); 107 DCHECK(!sid_.empty());
112 DCHECK(!lsid_.empty()); 108 DCHECK(!lsid_.empty());
113 gaia_fetcher_.StartIssueAuthToken(sid_, lsid_, GaiaConstants::kGaiaService); 109 gaia_fetcher_.StartIssueAuthToken(sid_, lsid_, GaiaConstants::kGaiaService);
114 } 110 }
115 111
116 bool OAuthLoginVerifier::RetryOnError(const GoogleServiceAuthError& error) { 112 bool OAuth1LoginVerifier::RetryOnError(const GoogleServiceAuthError& error) {
117 if (error.state() == GoogleServiceAuthError::CONNECTION_FAILED || 113 if (error.state() == GoogleServiceAuthError::CONNECTION_FAILED ||
118 error.state() == GoogleServiceAuthError::SERVICE_UNAVAILABLE || 114 error.state() == GoogleServiceAuthError::SERVICE_UNAVAILABLE ||
119 error.state() == GoogleServiceAuthError::REQUEST_CANCELED) { 115 error.state() == GoogleServiceAuthError::REQUEST_CANCELED) {
120 if (verification_count_ < kMaxOAuthTokenVerificationAttemptCount) { 116 if (verification_count_ < kMaxOAuthTokenVerificationAttemptCount) {
121 BrowserThread::PostDelayedTask( 117 BrowserThread::PostDelayedTask(
122 BrowserThread::UI, FROM_HERE, 118 BrowserThread::UI, FROM_HERE,
123 base::Bind(&OAuthLoginVerifier::ContinueVerification, AsWeakPtr()), 119 base::Bind(&OAuth1LoginVerifier::ContinueVerification, AsWeakPtr()),
124 base::TimeDelta::FromMilliseconds(kOAuthVerificationRestartDelay)); 120 base::TimeDelta::FromMilliseconds(kOAuthVerificationRestartDelay));
125 return true; 121 return true;
126 } 122 }
127 } 123 }
128 step_ = VERIFICATION_STEP_FAILED; 124 step_ = VERIFICATION_STEP_FAILED;
129 return false; 125 return false;
130 } 126 }
131 127
132 void OAuthLoginVerifier::OnOAuthLoginSuccess(const std::string& sid, 128 void OAuth1LoginVerifier::OnOAuthLoginSuccess(const std::string& sid,
133 const std::string& lsid, 129 const std::string& lsid,
134 const std::string& auth) { 130 const std::string& auth) {
131 LOG(INFO) << "OAuthLogin successful";
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(ERROR) << "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) {
178 LOG(INFO) << "IssueAuthToken successful";
181 gaia_fetcher_.StartMergeSession(auth_token); 179 gaia_fetcher_.StartMergeSession(auth_token);
182 } 180 }
183 181
184 void OAuthLoginVerifier::OnIssueAuthTokenFailure( 182 void OAuth1LoginVerifier::OnIssueAuthTokenFailure(
185 const std::string& service, 183 const std::string& service,
186 const GoogleServiceAuthError& error) { 184 const GoogleServiceAuthError& error) {
187 DVLOG(1) << "Failed IssueAuthToken request," 185 LOG(ERROR) << "IssueAuthToken failed,"
188 << " error.state=" << error.state(); 186 << " error: " << error.state();
189 OnCookieFetchFailed(error); 187 OnCookieFetchFailed(error);
190 } 188 }
191 189
192 void OAuthLoginVerifier::OnMergeSessionSuccess(const std::string& data) { 190 void OAuth1LoginVerifier::OnMergeSessionSuccess(const std::string& data) {
193 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 191 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
194 DVLOG(1) << "MergeSession successful."; 192 LOG(INFO) << "MergeSession successful.";
195 step_ = VERIFICATION_STEP_COOKIES_FETCHED; 193 step_ = VERIFICATION_STEP_COOKIES_FETCHED;
196 delegate_->OnUserCookiesFetchSucceeded(username_); 194 delegate_->OnCookiesFetchWithOAuth1Succeeded(username_);
197 } 195 }
198 196
199 void OAuthLoginVerifier::OnMergeSessionFailure( 197 void OAuth1LoginVerifier::OnMergeSessionFailure(
200 const GoogleServiceAuthError& error) { 198 const GoogleServiceAuthError& error) {
201 DVLOG(1) << "Failed MergeSession request," 199 LOG(ERROR) << "Failed MergeSession request,"
202 << " error.state=" << error.state(); 200 << " error: " << error.state();
203 OnCookieFetchFailed(error); 201 OnCookieFetchFailed(error);
204 } 202 }
205 203
206 } // namespace chromeos 204 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698