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

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

Issue 2796293003: Network traffic annotation added to google_apis/gaia. (Closed)
Patch Set: Moved oauth2_api_call_flow.* to CL 2888053003 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("...", R"(
126 semantics {
127 sender: "..."
128 description: "..."
msarda 2017/05/22 11:49:35 This request exchanges an authorization code for a
Ramin Halavati 2017/05/22 12:42:17 Done.
129 trigger: "..."
msarda 2017/05/22 11:49:35 This request is triggered at when another service
Ramin Halavati 2017/05/22 12:42:17 Done.
130 data: "..."
msarda 2017/05/22 11:49:35 The Google console client ID and client secret of
Ramin Halavati 2017/05/22 12:42:16 Done.
131 destination: WEBSITE/GOOGLE_OWNED_SERVICE/OTHER
msarda 2017/05/22 11:49:36 GOOGLE_OWNED_SERVICE
Ramin Halavati 2017/05/22 12:42:17 Done.
132 }
133 policy {
134 cookies_allowed: false
135 setting: "..."
msarda 2017/05/22 11:49:36 This feature cannot be disabled in settings. Howe
136 chrome_policy {
msarda 2017/05/22 11:49:36 I have no idea if this is gated on any policy. It
137 [POLICY_NAME] {
138 policy_options {mode: MANDATORY/RECOMMENDED/UNSET}
139 [POLICY_NAME]: ... //(value to disable it)
140 }
141 }
142 policy_exception_justification: "..."
143 })");
144 MakeGaiaRequest(GURL(GaiaUrls::GetInstance()->oauth2_token_url()), post_body,
145 max_retries, delegate, traffic_annotation);
124 } 146 }
125 147
126 void GaiaOAuthClient::Core::RefreshToken( 148 void GaiaOAuthClient::Core::RefreshToken(
127 const OAuthClientInfo& oauth_client_info, 149 const OAuthClientInfo& oauth_client_info,
128 const std::string& refresh_token, 150 const std::string& refresh_token,
129 const std::vector<std::string>& scopes, 151 const std::vector<std::string>& scopes,
130 int max_retries, 152 int max_retries,
131 GaiaOAuthClient::Delegate* delegate) { 153 GaiaOAuthClient::Delegate* delegate) {
132 DCHECK_EQ(request_type_, NO_PENDING_REQUEST); 154 DCHECK_EQ(request_type_, NO_PENDING_REQUEST);
133 request_type_ = REFRESH_TOKEN; 155 request_type_ = REFRESH_TOKEN;
134 std::string post_body = 156 std::string post_body =
135 "refresh_token=" + net::EscapeUrlEncodedData(refresh_token, true) + 157 "refresh_token=" + net::EscapeUrlEncodedData(refresh_token, true) +
136 "&client_id=" + net::EscapeUrlEncodedData(oauth_client_info.client_id, 158 "&client_id=" + net::EscapeUrlEncodedData(oauth_client_info.client_id,
137 true) + 159 true) +
138 "&client_secret=" + 160 "&client_secret=" +
139 net::EscapeUrlEncodedData(oauth_client_info.client_secret, true) + 161 net::EscapeUrlEncodedData(oauth_client_info.client_secret, true) +
140 "&grant_type=refresh_token"; 162 "&grant_type=refresh_token";
141 163
142 if (!scopes.empty()) { 164 if (!scopes.empty()) {
143 std::string scopes_string = base::JoinString(scopes, " "); 165 std::string scopes_string = base::JoinString(scopes, " ");
144 post_body += "&scope=" + net::EscapeUrlEncodedData(scopes_string, true); 166 post_body += "&scope=" + net::EscapeUrlEncodedData(scopes_string, true);
145 } 167 }
146 168
147 MakeGaiaRequest(GURL(GaiaUrls::GetInstance()->oauth2_token_url()), 169 net::NetworkTrafficAnnotationTag traffic_annotation =
148 post_body, max_retries, delegate); 170 net::DefineNetworkTrafficAnnotation("...", R"(
171 semantics {
172 sender: "..."
msarda 2017/05/22 11:49:35 Same as above
Ramin Halavati 2017/05/22 12:42:17 Done.
173 description: "..."
msarda 2017/05/22 11:49:36 This request fetches a fresh access token that can
Ramin Halavati 2017/05/22 12:42:16 Done.
174 trigger: "..."
msarda 2017/05/22 11:49:36 This is called whenever the caller needs a fresh O
Ramin Halavati 2017/05/22 12:42:17 Done.
175 data: "..."
msarda 2017/05/22 11:49:36 The OAuth 2.0 refresh token, the Google console cl
Ramin Halavati 2017/05/22 12:42:17 Done.
176 destination: WEBSITE/GOOGLE_OWNED_SERVICE/OTHER
msarda 2017/05/22 11:49:35 GOOGLE_OWNED_SERVICE
Ramin Halavati 2017/05/22 12:42:16 Done.
177 }
178 policy {
179 cookies_allowed: false
180 setting: "..."
msarda 2017/05/22 11:49:36 Same as above.
Ramin Halavati 2017/05/22 12:42:16 Done.
181 chrome_policy {
182 [POLICY_NAME] {
183 policy_options {mode: MANDATORY/RECOMMENDED/UNSET}
184 [POLICY_NAME]: ... //(value to disable it)
185 }
186 }
187 policy_exception_justification: "..."
188 })");
189 MakeGaiaRequest(GURL(GaiaUrls::GetInstance()->oauth2_token_url()), post_body,
190 max_retries, delegate, traffic_annotation);
149 } 191 }
150 192
151 void GaiaOAuthClient::Core::GetUserEmail(const std::string& oauth_access_token, 193 void GaiaOAuthClient::Core::GetUserEmail(const std::string& oauth_access_token,
152 int max_retries, 194 int max_retries,
153 Delegate* delegate) { 195 Delegate* delegate) {
154 GetUserInfoImpl(USER_EMAIL, oauth_access_token, max_retries, delegate); 196 GetUserInfoImpl(USER_EMAIL, oauth_access_token, max_retries, delegate);
155 } 197 }
156 198
157 void GaiaOAuthClient::Core::GetUserId(const std::string& oauth_access_token, 199 void GaiaOAuthClient::Core::GetUserId(const std::string& oauth_access_token,
158 int max_retries, 200 int max_retries,
(...skipping 10 matching lines...) Expand all
169 void GaiaOAuthClient::Core::GetUserInfoImpl( 211 void GaiaOAuthClient::Core::GetUserInfoImpl(
170 RequestType type, 212 RequestType type,
171 const std::string& oauth_access_token, 213 const std::string& oauth_access_token,
172 int max_retries, 214 int max_retries,
173 Delegate* delegate) { 215 Delegate* delegate) {
174 DCHECK_EQ(request_type_, NO_PENDING_REQUEST); 216 DCHECK_EQ(request_type_, NO_PENDING_REQUEST);
175 DCHECK(!request_.get()); 217 DCHECK(!request_.get());
176 request_type_ = type; 218 request_type_ = type;
177 delegate_ = delegate; 219 delegate_ = delegate;
178 num_retries_ = 0; 220 num_retries_ = 0;
221 net::NetworkTrafficAnnotationTag traffic_annotation =
222 net::DefineNetworkTrafficAnnotation("gaia_core_get_user_info", R"(
223 semantics {
224 sender: "OAuth2 Client"
msarda 2017/05/22 11:49:35 I am a bit split about the sender. In the other ch
Ramin Halavati 2017/05/22 12:42:16 I am not sure, I used the latter, but if you have
225 description: "This request is used to fetch user information."
226 trigger:
227 "The main trigger for this request in the AccountTrackerService "
228 "that fetches the user info soon after the user signs in."
229 data:
230 "The OAUth2 access token of the account."
msarda 2017/05/22 11:49:35 s/OAUth2/OAuth 2.0
Ramin Halavati 2017/05/22 12:42:17 Done.
231 destination: GOOGLE_OWNED_SERVICE
232 }
233 policy {
234 cookies_allowed: false
235 setting: "This feature cannot be disabled in settings."
236 policy_exception_justification:
237 "Not implemented. Disabling this fetcher would break features that "
238 "require user information about of the account that is signed in ("
239 "e.g. the profile switcher UI, the settings UI etc)."
240 })");
179 request_ = net::URLFetcher::Create( 241 request_ = net::URLFetcher::Create(
180 kUrlFetcherId, GURL(GaiaUrls::GetInstance()->oauth_user_info_url()), 242 kUrlFetcherId, GURL(GaiaUrls::GetInstance()->oauth_user_info_url()),
181 net::URLFetcher::GET, this); 243 net::URLFetcher::GET, this, traffic_annotation);
182 request_->SetRequestContext(request_context_getter_.get()); 244 request_->SetRequestContext(request_context_getter_.get());
183 request_->AddExtraRequestHeader("Authorization: OAuth " + oauth_access_token); 245 request_->AddExtraRequestHeader("Authorization: OAuth " + oauth_access_token);
184 request_->SetMaxRetriesOn5xx(max_retries); 246 request_->SetMaxRetriesOn5xx(max_retries);
185 request_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | 247 request_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES |
186 net::LOAD_DO_NOT_SAVE_COOKIES); 248 net::LOAD_DO_NOT_SAVE_COOKIES);
187 MarkURLFetcherAsGaia(request_.get()); 249 MarkURLFetcherAsGaia(request_.get());
188 250
189 // Fetchers are sometimes cancelled because a network change was detected, 251 // Fetchers are sometimes cancelled because a network change was detected,
190 // especially at startup and after sign-in on ChromeOS. Retrying once should 252 // 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. 253 // be enough in those cases; let the fetcher retry up to 3 times just in case.
192 // http://crbug.com/163710 254 // http://crbug.com/163710
193 request_->SetAutomaticallyRetryOnNetworkChanges(3); 255 request_->SetAutomaticallyRetryOnNetworkChanges(3);
194 request_->Start(); 256 request_->Start();
195 } 257 }
196 258
197 void GaiaOAuthClient::Core::GetTokenInfo(const std::string& qualifier, 259 void GaiaOAuthClient::Core::GetTokenInfo(const std::string& qualifier,
198 const std::string& query, 260 const std::string& query,
199 int max_retries, 261 int max_retries,
200 Delegate* delegate) { 262 Delegate* delegate) {
201 DCHECK_EQ(request_type_, NO_PENDING_REQUEST); 263 DCHECK_EQ(request_type_, NO_PENDING_REQUEST);
202 DCHECK(!request_.get()); 264 DCHECK(!request_.get());
203 request_type_ = TOKEN_INFO; 265 request_type_ = TOKEN_INFO;
204 std::string post_body = 266 std::string post_body =
205 qualifier + "=" + net::EscapeUrlEncodedData(query, true); 267 qualifier + "=" + net::EscapeUrlEncodedData(query, true);
268 net::NetworkTrafficAnnotationTag traffic_annotation =
269 net::DefineNetworkTrafficAnnotation("...", R"(
270 semantics {
271 sender: "..."
msarda 2017/05/22 11:49:35 Same as above.
Ramin Halavati 2017/05/22 12:42:17 Done.
272 description: "..."
msarda 2017/05/22 11:49:36 This request fetches information about an OAuth 2.
Ramin Halavati 2017/05/22 12:42:16 Done.
273 trigger: "..."
msarda 2017/05/22 11:49:36 This is triggered after a Google account is added
Ramin Halavati 2017/05/22 12:42:17 Done.
274 data: "..."
msarda 2017/05/22 11:49:35 The OAuth 2.0 access token.
Ramin Halavati 2017/05/22 12:42:17 Done.
275 destination: WEBSITE/GOOGLE_OWNED_SERVICE/OTHER
msarda 2017/05/22 11:49:36 GOOGLE_OWNED_SERVICE
Ramin Halavati 2017/05/22 12:42:16 Done.
276 }
277 policy {
278 cookies_allowed: false
279 setting: "..."
msarda 2017/05/22 11:49:35 "This feature cannot be disabled in settings."
Ramin Halavati 2017/05/22 12:42:16 Done.
280 chrome_policy {
281 [POLICY_NAME] {
282 policy_options {mode: MANDATORY/RECOMMENDED/UNSET}
283 [POLICY_NAME]: ... //(value to disable it)
284 }
285 }
286 policy_exception_justification: "..."
287 })");
206 MakeGaiaRequest(GURL(GaiaUrls::GetInstance()->oauth2_token_info_url()), 288 MakeGaiaRequest(GURL(GaiaUrls::GetInstance()->oauth2_token_info_url()),
207 post_body, 289 post_body, max_retries, delegate, traffic_annotation);
208 max_retries,
209 delegate);
210 } 290 }
211 291
212 void GaiaOAuthClient::Core::MakeGaiaRequest( 292 void GaiaOAuthClient::Core::MakeGaiaRequest(
213 const GURL& url, 293 const GURL& url,
214 const std::string& post_body, 294 const std::string& post_body,
215 int max_retries, 295 int max_retries,
216 GaiaOAuthClient::Delegate* delegate) { 296 GaiaOAuthClient::Delegate* delegate,
297 const net::NetworkTrafficAnnotationTag& traffic_annotation) {
217 DCHECK(!request_.get()) << "Tried to fetch two things at once!"; 298 DCHECK(!request_.get()) << "Tried to fetch two things at once!";
218 delegate_ = delegate; 299 delegate_ = delegate;
219 num_retries_ = 0; 300 num_retries_ = 0;
220 request_ = 301 request_ = net::URLFetcher::Create(kUrlFetcherId, url, net::URLFetcher::POST,
221 net::URLFetcher::Create(kUrlFetcherId, url, net::URLFetcher::POST, this); 302 this, traffic_annotation);
222 request_->SetRequestContext(request_context_getter_.get()); 303 request_->SetRequestContext(request_context_getter_.get());
223 request_->SetUploadData("application/x-www-form-urlencoded", post_body); 304 request_->SetUploadData("application/x-www-form-urlencoded", post_body);
224 request_->SetMaxRetriesOn5xx(max_retries); 305 request_->SetMaxRetriesOn5xx(max_retries);
225 request_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | 306 request_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES |
226 net::LOAD_DO_NOT_SAVE_COOKIES); 307 net::LOAD_DO_NOT_SAVE_COOKIES);
227 MarkURLFetcherAsGaia(request_.get()); 308 MarkURLFetcherAsGaia(request_.get());
228 // See comment on SetAutomaticallyRetryOnNetworkChanges() above. 309 // See comment on SetAutomaticallyRetryOnNetworkChanges() above.
229 request_->SetAutomaticallyRetryOnNetworkChanges(3); 310 request_->SetAutomaticallyRetryOnNetworkChanges(3);
230 request_->Start(); 311 request_->Start();
231 } 312 }
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after
409 } 490 }
410 491
411 void GaiaOAuthClient::GetTokenHandleInfo(const std::string& token_handle, 492 void GaiaOAuthClient::GetTokenHandleInfo(const std::string& token_handle,
412 int max_retries, 493 int max_retries,
413 Delegate* delegate) { 494 Delegate* delegate) {
414 return core_->GetTokenInfo("token_handle", token_handle, max_retries, 495 return core_->GetTokenInfo("token_handle", token_handle, max_retries,
415 delegate); 496 delegate);
416 } 497 }
417 498
418 } // namespace gaia 499 } // namespace gaia
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698