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