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

Side by Side Diff: chrome/service/cloud_print/cloud_print_auth.cc

Issue 8387011: Chrome proxy refactoring. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 9 years, 1 month 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) 2011 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/service/cloud_print/cloud_print_auth.h"
6
7 #include "base/string_util.h"
8 #include "chrome/common/net/gaia/gaia_urls.h"
9 #include "chrome/service/cloud_print/cloud_print_consts.h"
10 #include "chrome/service/cloud_print/cloud_print_helpers.h"
11 #include "chrome/service/cloud_print/cloud_print_token_store.h"
12 #include "chrome/service/gaia/service_gaia_authenticator.h"
13 #include "chrome/service/net/service_url_request_context.h"
14 #include "chrome/service/service_process.h"
15
16 CloudPrintAuth::CloudPrintAuth(
17 Client* client,
18 const GURL& cloud_print_server_url,
19 const base::DictionaryValue* print_sys_settings,
20 const gaia::OAuthClientInfo& oauth_client_info,
21 const std::string& proxy_id)
22 : client_(client),
23 oauth_client_info_(oauth_client_info),
24 cloud_print_server_url_(cloud_print_server_url),
25 proxy_id_(proxy_id) {
26 DCHECK(client);
27 if (print_sys_settings) {
28 // It is possible to have no print settings specified.
29 print_system_settings_.reset(print_sys_settings->DeepCopy());
30 }
31 }
32
33 CloudPrintAuth::~CloudPrintAuth() {
34 }
35
36 void CloudPrintAuth::AuthenticateWithLsid(
37 const std::string& lsid,
38 const std::string& last_robot_refresh_token,
39 const std::string& last_robot_email,
40 const std::string& last_user_email) {
41 VLOG(1) << "CP_AUTH: Authenticating with LSID";
Albert Bodenhamer 2011/10/26 23:58:09 Add comment explaining why VLOG is needed as we di
42 scoped_refptr<ServiceGaiaAuthenticator> gaia_auth_for_print(
43 new ServiceGaiaAuthenticator(
44 kProxyAuthUserAgent, kCloudPrintGaiaServiceId,
45 GaiaUrls::GetInstance()->client_login_url(),
46 g_service_process->io_thread()->message_loop_proxy()));
47 gaia_auth_for_print->set_message_loop(MessageLoop::current());
48 if (gaia_auth_for_print->AuthenticateWithLsid(lsid)) {
49 // Stash away the user email so we can save it in prefs.
50 user_email_ = gaia_auth_for_print->email();
51 // If the same user is re-enabling Cloud Print and we have stashed robot
52 // credentials, we will use those.
53 if ((0 == base::strcasecmp(user_email_.c_str(), last_user_email.c_str())) &&
54 !last_robot_refresh_token.empty() &&
55 !last_robot_email.empty()) {
56 AuthenticateWithRobotToken(last_robot_refresh_token,
57 last_robot_email);
58 }
59 AuthenticateWithToken(gaia_auth_for_print->auth_token());
60 } else {
61 // Notify client about authentication error.
62 client_->OnInvalidCredentials();
63 }
64 }
65
66 void CloudPrintAuth::AuthenticateWithToken(
67 const std::string cloud_print_token) {
68 VLOG(1) << "CP_AUTH: Authenticating with token";
69
70 client_login_token_ = cloud_print_token;
71
72 // We need to get the credentials of the robot here.
73 GURL get_authcode_url =
74 CloudPrintHelpers::GetUrlForGetAuthCode(cloud_print_server_url_,
75 oauth_client_info_.client_id,
76 proxy_id_);
77 request_ = new CloudPrintURLFetcher;
78 request_->StartGetRequest(get_authcode_url,
79 this,
80 kCloudPrintAuthMaxRetryCount,
81 std::string());
82 }
83
84 void CloudPrintAuth::AuthenticateWithRobotToken(
85 const std::string& robot_oauth_refresh_token,
86 const std::string& robot_email) {
87 VLOG(1) << "CP_AUTH: Authenticating with robot token";
88
89 robot_email_ = robot_email;
90 refresh_token_ = robot_oauth_refresh_token;
91 RefreshAccessToken();
92 }
93
94 void CloudPrintAuth::AuthenticateWithRobotAuthCode(
95 const std::string& robot_oauth_auth_code,
96 const std::string& robot_email) {
97 VLOG(1) << "CP_AUTH: Authenticating with robot auth code";
98
99 robot_email_ = robot_email;
100 // Now that we have an auth code we need to get the refresh and access tokens.
101 oauth_client_.reset(new gaia::GaiaOAuthClient(
102 gaia::kGaiaOAuth2Url,
103 g_service_process->GetServiceURLRequestContextGetter()));
104 oauth_client_->GetTokensFromAuthCode(oauth_client_info_,
105 robot_oauth_auth_code,
106 kCloudPrintAuthMaxRetryCount,
107 this);
108 }
109
110 void CloudPrintAuth::RefreshAccessToken() {
111 oauth_client_.reset(new gaia::GaiaOAuthClient(
112 gaia::kGaiaOAuth2Url,
113 g_service_process->GetServiceURLRequestContextGetter()));
114 oauth_client_->RefreshToken(oauth_client_info_,
115 refresh_token_,
116 kCloudPrintAuthMaxRetryCount,
117 this);
118 }
119
120 void CloudPrintAuth::OnGetTokensResponse(const std::string& refresh_token,
121 const std::string& access_token,
122 int expires_in_seconds) {
123 refresh_token_ = refresh_token;
124 // After saving the refresh token, this is just like having just refreshed
125 // the access token. Just call OnRefreshTokenResponse.
126 OnRefreshTokenResponse(access_token, expires_in_seconds);
127 }
128
129 void CloudPrintAuth::OnRefreshTokenResponse(const std::string& access_token,
130 int expires_in_seconds) {
131 client_->OnAuthenticationComplete(access_token, refresh_token_,
132 robot_email_, user_email_);
133
134 // Schedule a task to refresh the access token again when it is about to
135 // expire.
136 DCHECK(expires_in_seconds > kTokenRefreshGracePeriodSecs);
137 int64 refresh_delay =
138 (expires_in_seconds - kTokenRefreshGracePeriodSecs)*1000;
139 MessageLoop::current()->PostDelayedTask(
140 FROM_HERE,
141 NewRunnableMethod(this, &CloudPrintAuth::RefreshAccessToken),
142 refresh_delay);
143 }
144
145 void CloudPrintAuth::OnOAuthError() {
146 // Notify client about authentication error.
147 client_->OnInvalidCredentials();
148 }
149
150 void CloudPrintAuth::OnNetworkError(int response_code) {
151 // Since we specify infinite retries on network errors, this should never
152 // be called.
153 NOTREACHED() <<
154 "OnNetworkError invoked when not expected, response code is " <<
155 response_code;
156 }
157
158 CloudPrintURLFetcher::ResponseAction CloudPrintAuth::HandleJSONData(
159 const content::URLFetcher* source,
160 const GURL& url,
161 base::DictionaryValue* json_data,
162 bool succeeded) {
163 if (!succeeded) {
164 VLOG(1) << "CP_AUTH: Creating robot account failed";
165 client_->OnInvalidCredentials();
166 return CloudPrintURLFetcher::STOP_PROCESSING;
167 }
168
169 std::string auth_code;
170 if (!json_data->GetString(kOAuthCodeValue, &auth_code)) {
171 VLOG(1) << "CP_AUTH: Creating robot account returned invalid json response";
172 client_->OnInvalidCredentials();
173 return CloudPrintURLFetcher::STOP_PROCESSING;
174 }
175
176 json_data->GetString(kXMPPJidValue, &robot_email_);
177 // Now that we have an auth code we need to get the refresh and access tokens.
178 oauth_client_.reset(new gaia::GaiaOAuthClient(
179 gaia::kGaiaOAuth2Url,
180 g_service_process->GetServiceURLRequestContextGetter()));
181 oauth_client_->GetTokensFromAuthCode(oauth_client_info_,
182 auth_code,
183 kCloudPrintAPIMaxRetryCount,
184 this);
185
186 return CloudPrintURLFetcher::STOP_PROCESSING;
187 }
188
189 CloudPrintURLFetcher::ResponseAction CloudPrintAuth::OnRequestAuthError() {
190 VLOG(1) << "CP_AUTH: Creating robot account authentication error";
191 // Notify client about authentication error.
192 client_->OnInvalidCredentials();
193 return CloudPrintURLFetcher::STOP_PROCESSING;
194 }
195
196 std::string CloudPrintAuth::GetAuthHeader() {
197 DCHECK(!client_login_token_.empty());
198 std::string header;
199 header = "Authorization: GoogleLogin auth=";
200 header += client_login_token_;
201 return header;
202 }
203
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698