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

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

Issue 11649055: OAuth2 sign-in flow for ChromeOS (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: 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
(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/oauth2_login_verifier.h"
6
7 #include "base/logging.h"
8 #include "base/metrics/histogram.h"
9 #include "base/stringprintf.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.h"
13 #include "content/public/browser/browser_thread.h"
14 #include "google_apis/gaia/gaia_constants.h"
15 #include "google_apis/gaia/gaia_urls.h"
16 #include "google_apis/gaia/oauth2_access_token_fetcher.h"
17
18 using content::BrowserThread;
19
20 namespace {
21
22 // OAuth token request max retry count.
23 const int kMaxRequestAttemptCount = 5;
24 // OAuth token request retry delay in milliseconds.
25 const int kRequestRestartDelay = 3000;
26
27 // The service scope of the OAuth v2 token that ChromeOS login will be
28 // requesting.
29 const char kServiceScopeChromeOS[] =
30 "https://www.googleapis.com/auth/chromesync";
31
32 const char kOAuthLoginServiceScope[] =
33 "https://www.google.com/accounts/OAuthLogin";
34
35 } // namespace
36
37 namespace chromeos {
38
39 OAuth2LoginVerifier::OAuth2LoginVerifier(
40 OAuth2LoginVerifier::Delegate* delegate,
41 net::URLRequestContextGetter* system_request_context,
42 Profile* user_profile)
xiyuan 2013/01/07 23:07:55 nit: 4-space indent for above three lines
zel 2013/01/08 02:05:41 Done.
43 : delegate_(delegate),
44 token_fetcher_(this, system_request_context),
45 gaia_fetcher_(this, std::string(GaiaConstants::kChromeOSSource),
46 user_profile->GetRequestContext()),
47 retry_count_(0) {
48 DCHECK(delegate);
49 }
50
51 OAuth2LoginVerifier::~OAuth2LoginVerifier() {
52 }
53
54 void OAuth2LoginVerifier::VerifyRefreshToken(const std::string& refresh_token) {
55 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
56 if (CrosLibrary::Get()->libcros_loaded()) {
57 // Delay the verification if the network is not connected or on a captive
58 // portal.
59 const Network* network =
60 CrosLibrary::Get()->GetNetworkLibrary()->active_network();
61 if (!network || !network->connected() || network->restricted_pool()) {
62 // If network is offline, defer the token fetching until online.
63 VLOG(1) << "Network is offline. Deferring OAuth2 access token fetch.";
64 BrowserThread::PostDelayedTask(
65 BrowserThread::UI, FROM_HERE,
66 base::Bind(&OAuth2LoginVerifier::VerifyRefreshToken,
67 AsWeakPtr(),
68 refresh_token),
69 base::TimeDelta::FromMilliseconds(kRequestRestartDelay));
70 return;
71 }
72 }
73
74 access_token_.clear();
75 refresh_token_ = refresh_token;
76 std::vector<std::string> scopes;
77 scopes.push_back(kOAuthLoginServiceScope);
78 token_fetcher_.Start(GaiaUrls::GetInstance()->oauth2_chrome_client_id(),
79 GaiaUrls::GetInstance()->oauth2_chrome_client_secret(),
80 refresh_token,
81 scopes);
82 }
83
84 void OAuth2LoginVerifier::OnGetTokenSuccess(
85 const std::string& access_token, const base::Time& expiration_time) {
86 VLOG(1) << "Got OAuth2 access token!";
87 retry_count_ = 0;
88 access_token_ = access_token;
89 StartOAuthLogin();
90 }
91
92 void OAuth2LoginVerifier::StartOAuthLogin() {
93 gaia_fetcher_.StartOAuthLogin(access_token_,
94 GaiaConstants::kSyncService);
95 }
96
97 void OAuth2LoginVerifier::OnGetTokenFailure(
98 const GoogleServiceAuthError& error) {
99 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
100 LOG(WARNING) << "Failed to hget OAuth2 access token, "
101 << " error: " << error.state();
102 RetryOnError("GetToken", error,
103 base::Bind(&OAuth2LoginVerifier::VerifyRefreshToken,
104 AsWeakPtr(),
105 refresh_token_));
106 }
107
108 void OAuth2LoginVerifier::OnClientLoginSuccess(
109 const ClientLoginResult& result) {
110 LOG(WARNING) << "OAuthLogin successful!";
111 sid_ = result.sid;
112 lsid_ = result.lsid;
113 token_ = result.token;
114 retry_count_ = 0;
115 StartCookiesRetrieval();
116 }
117
118 void OAuth2LoginVerifier::OnClientLoginFailure(
119 const GoogleServiceAuthError& error) {
120 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
121 LOG(WARNING) << "Failed to log in with OAuth1 access tokens,"
122 << " error: " << error.state();
123 RetryOnError("OAuthLogin", error,
124 base::Bind(&OAuth2LoginVerifier::StartOAuthLogin,
125 AsWeakPtr()));
126 }
127
128 void OAuth2LoginVerifier::StartCookiesRetrieval() {
129 DCHECK(!sid_.empty());
130 DCHECK(!lsid_.empty());
131 gaia_fetcher_.StartIssueAuthToken(sid_, lsid_, GaiaConstants::kGaiaService);
132 }
133
134 void OAuth2LoginVerifier::OnIssueAuthTokenSuccess(
135 const std::string& service,
136 const std::string& token_token) {
137 LOG(WARNING) << "Got auth token!";
138 retry_count_ = 0;
139 gaia_fetcher_.StartMergeSession(token_token);
140 }
141
142 void OAuth2LoginVerifier::OnIssueAuthTokenFailure(
143 const std::string& service,
144 const GoogleServiceAuthError& error) {
145 LOG(WARNING) << "Failed IssueAuthToken request,"
146 << " error:" << error.state();
147 RetryOnError("IssueAuthToken", error,
148 base::Bind(&OAuth2LoginVerifier::StartCookiesRetrieval,
149 AsWeakPtr()));
150 }
151
152 void OAuth2LoginVerifier::OnMergeSessionSuccess(const std::string& data) {
153 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
154 LOG(WARNING) << "MergeSession successful.";
155 delegate_->OnOAuth2LoginVerifierSuccess(sid_, lsid_, token_);
156 }
157
158 void OAuth2LoginVerifier::OnMergeSessionFailure(
159 const GoogleServiceAuthError& error) {
160 LOG(WARNING) << "Failed MergeSession request,"
161 << " error: " << error.state();
162 RetryOnError("MergeSession", error,
163 base::Bind(&OAuth2LoginVerifier::StartCookiesRetrieval,
164 AsWeakPtr()));
165 }
166
167 void OAuth2LoginVerifier::RetryOnError(const char* operation_id,
168 const GoogleServiceAuthError& error,
169 const base::Closure& task) {
170 if ((error.state() == GoogleServiceAuthError::CONNECTION_FAILED ||
171 error.state() == GoogleServiceAuthError::SERVICE_UNAVAILABLE ||
172 error.state() == GoogleServiceAuthError::REQUEST_CANCELED) &&
173 retry_count_++ < kMaxRequestAttemptCount) {
xiyuan 2013/01/07 23:07:55 Get rid of '++' here since we are doing it in the
zel 2013/01/08 02:05:41 Done.
174 retry_count_++;
175 UMA_HISTOGRAM_ENUMERATION(
176 base::StringPrintf("OAuth2LoginVerifier.%sWithRetry", operation_id),
177 error.state(),
178 GoogleServiceAuthError::NUM_STATES);
179 BrowserThread::PostDelayedTask(
180 BrowserThread::UI, FROM_HERE, task,
181 base::TimeDelta::FromMilliseconds(kRequestRestartDelay));
182 } else {
183 LOG(WARNING) << "Unrecoverable error or retry count max reached for "
184 << operation_id;
185 UMA_HISTOGRAM_ENUMERATION(
186 base::StringPrintf("OAuth2LoginVerifier.%sWithNoRetry", operation_id),
187 error.state(),
188 GoogleServiceAuthError::NUM_STATES);
189 delegate_->OnOAuth2LoginVerifierFaulure();
190 }
191 }
192
193 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698