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

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 // Keeping VLOGs for Cloud Print proxy logging. It is convinient for finding
42 // issues with GCP in the field, where only release version is avaialble.
43 VLOG(1) << "CP_AUTH: Authenticating with LSID";
44 scoped_refptr<ServiceGaiaAuthenticator> gaia_auth_for_print(
45 new ServiceGaiaAuthenticator(
46 kProxyAuthUserAgent, kCloudPrintGaiaServiceId,
47 GaiaUrls::GetInstance()->client_login_url(),
48 g_service_process->io_thread()->message_loop_proxy()));
49 gaia_auth_for_print->set_message_loop(MessageLoop::current());
50 if (gaia_auth_for_print->AuthenticateWithLsid(lsid)) {
51 // Stash away the user email so we can save it in prefs.
52 user_email_ = gaia_auth_for_print->email();
53 // If the same user is re-enabling Cloud Print and we have stashed robot
54 // credentials, we will use those.
55 if ((0 == base::strcasecmp(user_email_.c_str(), last_user_email.c_str())) &&
56 !last_robot_refresh_token.empty() &&
57 !last_robot_email.empty()) {
58 AuthenticateWithRobotToken(last_robot_refresh_token,
59 last_robot_email);
60 }
61 AuthenticateWithToken(gaia_auth_for_print->auth_token());
62 } else {
63 // Notify client about authentication error.
64 client_->OnInvalidCredentials();
65 }
66 }
67
68 void CloudPrintAuth::AuthenticateWithToken(
69 const std::string cloud_print_token) {
70 VLOG(1) << "CP_AUTH: Authenticating with token";
71
72 client_login_token_ = cloud_print_token;
73
74 // We need to get the credentials of the robot here.
75 GURL get_authcode_url =
76 CloudPrintHelpers::GetUrlForGetAuthCode(cloud_print_server_url_,
77 oauth_client_info_.client_id,
78 proxy_id_);
79 request_ = new CloudPrintURLFetcher;
80 request_->StartGetRequest(get_authcode_url,
81 this,
82 kCloudPrintAuthMaxRetryCount,
83 std::string());
84 }
85
86 void CloudPrintAuth::AuthenticateWithRobotToken(
87 const std::string& robot_oauth_refresh_token,
88 const std::string& robot_email) {
89 VLOG(1) << "CP_AUTH: Authenticating with robot token";
90
91 robot_email_ = robot_email;
92 refresh_token_ = robot_oauth_refresh_token;
93 RefreshAccessToken();
94 }
95
96 void CloudPrintAuth::AuthenticateWithRobotAuthCode(
97 const std::string& robot_oauth_auth_code,
98 const std::string& robot_email) {
99 VLOG(1) << "CP_AUTH: Authenticating with robot auth code";
100
101 robot_email_ = robot_email;
102 // Now that we have an auth code we need to get the refresh and access tokens.
103 oauth_client_.reset(new gaia::GaiaOAuthClient(
104 gaia::kGaiaOAuth2Url,
105 g_service_process->GetServiceURLRequestContextGetter()));
106 oauth_client_->GetTokensFromAuthCode(oauth_client_info_,
107 robot_oauth_auth_code,
108 kCloudPrintAuthMaxRetryCount,
109 this);
110 }
111
112 void CloudPrintAuth::RefreshAccessToken() {
113 oauth_client_.reset(new gaia::GaiaOAuthClient(
114 gaia::kGaiaOAuth2Url,
115 g_service_process->GetServiceURLRequestContextGetter()));
116 oauth_client_->RefreshToken(oauth_client_info_,
117 refresh_token_,
118 kCloudPrintAuthMaxRetryCount,
119 this);
120 }
121
122 void CloudPrintAuth::OnGetTokensResponse(const std::string& refresh_token,
123 const std::string& access_token,
124 int expires_in_seconds) {
125 refresh_token_ = refresh_token;
126 // After saving the refresh token, this is just like having just refreshed
127 // the access token. Just call OnRefreshTokenResponse.
128 OnRefreshTokenResponse(access_token, expires_in_seconds);
129 }
130
131 void CloudPrintAuth::OnRefreshTokenResponse(const std::string& access_token,
132 int expires_in_seconds) {
133 client_->OnAuthenticationComplete(access_token, refresh_token_,
134 robot_email_, user_email_);
135
136 // Schedule a task to refresh the access token again when it is about to
137 // expire.
138 DCHECK(expires_in_seconds > kTokenRefreshGracePeriodSecs);
139 int64 refresh_delay =
140 (expires_in_seconds - kTokenRefreshGracePeriodSecs)*1000;
141 MessageLoop::current()->PostDelayedTask(
142 FROM_HERE,
143 NewRunnableMethod(this, &CloudPrintAuth::RefreshAccessToken),
144 refresh_delay);
145 }
146
147 void CloudPrintAuth::OnOAuthError() {
148 // Notify client about authentication error.
149 client_->OnInvalidCredentials();
150 }
151
152 void CloudPrintAuth::OnNetworkError(int response_code) {
153 // Since we specify infinite retries on network errors, this should never
154 // be called.
155 NOTREACHED() <<
156 "OnNetworkError invoked when not expected, response code is " <<
157 response_code;
158 }
159
160 CloudPrintURLFetcher::ResponseAction CloudPrintAuth::HandleJSONData(
161 const content::URLFetcher* source,
162 const GURL& url,
163 base::DictionaryValue* json_data,
164 bool succeeded) {
165 if (!succeeded) {
166 VLOG(1) << "CP_AUTH: Creating robot account failed";
167 client_->OnInvalidCredentials();
168 return CloudPrintURLFetcher::STOP_PROCESSING;
169 }
170
171 std::string auth_code;
172 if (!json_data->GetString(kOAuthCodeValue, &auth_code)) {
173 VLOG(1) << "CP_AUTH: Creating robot account returned invalid json response";
174 client_->OnInvalidCredentials();
175 return CloudPrintURLFetcher::STOP_PROCESSING;
176 }
177
178 json_data->GetString(kXMPPJidValue, &robot_email_);
179 // Now that we have an auth code we need to get the refresh and access tokens.
180 oauth_client_.reset(new gaia::GaiaOAuthClient(
181 gaia::kGaiaOAuth2Url,
182 g_service_process->GetServiceURLRequestContextGetter()));
183 oauth_client_->GetTokensFromAuthCode(oauth_client_info_,
184 auth_code,
185 kCloudPrintAPIMaxRetryCount,
186 this);
187
188 return CloudPrintURLFetcher::STOP_PROCESSING;
189 }
190
191 CloudPrintURLFetcher::ResponseAction CloudPrintAuth::OnRequestAuthError() {
192 VLOG(1) << "CP_AUTH: Creating robot account authentication error";
193 // Notify client about authentication error.
194 client_->OnInvalidCredentials();
195 return CloudPrintURLFetcher::STOP_PROCESSING;
196 }
197
198 std::string CloudPrintAuth::GetAuthHeader() {
199 DCHECK(!client_login_token_.empty());
200 std::string header;
201 header = "Authorization: GoogleLogin auth=";
202 header += client_login_token_;
203 return header;
204 }
205
OLDNEW
« no previous file with comments | « chrome/service/cloud_print/cloud_print_auth.h ('k') | chrome/service/cloud_print/cloud_print_connector.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698