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

Side by Side Diff: google_apis/gaia/gaia_oauth_client.cc

Issue 2796293003: Network traffic annotation added to google_apis/gaia. (Closed)
Patch Set: Annotations updated. Created 3 years, 7 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
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 "google_apis/gaia/gaia_oauth_client.h" 5 #include "google_apis/gaia/gaia_oauth_client.h"
6 6
7 #include <memory> 7 #include <memory>
8 #include <utility> 8 #include <utility>
9 9
10 #include "base/json/json_reader.h" 10 #include "base/json/json_reader.h"
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "base/strings/string_util.h" 12 #include "base/strings/string_util.h"
13 #include "base/values.h" 13 #include "base/values.h"
14 #include "google_apis/gaia/gaia_auth_util.h" 14 #include "google_apis/gaia/gaia_auth_util.h"
15 #include "google_apis/gaia/gaia_urls.h" 15 #include "google_apis/gaia/gaia_urls.h"
16 #include "net/base/escape.h" 16 #include "net/base/escape.h"
17 #include "net/base/load_flags.h" 17 #include "net/base/load_flags.h"
18 #include "net/http/http_status_code.h" 18 #include "net/http/http_status_code.h"
19 #include "net/traffic_annotation/network_traffic_annotation.h"
19 #include "net/url_request/url_fetcher.h" 20 #include "net/url_request/url_fetcher.h"
20 #include "net/url_request/url_fetcher_delegate.h" 21 #include "net/url_request/url_fetcher_delegate.h"
21 #include "net/url_request/url_request_context_getter.h" 22 #include "net/url_request/url_request_context_getter.h"
22 #include "url/gurl.h" 23 #include "url/gurl.h"
23 24
24 namespace { 25 namespace {
25 const char kAccessTokenValue[] = "access_token"; 26 const char kAccessTokenValue[] = "access_token";
26 const char kRefreshTokenValue[] = "refresh_token"; 27 const char kRefreshTokenValue[] = "refresh_token";
27 const char kExpiresInValue[] = "expires_in"; 28 const char kExpiresInValue[] = "expires_in";
28 } 29 }
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
85 86
86 ~Core() override {} 87 ~Core() override {}
87 88
88 void GetUserInfoImpl(RequestType type, 89 void GetUserInfoImpl(RequestType type,
89 const std::string& oauth_access_token, 90 const std::string& oauth_access_token,
90 int max_retries, 91 int max_retries,
91 Delegate* delegate); 92 Delegate* delegate);
92 void MakeGaiaRequest(const GURL& url, 93 void MakeGaiaRequest(const GURL& url,
93 const std::string& post_body, 94 const std::string& post_body,
94 int max_retries, 95 int max_retries,
95 GaiaOAuthClient::Delegate* delegate); 96 GaiaOAuthClient::Delegate* delegate,
97 net::NetworkTrafficAnnotationTag& traffic_annotation);
96 void HandleResponse(const net::URLFetcher* source, 98 void HandleResponse(const net::URLFetcher* source,
97 bool* should_retry_request); 99 bool* should_retry_request);
98 100
99 int num_retries_; 101 int num_retries_;
100 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; 102 scoped_refptr<net::URLRequestContextGetter> request_context_getter_;
101 GaiaOAuthClient::Delegate* delegate_; 103 GaiaOAuthClient::Delegate* delegate_;
102 std::unique_ptr<net::URLFetcher> request_; 104 std::unique_ptr<net::URLFetcher> request_;
103 RequestType request_type_; 105 RequestType request_type_;
104 }; 106 };
105 107
106 void GaiaOAuthClient::Core::GetTokensFromAuthCode( 108 void GaiaOAuthClient::Core::GetTokensFromAuthCode(
107 const OAuthClientInfo& oauth_client_info, 109 const OAuthClientInfo& oauth_client_info,
108 const std::string& auth_code, 110 const std::string& auth_code,
109 int max_retries, 111 int max_retries,
110 GaiaOAuthClient::Delegate* delegate) { 112 GaiaOAuthClient::Delegate* delegate) {
111 DCHECK_EQ(request_type_, NO_PENDING_REQUEST); 113 DCHECK_EQ(request_type_, NO_PENDING_REQUEST);
112 request_type_ = TOKENS_FROM_AUTH_CODE; 114 request_type_ = TOKENS_FROM_AUTH_CODE;
113 std::string post_body = 115 std::string post_body =
114 "code=" + net::EscapeUrlEncodedData(auth_code, true) + 116 "code=" + net::EscapeUrlEncodedData(auth_code, true) +
115 "&client_id=" + net::EscapeUrlEncodedData(oauth_client_info.client_id, 117 "&client_id=" + net::EscapeUrlEncodedData(oauth_client_info.client_id,
116 true) + 118 true) +
117 "&client_secret=" + 119 "&client_secret=" +
118 net::EscapeUrlEncodedData(oauth_client_info.client_secret, true) + 120 net::EscapeUrlEncodedData(oauth_client_info.client_secret, true) +
119 "&redirect_uri=" + 121 "&redirect_uri=" +
120 net::EscapeUrlEncodedData(oauth_client_info.redirect_uri, true) + 122 net::EscapeUrlEncodedData(oauth_client_info.redirect_uri, true) +
121 "&grant_type=authorization_code"; 123 "&grant_type=authorization_code";
122 MakeGaiaRequest(GURL(GaiaUrls::GetInstance()->oauth2_token_url()), 124 net::NetworkTrafficAnnotationTag traffic_annotation =
123 post_body, max_retries, delegate); 125 net::DefineNetworkTrafficAnnotation("gaia_oauth_client_get_tokens", R"(
126 semantics {
127 sender: "OAuth 2.0 calls"
128 description:
129 "This request exchanges an authorization code for an OAuth 2.0 "
130 "refresh token and an OAuth 2.0 access token."
131 trigger:
132 "This request is triggered when a Chrome service requires an "
133 "access token and a refresh token (e.g. Cloud Print, Chrome Remote "
134 "Desktop etc.) See https://developers.google.com/identity/protocols"
135 "/OAuth2 for more information about the Google implementation of "
136 "the OAuth 2.0 protocol."
137 data:
138 "The Google console client ID and client secret of the caller, the "
139 "OAuth authorization code and the redirect URI."
140 destination: GOOGLE_OWNED_SERVICE
141 }
142 policy {
143 cookies_allowed: false
144 setting:
145 "This feature cannot be disabled in settings, but if user signs "
146 "out of Chrome, this request would not be made."
147 chrome_policy {
148 SigninAllowed {
149 policy_options {mode: MANDATORY}
150 SigninAllowed: false
151 }
152 }
153 })");
154 MakeGaiaRequest(GURL(GaiaUrls::GetInstance()->oauth2_token_url()), post_body,
155 max_retries, delegate, traffic_annotation);
124 } 156 }
125 157
126 void GaiaOAuthClient::Core::RefreshToken( 158 void GaiaOAuthClient::Core::RefreshToken(
127 const OAuthClientInfo& oauth_client_info, 159 const OAuthClientInfo& oauth_client_info,
128 const std::string& refresh_token, 160 const std::string& refresh_token,
129 const std::vector<std::string>& scopes, 161 const std::vector<std::string>& scopes,
130 int max_retries, 162 int max_retries,
131 GaiaOAuthClient::Delegate* delegate) { 163 GaiaOAuthClient::Delegate* delegate) {
132 DCHECK_EQ(request_type_, NO_PENDING_REQUEST); 164 DCHECK_EQ(request_type_, NO_PENDING_REQUEST);
133 request_type_ = REFRESH_TOKEN; 165 request_type_ = REFRESH_TOKEN;
134 std::string post_body = 166 std::string post_body =
135 "refresh_token=" + net::EscapeUrlEncodedData(refresh_token, true) + 167 "refresh_token=" + net::EscapeUrlEncodedData(refresh_token, true) +
136 "&client_id=" + net::EscapeUrlEncodedData(oauth_client_info.client_id, 168 "&client_id=" + net::EscapeUrlEncodedData(oauth_client_info.client_id,
137 true) + 169 true) +
138 "&client_secret=" + 170 "&client_secret=" +
139 net::EscapeUrlEncodedData(oauth_client_info.client_secret, true) + 171 net::EscapeUrlEncodedData(oauth_client_info.client_secret, true) +
140 "&grant_type=refresh_token"; 172 "&grant_type=refresh_token";
141 173
142 if (!scopes.empty()) { 174 if (!scopes.empty()) {
143 std::string scopes_string = base::JoinString(scopes, " "); 175 std::string scopes_string = base::JoinString(scopes, " ");
144 post_body += "&scope=" + net::EscapeUrlEncodedData(scopes_string, true); 176 post_body += "&scope=" + net::EscapeUrlEncodedData(scopes_string, true);
145 } 177 }
146 178
147 MakeGaiaRequest(GURL(GaiaUrls::GetInstance()->oauth2_token_url()), 179 net::NetworkTrafficAnnotationTag traffic_annotation =
148 post_body, max_retries, delegate); 180 net::DefineNetworkTrafficAnnotation("gaia_oauth_client_refresh_token", R"(
181 semantics {
182 sender: "OAuth 2.0 calls"
183 description:
184 "This request fetches a fresh access token that can be used to "
185 "authenticate an API call to a Google web endpoint."
186 trigger:
187 "This is called whenever the caller needs a fresh OAuth 2.0 access "
188 "token."
189 data:
190 "The OAuth 2.0 refresh token, the Google console client ID and "
191 "client secret of the caller, and optionally the scopes of the API "
192 "for which the access token should be authorized."
193 destination: GOOGLE_OWNED_SERVICE
194 }
195 policy {
196 cookies_allowed: false
197 setting:
198 "This feature cannot be disabled in settings, but if user signs "
199 "out of Chrome, this request would not be made."
200 chrome_policy {
201 SigninAllowed {
202 policy_options {mode: MANDATORY}
203 SigninAllowed: false
204 }
205 }
206 })");
207 MakeGaiaRequest(GURL(GaiaUrls::GetInstance()->oauth2_token_url()), post_body,
208 max_retries, delegate, traffic_annotation);
149 } 209 }
150 210
151 void GaiaOAuthClient::Core::GetUserEmail(const std::string& oauth_access_token, 211 void GaiaOAuthClient::Core::GetUserEmail(const std::string& oauth_access_token,
152 int max_retries, 212 int max_retries,
153 Delegate* delegate) { 213 Delegate* delegate) {
154 GetUserInfoImpl(USER_EMAIL, oauth_access_token, max_retries, delegate); 214 GetUserInfoImpl(USER_EMAIL, oauth_access_token, max_retries, delegate);
155 } 215 }
156 216
157 void GaiaOAuthClient::Core::GetUserId(const std::string& oauth_access_token, 217 void GaiaOAuthClient::Core::GetUserId(const std::string& oauth_access_token,
158 int max_retries, 218 int max_retries,
(...skipping 10 matching lines...) Expand all
169 void GaiaOAuthClient::Core::GetUserInfoImpl( 229 void GaiaOAuthClient::Core::GetUserInfoImpl(
170 RequestType type, 230 RequestType type,
171 const std::string& oauth_access_token, 231 const std::string& oauth_access_token,
172 int max_retries, 232 int max_retries,
173 Delegate* delegate) { 233 Delegate* delegate) {
174 DCHECK_EQ(request_type_, NO_PENDING_REQUEST); 234 DCHECK_EQ(request_type_, NO_PENDING_REQUEST);
175 DCHECK(!request_.get()); 235 DCHECK(!request_.get());
176 request_type_ = type; 236 request_type_ = type;
177 delegate_ = delegate; 237 delegate_ = delegate;
178 num_retries_ = 0; 238 num_retries_ = 0;
239 net::NetworkTrafficAnnotationTag traffic_annotation =
240 net::DefineNetworkTrafficAnnotation("gaia_core_get_user_info", R"(
241 semantics {
242 sender: "OAuth 2.0 calls"
243 description: "This request is used to fetch user information."
244 trigger:
245 "The main trigger for this request in the AccountTrackerService "
246 "that fetches the user info soon after the user signs in."
247 data:
248 "The OAuth 2.0 access token of the account."
249 destination: GOOGLE_OWNED_SERVICE
250 }
251 policy {
252 cookies_allowed: false
253 setting:
254 "This feature cannot be disabled in settings, but if user signs "
255 "out of Chrome, this request would not be made."
256 chrome_policy {
257 SigninAllowed {
258 policy_options {mode: MANDATORY}
259 SigninAllowed: false
260 }
261 }
262 })");
179 request_ = net::URLFetcher::Create( 263 request_ = net::URLFetcher::Create(
180 kUrlFetcherId, GURL(GaiaUrls::GetInstance()->oauth_user_info_url()), 264 kUrlFetcherId, GURL(GaiaUrls::GetInstance()->oauth_user_info_url()),
181 net::URLFetcher::GET, this); 265 net::URLFetcher::GET, this, traffic_annotation);
182 request_->SetRequestContext(request_context_getter_.get()); 266 request_->SetRequestContext(request_context_getter_.get());
183 request_->AddExtraRequestHeader("Authorization: OAuth " + oauth_access_token); 267 request_->AddExtraRequestHeader("Authorization: OAuth " + oauth_access_token);
184 request_->SetMaxRetriesOn5xx(max_retries); 268 request_->SetMaxRetriesOn5xx(max_retries);
185 request_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | 269 request_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES |
186 net::LOAD_DO_NOT_SAVE_COOKIES); 270 net::LOAD_DO_NOT_SAVE_COOKIES);
187 MarkURLFetcherAsGaia(request_.get()); 271 MarkURLFetcherAsGaia(request_.get());
188 272
189 // Fetchers are sometimes cancelled because a network change was detected, 273 // Fetchers are sometimes cancelled because a network change was detected,
190 // especially at startup and after sign-in on ChromeOS. Retrying once should 274 // especially at startup and after sign-in on ChromeOS. Retrying once should
191 // be enough in those cases; let the fetcher retry up to 3 times just in case. 275 // be enough in those cases; let the fetcher retry up to 3 times just in case.
192 // http://crbug.com/163710 276 // http://crbug.com/163710
193 request_->SetAutomaticallyRetryOnNetworkChanges(3); 277 request_->SetAutomaticallyRetryOnNetworkChanges(3);
194 request_->Start(); 278 request_->Start();
195 } 279 }
196 280
197 void GaiaOAuthClient::Core::GetTokenInfo(const std::string& qualifier, 281 void GaiaOAuthClient::Core::GetTokenInfo(const std::string& qualifier,
198 const std::string& query, 282 const std::string& query,
199 int max_retries, 283 int max_retries,
200 Delegate* delegate) { 284 Delegate* delegate) {
201 DCHECK_EQ(request_type_, NO_PENDING_REQUEST); 285 DCHECK_EQ(request_type_, NO_PENDING_REQUEST);
202 DCHECK(!request_.get()); 286 DCHECK(!request_.get());
203 request_type_ = TOKEN_INFO; 287 request_type_ = TOKEN_INFO;
204 std::string post_body = 288 std::string post_body =
205 qualifier + "=" + net::EscapeUrlEncodedData(query, true); 289 qualifier + "=" + net::EscapeUrlEncodedData(query, true);
290 net::NetworkTrafficAnnotationTag traffic_annotation =
291 net::DefineNetworkTrafficAnnotation("...", R"(
292 semantics {
293 sender: "OAuth 2.0 calls"
294 description:
295 "This request fetches information about an OAuth 2.0 access token. "
296 "The response is a dictionary of response values. The provided "
297 "access token may have any scope, and basic results will be "
298 "returned: issued_to, audience, scope, expires_in, access_type. In "
299 "addition, if the https://www.googleapis.com/auth/userinfo.email "
300 "scope is present, the email and verified_email fields will be "
301 "returned. If the https://www.googleapis.com/auth/userinfo.profile "
302 "scope is present, the user_id field will be returned."
303 trigger:
304 "This is triggered after a Google account is added to the browser. "
305 "It it also triggered after each successful fetch of an OAuth 2.0 "
306 "access token."
307 data: "The OAuth 2.0 access token."
308 destination: GOOGLE_OWNED_SERVICE
309 }
310 policy {
311 cookies_allowed: false
312 setting:
313 "This feature cannot be disabled in settings, but if user signs "
314 "out of Chrome, this request would not be made."
315 chrome_policy {
316 SigninAllowed {
317 policy_options {mode: MANDATORY}
318 SigninAllowed: false
319 }
320 }
321 })");
206 MakeGaiaRequest(GURL(GaiaUrls::GetInstance()->oauth2_token_info_url()), 322 MakeGaiaRequest(GURL(GaiaUrls::GetInstance()->oauth2_token_info_url()),
207 post_body, 323 post_body, max_retries, delegate, traffic_annotation);
208 max_retries,
209 delegate);
210 } 324 }
211 325
212 void GaiaOAuthClient::Core::MakeGaiaRequest( 326 void GaiaOAuthClient::Core::MakeGaiaRequest(
213 const GURL& url, 327 const GURL& url,
214 const std::string& post_body, 328 const std::string& post_body,
215 int max_retries, 329 int max_retries,
216 GaiaOAuthClient::Delegate* delegate) { 330 GaiaOAuthClient::Delegate* delegate,
331 const net::NetworkTrafficAnnotationTag& traffic_annotation) {
217 DCHECK(!request_.get()) << "Tried to fetch two things at once!"; 332 DCHECK(!request_.get()) << "Tried to fetch two things at once!";
218 delegate_ = delegate; 333 delegate_ = delegate;
219 num_retries_ = 0; 334 num_retries_ = 0;
220 request_ = 335 request_ = net::URLFetcher::Create(kUrlFetcherId, url, net::URLFetcher::POST,
221 net::URLFetcher::Create(kUrlFetcherId, url, net::URLFetcher::POST, this); 336 this, traffic_annotation);
222 request_->SetRequestContext(request_context_getter_.get()); 337 request_->SetRequestContext(request_context_getter_.get());
223 request_->SetUploadData("application/x-www-form-urlencoded", post_body); 338 request_->SetUploadData("application/x-www-form-urlencoded", post_body);
224 request_->SetMaxRetriesOn5xx(max_retries); 339 request_->SetMaxRetriesOn5xx(max_retries);
225 request_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | 340 request_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES |
226 net::LOAD_DO_NOT_SAVE_COOKIES); 341 net::LOAD_DO_NOT_SAVE_COOKIES);
227 MarkURLFetcherAsGaia(request_.get()); 342 MarkURLFetcherAsGaia(request_.get());
228 // See comment on SetAutomaticallyRetryOnNetworkChanges() above. 343 // See comment on SetAutomaticallyRetryOnNetworkChanges() above.
229 request_->SetAutomaticallyRetryOnNetworkChanges(3); 344 request_->SetAutomaticallyRetryOnNetworkChanges(3);
230 request_->Start(); 345 request_->Start();
231 } 346 }
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after
409 } 524 }
410 525
411 void GaiaOAuthClient::GetTokenHandleInfo(const std::string& token_handle, 526 void GaiaOAuthClient::GetTokenHandleInfo(const std::string& token_handle,
412 int max_retries, 527 int max_retries,
413 Delegate* delegate) { 528 Delegate* delegate) {
414 return core_->GetTokenInfo("token_handle", token_handle, max_retries, 529 return core_->GetTokenInfo("token_handle", token_handle, max_retries,
415 delegate); 530 delegate);
416 } 531 }
417 532
418 } // namespace gaia 533 } // namespace gaia
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698