forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgaia_oauth_client.cc
596 lines (534 loc) · 21.7 KB
/
gaia_oauth_client.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "google_apis/gaia/gaia_oauth_client.h"
#include <memory>
#include <utility>
#include "base/bind.h"
#include "base/check_op.h"
#include "base/json/json_reader.h"
#include "base/memory/weak_ptr.h"
#include "base/notreached.h"
#include "base/strings/string_util.h"
#include "base/values.h"
#include "google_apis/gaia/gaia_auth_util.h"
#include "google_apis/gaia/gaia_urls.h"
#include "net/base/backoff_entry.h"
#include "net/base/escape.h"
#include "net/base/load_flags.h"
#include "net/http/http_status_code.h"
#include "net/traffic_annotation/network_traffic_annotation.h"
#include "net/url_request/url_request_throttler_entry.h"
#include "services/network/public/cpp/resource_request.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "services/network/public/cpp/simple_url_loader.h"
#include "url/gurl.h"
namespace {
const char kAccessTokenValue[] = "access_token";
const char kRefreshTokenValue[] = "refresh_token";
const char kExpiresInValue[] = "expires_in";
}
namespace gaia {
class GaiaOAuthClient::Core
: public base::RefCountedThreadSafe<GaiaOAuthClient::Core> {
public:
Core(scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory)
: backoff_entry_(&backoff_policy_),
num_retries_(0),
max_retries_(0),
url_loader_factory_(url_loader_factory),
delegate_(nullptr),
request_type_(NO_PENDING_REQUEST) {
backoff_policy_.num_errors_to_ignore =
net::URLRequestThrottlerEntry::kDefaultNumErrorsToIgnore;
backoff_policy_.initial_delay_ms =
net::URLRequestThrottlerEntry::kDefaultInitialDelayMs;
backoff_policy_.multiply_factor =
net::URLRequestThrottlerEntry::kDefaultMultiplyFactor;
backoff_policy_.jitter_factor =
net::URLRequestThrottlerEntry::kDefaultJitterFactor;
backoff_policy_.maximum_backoff_ms =
net::URLRequestThrottlerEntry::kDefaultMaximumBackoffMs;
backoff_policy_.entry_lifetime_ms =
net::URLRequestThrottlerEntry::kDefaultEntryLifetimeMs;
backoff_policy_.always_use_initial_delay = false;
}
void GetTokensFromAuthCode(const OAuthClientInfo& oauth_client_info,
const std::string& auth_code,
int max_retries,
GaiaOAuthClient::Delegate* delegate);
void RefreshToken(const OAuthClientInfo& oauth_client_info,
const std::string& refresh_token,
const std::vector<std::string>& scopes,
int max_retries,
GaiaOAuthClient::Delegate* delegate);
void GetUserEmail(const std::string& oauth_access_token,
int max_retries,
Delegate* delegate);
void GetUserId(const std::string& oauth_access_token,
int max_retries,
Delegate* delegate);
void GetUserInfo(const std::string& oauth_access_token,
int max_retries,
Delegate* delegate);
void GetTokenInfo(const std::string& qualifier,
const std::string& query,
int max_retries,
Delegate* delegate);
// Called as a SimpleURLLoader callback
void OnURLLoadComplete(std::unique_ptr<std::string> body);
private:
friend class base::RefCountedThreadSafe<Core>;
enum RequestType {
NO_PENDING_REQUEST,
TOKENS_FROM_AUTH_CODE,
REFRESH_TOKEN,
TOKEN_INFO,
USER_EMAIL,
USER_ID,
USER_INFO,
};
~Core() {}
void GetUserInfoImpl(RequestType type,
const std::string& oauth_access_token,
int max_retries,
Delegate* delegate);
// Stores request settings into |this| and calls SendRequest().
void MakeRequest(
RequestType type,
const GURL& url,
std::string post_body /* may be empty if not needed*/,
std::string authorization_header /* empty if not needed */,
int max_retries,
GaiaOAuthClient::Delegate* delegate,
const net::MutableNetworkTrafficAnnotationTag& traffic_annotation);
// Sends out a request based on things MakeRequest() stored...
// once |backoff_entry_| says it's OK. Can be called many times to retry,
// assuming |request_| is destroyed first.
void SendRequest();
// Actually sends the request.
void SendRequestImpl();
void HandleResponse(std::unique_ptr<std::string> body,
bool* should_retry_request);
net::BackoffEntry::Policy backoff_policy_;
net::BackoffEntry backoff_entry_;
int num_retries_;
int max_retries_;
GURL url_;
net::MutableNetworkTrafficAnnotationTag traffic_annotation_;
std::string post_body_;
std::string authorization_header_;
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory_;
GaiaOAuthClient::Delegate* delegate_;
std::unique_ptr<network::SimpleURLLoader> request_;
RequestType request_type_;
base::WeakPtrFactory<Core> weak_ptr_factory_{this};
};
void GaiaOAuthClient::Core::GetTokensFromAuthCode(
const OAuthClientInfo& oauth_client_info,
const std::string& auth_code,
int max_retries,
GaiaOAuthClient::Delegate* delegate) {
std::string post_body =
"code=" + net::EscapeUrlEncodedData(auth_code, true) +
"&client_id=" + net::EscapeUrlEncodedData(oauth_client_info.client_id,
true) +
"&client_secret=" +
net::EscapeUrlEncodedData(oauth_client_info.client_secret, true) +
"&redirect_uri=" +
net::EscapeUrlEncodedData(oauth_client_info.redirect_uri, true) +
"&grant_type=authorization_code";
net::MutableNetworkTrafficAnnotationTag traffic_annotation(
net::DefineNetworkTrafficAnnotation("gaia_oauth_client_get_tokens", R"(
semantics {
sender: "OAuth 2.0 calls"
description:
"This request exchanges an authorization code for an OAuth 2.0 "
"refresh token and an OAuth 2.0 access token."
trigger:
"This request is triggered when a Chrome service requires an "
"access token and a refresh token (e.g. Cloud Print, Chrome Remote "
"Desktop etc.) See https://developers.google.com/identity/protocols"
"/OAuth2 for more information about the Google implementation of "
"the OAuth 2.0 protocol."
data:
"The Google console client ID and client secret of the caller, the "
"OAuth authorization code and the redirect URI."
destination: GOOGLE_OWNED_SERVICE
}
policy {
cookies_allowed: NO
setting:
"This feature cannot be disabled in settings, but if the user "
"signs out of Chrome, this request would not be made."
chrome_policy {
SigninAllowed {
policy_options {mode: MANDATORY}
SigninAllowed: false
}
}
})"));
MakeRequest(TOKENS_FROM_AUTH_CODE,
GURL(GaiaUrls::GetInstance()->oauth2_token_url()), post_body,
/* authorization_header = */ std::string(), max_retries, delegate,
traffic_annotation);
}
void GaiaOAuthClient::Core::RefreshToken(
const OAuthClientInfo& oauth_client_info,
const std::string& refresh_token,
const std::vector<std::string>& scopes,
int max_retries,
GaiaOAuthClient::Delegate* delegate) {
std::string post_body =
"refresh_token=" + net::EscapeUrlEncodedData(refresh_token, true) +
"&client_id=" + net::EscapeUrlEncodedData(oauth_client_info.client_id,
true) +
"&client_secret=" +
net::EscapeUrlEncodedData(oauth_client_info.client_secret, true) +
"&grant_type=refresh_token";
if (!scopes.empty()) {
std::string scopes_string = base::JoinString(scopes, " ");
post_body += "&scope=" + net::EscapeUrlEncodedData(scopes_string, true);
}
net::MutableNetworkTrafficAnnotationTag traffic_annotation(
net::DefineNetworkTrafficAnnotation("gaia_oauth_client_refresh_token", R"(
semantics {
sender: "OAuth 2.0 calls"
description:
"This request fetches a fresh access token that can be used to "
"authenticate an API call to a Google web endpoint."
trigger:
"This is called whenever the caller needs a fresh OAuth 2.0 access "
"token."
data:
"The OAuth 2.0 refresh token, the Google console client ID and "
"client secret of the caller, and optionally the scopes of the API "
"for which the access token should be authorized."
destination: GOOGLE_OWNED_SERVICE
}
policy {
cookies_allowed: NO
setting:
"This feature cannot be disabled in settings, but if the user "
"signs out of Chrome, this request would not be made."
chrome_policy {
SigninAllowed {
policy_options {mode: MANDATORY}
SigninAllowed: false
}
}
})"));
MakeRequest(REFRESH_TOKEN, GURL(GaiaUrls::GetInstance()->oauth2_token_url()),
post_body,
/* authorization_header = */ std::string(), max_retries, delegate,
traffic_annotation);
}
void GaiaOAuthClient::Core::GetUserEmail(const std::string& oauth_access_token,
int max_retries,
Delegate* delegate) {
GetUserInfoImpl(USER_EMAIL, oauth_access_token, max_retries, delegate);
}
void GaiaOAuthClient::Core::GetUserId(const std::string& oauth_access_token,
int max_retries,
Delegate* delegate) {
GetUserInfoImpl(USER_ID, oauth_access_token, max_retries, delegate);
}
void GaiaOAuthClient::Core::GetUserInfo(const std::string& oauth_access_token,
int max_retries,
Delegate* delegate) {
GetUserInfoImpl(USER_INFO, oauth_access_token, max_retries, delegate);
}
void GaiaOAuthClient::Core::GetUserInfoImpl(
RequestType type,
const std::string& oauth_access_token,
int max_retries,
Delegate* delegate) {
net::MutableNetworkTrafficAnnotationTag traffic_annotation(
net::DefineNetworkTrafficAnnotation("gaia_oauth_client_get_user_info", R"(
semantics {
sender: "OAuth 2.0 calls"
description:
"This request is used to fetch profile information about the user, "
"like the email, the ID of the account, the full name, and the "
"profile picture."
trigger:
"The main trigger for this request is in the AccountTrackerService "
"that fetches the user info soon after the user signs in."
data:
"The OAuth 2.0 access token of the account."
destination: GOOGLE_OWNED_SERVICE
}
policy {
cookies_allowed: NO
setting:
"This feature cannot be disabled in settings, but if the user "
"signs out of Chrome, this request would not be made."
chrome_policy {
SigninAllowed {
policy_options {mode: MANDATORY}
SigninAllowed: false
}
}
})"));
std::string auth = "OAuth " + oauth_access_token;
MakeRequest(type, GaiaUrls::GetInstance()->oauth_user_info_url(),
/* post_body = */ std::string(), auth, max_retries, delegate,
traffic_annotation);
}
void GaiaOAuthClient::Core::GetTokenInfo(const std::string& qualifier,
const std::string& query,
int max_retries,
Delegate* delegate) {
std::string post_body =
qualifier + "=" + net::EscapeUrlEncodedData(query, true);
net::MutableNetworkTrafficAnnotationTag traffic_annotation(
net::DefineNetworkTrafficAnnotation("gaia_oauth_client_get_token_info",
R"(
semantics {
sender: "OAuth 2.0 calls"
description:
"This request fetches information about an OAuth 2.0 access token. "
"The response is a dictionary of response values. The provided "
"access token may have any scope, and basic results will be "
"returned: issued_to, audience, scope, expires_in, access_type. In "
"addition, if the https://www.googleapis.com/auth/userinfo.email "
"scope is present, the email and verified_email fields will be "
"returned. If the https://www.googleapis.com/auth/userinfo.profile "
"scope is present, the user_id field will be returned."
trigger:
"This is triggered after a Google account is added to the browser. "
"It it also triggered after each successful fetch of an OAuth 2.0 "
"access token."
data: "The OAuth 2.0 access token."
destination: GOOGLE_OWNED_SERVICE
}
policy {
cookies_allowed: NO
setting:
"This feature cannot be disabled in settings, but if the user "
"signs out of Chrome, this request would not be made."
chrome_policy {
SigninAllowed {
policy_options {mode: MANDATORY}
SigninAllowed: false
}
}
})"));
MakeRequest(TOKEN_INFO,
GURL(GaiaUrls::GetInstance()->oauth2_token_info_url()), post_body,
/* authorization_header = */ std::string(), max_retries, delegate,
traffic_annotation);
}
void GaiaOAuthClient::Core::MakeRequest(
RequestType type,
const GURL& url,
std::string post_body,
std::string authorization_header,
int max_retries,
GaiaOAuthClient::Delegate* delegate,
const net::MutableNetworkTrafficAnnotationTag& traffic_annotation) {
DCHECK_EQ(request_type_, NO_PENDING_REQUEST);
request_type_ = type;
delegate_ = delegate;
num_retries_ = 0;
max_retries_ = max_retries;
url_ = url;
traffic_annotation_ = traffic_annotation;
post_body_ = std::move(post_body);
authorization_header_ = std::move(authorization_header);
SendRequest();
}
void GaiaOAuthClient::Core::SendRequest() {
if (backoff_entry_.ShouldRejectRequest()) {
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE,
base::BindOnce(&GaiaOAuthClient::Core::SendRequestImpl,
weak_ptr_factory_.GetWeakPtr()),
backoff_entry_.GetTimeUntilRelease());
} else {
SendRequestImpl();
}
}
void GaiaOAuthClient::Core::SendRequestImpl() {
DCHECK(!request_.get()) << "Tried to fetch two things at once!";
auto resource_request = std::make_unique<network::ResourceRequest>();
resource_request->url = url_;
resource_request->method = post_body_.empty() ? "GET" : "POST";
resource_request->credentials_mode = network::mojom::CredentialsMode::kOmit;
if (!authorization_header_.empty())
resource_request->headers.SetHeader("Authorization", authorization_header_);
request_ = network::SimpleURLLoader::Create(
std::move(resource_request),
static_cast<net::NetworkTrafficAnnotationTag>(traffic_annotation_));
if (!post_body_.empty()) {
request_->AttachStringForUpload(post_body_,
"application/x-www-form-urlencoded");
}
// Retry is implemented internally.
request_->SetRetryOptions(0, network::SimpleURLLoader::RETRY_NEVER);
request_->DownloadToStringOfUnboundedSizeUntilCrashAndDie(
url_loader_factory_.get(),
// Unretained(this) is safe since |this| owns |request_|, and its deletion
// will cancel the callback.
base::BindOnce(&GaiaOAuthClient::Core::OnURLLoadComplete,
base::Unretained(this)));
}
void GaiaOAuthClient::Core::OnURLLoadComplete(
std::unique_ptr<std::string> body) {
bool should_retry = false;
base::WeakPtr<GaiaOAuthClient::Core> weak_this =
weak_ptr_factory_.GetWeakPtr();
// HandleResponse() may delete |this| if it assigns |should_retry| == false.
HandleResponse(std::move(body), &should_retry);
if (should_retry) {
num_retries_++;
backoff_entry_.InformOfRequest(false);
SendRequest();
} else {
if (weak_this)
backoff_entry_.InformOfRequest(true);
}
}
void GaiaOAuthClient::Core::HandleResponse(std::unique_ptr<std::string> body,
bool* should_retry_request) {
*should_retry_request = false;
// Move ownership of the request fetcher into a local scoped_ptr which
// will be nuked when we're done handling the request.
std::unique_ptr<network::SimpleURLLoader> source = std::move(request_);
int response_code = -1;
if (source->ResponseInfo() && source->ResponseInfo()->headers)
response_code = source->ResponseInfo()->headers->response_code();
// HTTP_BAD_REQUEST means the arguments are invalid. HTTP_UNAUTHORIZED means
// the access or refresh token is invalid. No point retrying. We are
// done here.
if (response_code == net::HTTP_BAD_REQUEST ||
response_code == net::HTTP_UNAUTHORIZED) {
delegate_->OnOAuthError();
return;
}
std::unique_ptr<base::DictionaryValue> response_dict;
if (response_code == net::HTTP_OK && body) {
std::string data = std::move(*body);
std::unique_ptr<base::Value> message_value =
base::JSONReader::ReadDeprecated(data);
if (message_value.get() && message_value->is_dict()) {
response_dict.reset(
static_cast<base::DictionaryValue*>(message_value.release()));
}
}
if (!response_dict.get()) {
// If we don't have an access token yet and the the error was not
// RC_BAD_REQUEST, we may need to retry.
if ((max_retries_ != -1) && (num_retries_ >= max_retries_)) {
// Retry limit reached. Give up.
request_type_ = NO_PENDING_REQUEST;
delegate_->OnNetworkError(response_code);
} else {
*should_retry_request = true;
}
return;
}
RequestType type = request_type_;
request_type_ = NO_PENDING_REQUEST;
switch (type) {
case USER_EMAIL: {
std::string email;
response_dict->GetString("email", &email);
delegate_->OnGetUserEmailResponse(email);
break;
}
case USER_ID: {
std::string id;
response_dict->GetString("id", &id);
delegate_->OnGetUserIdResponse(id);
break;
}
case USER_INFO: {
delegate_->OnGetUserInfoResponse(std::move(response_dict));
break;
}
case TOKEN_INFO: {
delegate_->OnGetTokenInfoResponse(std::move(response_dict));
break;
}
case TOKENS_FROM_AUTH_CODE:
case REFRESH_TOKEN: {
std::string access_token;
std::string refresh_token;
int expires_in_seconds = 0;
response_dict->GetString(kAccessTokenValue, &access_token);
response_dict->GetString(kRefreshTokenValue, &refresh_token);
response_dict->GetInteger(kExpiresInValue, &expires_in_seconds);
if (access_token.empty()) {
delegate_->OnOAuthError();
return;
}
if (type == REFRESH_TOKEN) {
delegate_->OnRefreshTokenResponse(access_token, expires_in_seconds);
} else {
delegate_->OnGetTokensResponse(refresh_token,
access_token,
expires_in_seconds);
}
break;
}
default:
NOTREACHED();
}
}
GaiaOAuthClient::GaiaOAuthClient(
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory) {
core_ = new Core(std::move(url_loader_factory));
}
GaiaOAuthClient::~GaiaOAuthClient() {
}
void GaiaOAuthClient::GetTokensFromAuthCode(
const OAuthClientInfo& oauth_client_info,
const std::string& auth_code,
int max_retries,
Delegate* delegate) {
return core_->GetTokensFromAuthCode(oauth_client_info,
auth_code,
max_retries,
delegate);
}
void GaiaOAuthClient::RefreshToken(
const OAuthClientInfo& oauth_client_info,
const std::string& refresh_token,
const std::vector<std::string>& scopes,
int max_retries,
Delegate* delegate) {
return core_->RefreshToken(oauth_client_info,
refresh_token,
scopes,
max_retries,
delegate);
}
void GaiaOAuthClient::GetUserEmail(const std::string& access_token,
int max_retries,
Delegate* delegate) {
return core_->GetUserEmail(access_token, max_retries, delegate);
}
void GaiaOAuthClient::GetUserId(const std::string& access_token,
int max_retries,
Delegate* delegate) {
return core_->GetUserId(access_token, max_retries, delegate);
}
void GaiaOAuthClient::GetUserInfo(const std::string& access_token,
int max_retries,
Delegate* delegate) {
return core_->GetUserInfo(access_token, max_retries, delegate);
}
void GaiaOAuthClient::GetTokenInfo(const std::string& access_token,
int max_retries,
Delegate* delegate) {
return core_->GetTokenInfo("access_token", access_token, max_retries,
delegate);
}
void GaiaOAuthClient::GetTokenHandleInfo(const std::string& token_handle,
int max_retries,
Delegate* delegate) {
return core_->GetTokenInfo("token_handle", token_handle, max_retries,
delegate);
}
} // namespace gaia