forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoauth2_access_token_fetcher_impl.cc
354 lines (310 loc) · 12.7 KB
/
oauth2_access_token_fetcher_impl.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
// Copyright 2014 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/oauth2_access_token_fetcher_impl.h"
#include <algorithm>
#include <string>
#include <vector>
#include "base/bind.h"
#include "base/json/json_reader.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/time/time.h"
#include "base/values.h"
#include "google_apis/gaia/gaia_auth_util.h"
#include "google_apis/gaia/google_service_auth_error.h"
#include "net/base/escape.h"
#include "net/http/http_status_code.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"
namespace {
constexpr char kGetAccessTokenBodyFormat[] =
"client_id=%s&"
"client_secret=%s&"
"grant_type=%s&"
"%s=%s";
constexpr char kGetAccessTokenBodyWithScopeFormat[] =
"client_id=%s&"
"client_secret=%s&"
"grant_type=%s&"
"%s=%s&"
"scope=%s";
constexpr char kGrantTypeAuthCode[] = "authorization_code";
constexpr char kGrantTypeRefreshToken[] = "refresh_token";
constexpr char kKeyAuthCode[] = "code";
constexpr char kKeyRefreshToken[] = "refresh_token";
constexpr char kAccessTokenKey[] = "access_token";
constexpr char krefreshTokenKey[] = "refresh_token";
constexpr char kExpiresInKey[] = "expires_in";
constexpr char kIdTokenKey[] = "id_token";
constexpr char kErrorKey[] = "error";
OAuth2AccessTokenFetcherImpl::OAuth2ErrorCodesForHistogram
OAuth2ErrorToHistogramValue(const std::string& error) {
if (error == "invalid_request")
return OAuth2AccessTokenFetcherImpl::OAUTH2_ACCESS_ERROR_INVALID_REQUEST;
else if (error == "invalid_client")
return OAuth2AccessTokenFetcherImpl::OAUTH2_ACCESS_ERROR_INVALID_CLIENT;
else if (error == "invalid_grant")
return OAuth2AccessTokenFetcherImpl::OAUTH2_ACCESS_ERROR_INVALID_GRANT;
else if (error == "unauthorized_client")
return OAuth2AccessTokenFetcherImpl::
OAUTH2_ACCESS_ERROR_UNAUTHORIZED_CLIENT;
else if (error == "unsupported_grant_type")
return OAuth2AccessTokenFetcherImpl::
OAUTH2_ACCESS_ERROR_UNSUPPORTED_GRANT_TYPE;
else if (error == "invalid_scope")
return OAuth2AccessTokenFetcherImpl::OAUTH2_ACCESS_ERROR_INVALID_SCOPE;
return OAuth2AccessTokenFetcherImpl::OAUTH2_ACCESS_ERROR_UNKNOWN;
}
static GoogleServiceAuthError CreateAuthError(int net_error) {
CHECK_NE(net_error, net::OK);
DLOG(WARNING) << "Could not reach Authorization servers: errno " << net_error;
return GoogleServiceAuthError::FromConnectionError(net_error);
}
static std::unique_ptr<network::SimpleURLLoader> CreateURLLoader(
const GURL& url,
const std::string& body,
const net::NetworkTrafficAnnotationTag& traffic_annotation) {
auto resource_request = std::make_unique<network::ResourceRequest>();
resource_request->url = url;
resource_request->credentials_mode = network::mojom::CredentialsMode::kOmit;
if (!body.empty())
resource_request->method = "POST";
auto url_loader = network::SimpleURLLoader::Create(
std::move(resource_request), traffic_annotation);
if (!body.empty())
url_loader->AttachStringForUpload(body,
"application/x-www-form-urlencoded");
// We want to receive the body even on error, as it may contain the reason for
// failure.
url_loader->SetAllowHttpErrorResults(true);
// Fetchers are sometimes cancelled because a network change was detected,
// especially at startup and after sign-in on ChromeOS. Retrying once should
// be enough in those cases; let the fetcher retry up to 3 times just in case.
// http://crbug.com/163710
url_loader->SetRetryOptions(
3, network::SimpleURLLoader::RETRY_ON_NETWORK_CHANGE);
return url_loader;
}
std::unique_ptr<base::DictionaryValue> ParseGetAccessTokenResponse(
std::unique_ptr<std::string> data) {
if (!data)
return nullptr;
std::unique_ptr<base::Value> value = base::JSONReader::ReadDeprecated(*data);
if (!value.get() || value->type() != base::Value::Type::DICTIONARY)
value.reset();
return std::unique_ptr<base::DictionaryValue>(
static_cast<base::DictionaryValue*>(value.release()));
}
} // namespace
OAuth2AccessTokenFetcherImpl::OAuth2AccessTokenFetcherImpl(
OAuth2AccessTokenConsumer* consumer,
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
const std::string& refresh_token,
const std::string& auth_code)
: OAuth2AccessTokenFetcher(consumer),
url_loader_factory_(url_loader_factory),
refresh_token_(refresh_token),
auth_code_(auth_code),
state_(INITIAL) {
// It's an error to specify neither a refresh token nor an auth code, or
// to specify both at the same time.
CHECK_NE(refresh_token_.empty(), auth_code_.empty());
}
OAuth2AccessTokenFetcherImpl::~OAuth2AccessTokenFetcherImpl() = default;
void OAuth2AccessTokenFetcherImpl::CancelRequest() {
url_loader_.reset();
}
void OAuth2AccessTokenFetcherImpl::Start(
const std::string& client_id,
const std::string& client_secret,
const std::vector<std::string>& scopes) {
client_id_ = client_id;
client_secret_ = client_secret;
scopes_ = scopes;
StartGetAccessToken();
}
void OAuth2AccessTokenFetcherImpl::StartGetAccessToken() {
CHECK_EQ(INITIAL, state_);
state_ = GET_ACCESS_TOKEN_STARTED;
url_loader_ = CreateURLLoader(
GetAccessTokenURL(),
MakeGetAccessTokenBody(client_id_, client_secret_, refresh_token_,
auth_code_, scopes_),
GetTrafficAnnotationTag());
// It's safe to use Unretained below as the |url_loader_| is owned by |this|.
url_loader_->DownloadToString(
url_loader_factory_.get(),
base::BindOnce(&OAuth2AccessTokenFetcherImpl::OnURLLoadComplete,
base::Unretained(this)),
1024 * 1024);
}
void OAuth2AccessTokenFetcherImpl::EndGetAccessToken(
std::unique_ptr<std::string> response_body) {
CHECK_EQ(GET_ACCESS_TOKEN_STARTED, state_);
state_ = GET_ACCESS_TOKEN_DONE;
bool net_failure = false;
int histogram_value;
if (url_loader_->NetError() == net::OK && url_loader_->ResponseInfo() &&
url_loader_->ResponseInfo()->headers) {
histogram_value = url_loader_->ResponseInfo()->headers->response_code();
} else {
histogram_value = url_loader_->NetError();
net_failure = true;
}
RecordResponseCodeUma(histogram_value);
if (net_failure) {
OnGetTokenFailure(CreateAuthError(histogram_value));
return;
}
int response_code = url_loader_->ResponseInfo()->headers->response_code();
switch (response_code) {
case net::HTTP_OK:
break;
case net::HTTP_PROXY_AUTHENTICATION_REQUIRED:
NOTREACHED() << "HTTP 407 should be treated as a network error.";
// If this ever happens in production, we treat it as a temporary error as
// it is similar to a network error.
OnGetTokenFailure(
GoogleServiceAuthError(GoogleServiceAuthError::SERVICE_UNAVAILABLE));
return;
case net::HTTP_FORBIDDEN:
// HTTP_FORBIDDEN (403) is treated as temporary error, because it may be
// '403 Rate Limit Exeeded.'
OnGetTokenFailure(
GoogleServiceAuthError(GoogleServiceAuthError::SERVICE_UNAVAILABLE));
return;
case net::HTTP_BAD_REQUEST: {
// HTTP_BAD_REQUEST (400) usually contains error as per
// http://tools.ietf.org/html/rfc6749#section-5.2.
std::string oauth2_error;
if (!ParseGetAccessTokenFailureResponse(std::move(response_body),
&oauth2_error)) {
OnGetTokenFailure(
GoogleServiceAuthError(GoogleServiceAuthError::SERVICE_ERROR));
return;
}
OAuth2ErrorCodesForHistogram access_error(
OAuth2ErrorToHistogramValue(oauth2_error));
RecordBadRequestTypeUma(access_error);
OnGetTokenFailure(
access_error == OAUTH2_ACCESS_ERROR_INVALID_GRANT
? GoogleServiceAuthError::FromInvalidGaiaCredentialsReason(
GoogleServiceAuthError::InvalidGaiaCredentialsReason::
CREDENTIALS_REJECTED_BY_SERVER)
: GoogleServiceAuthError(GoogleServiceAuthError::SERVICE_ERROR));
return;
}
default: {
if (response_code >= net::HTTP_INTERNAL_SERVER_ERROR) {
// 5xx is always treated as transient.
OnGetTokenFailure(GoogleServiceAuthError(
GoogleServiceAuthError::SERVICE_UNAVAILABLE));
} else {
// The other errors are treated as permanent error.
DLOG(ERROR) << "Unexpected persistent error: http_status="
<< response_code;
OnGetTokenFailure(
GoogleServiceAuthError::FromInvalidGaiaCredentialsReason(
GoogleServiceAuthError::InvalidGaiaCredentialsReason::
CREDENTIALS_REJECTED_BY_SERVER));
}
return;
}
}
// The request was successfully fetched and it returned OK.
// Parse out the access token and the expiration time.
OAuth2AccessTokenConsumer::TokenResponse token_response;
if (!ParseGetAccessTokenSuccessResponse(std::move(response_body),
&token_response)) {
DLOG(WARNING) << "Response doesn't match expected format";
OnGetTokenFailure(
GoogleServiceAuthError(GoogleServiceAuthError::SERVICE_UNAVAILABLE));
return;
}
// The token will expire in |expires_in| seconds. Take a 10% error margin to
// prevent reusing a token too close to its expiration date.
OnGetTokenSuccess(token_response);
}
void OAuth2AccessTokenFetcherImpl::OnGetTokenSuccess(
const OAuth2AccessTokenConsumer::TokenResponse& token_response) {
FireOnGetTokenSuccess(token_response);
}
void OAuth2AccessTokenFetcherImpl::OnGetTokenFailure(
const GoogleServiceAuthError& error) {
state_ = ERROR_STATE;
FireOnGetTokenFailure(error);
}
void OAuth2AccessTokenFetcherImpl::OnURLLoadComplete(
std::unique_ptr<std::string> response_body) {
CHECK(state_ == GET_ACCESS_TOKEN_STARTED);
EndGetAccessToken(std::move(response_body));
}
// static
std::string OAuth2AccessTokenFetcherImpl::MakeGetAccessTokenBody(
const std::string& client_id,
const std::string& client_secret,
const std::string& refresh_token,
const std::string& auth_code,
const std::vector<std::string>& scopes) {
// It's an error to specify neither a refresh token nor an auth code, or
// to specify both at the same time.
CHECK_NE(refresh_token.empty(), auth_code.empty());
std::string enc_client_id = net::EscapeUrlEncodedData(client_id, true);
std::string enc_client_secret =
net::EscapeUrlEncodedData(client_secret, true);
const char* key = nullptr;
const char* grant_type = nullptr;
std::string enc_value;
if (refresh_token.empty()) {
key = kKeyAuthCode;
grant_type = kGrantTypeAuthCode;
enc_value = net::EscapeUrlEncodedData(auth_code, true);
} else {
key = kKeyRefreshToken;
grant_type = kGrantTypeRefreshToken;
enc_value = net::EscapeUrlEncodedData(refresh_token, true);
}
if (scopes.empty()) {
return base::StringPrintf(kGetAccessTokenBodyFormat, enc_client_id.c_str(),
enc_client_secret.c_str(), grant_type, key,
enc_value.c_str());
} else {
std::string scopes_string = base::JoinString(scopes, " ");
return base::StringPrintf(
kGetAccessTokenBodyWithScopeFormat, enc_client_id.c_str(),
enc_client_secret.c_str(), grant_type, key, enc_value.c_str(),
net::EscapeUrlEncodedData(scopes_string, true).c_str());
}
}
// static
bool OAuth2AccessTokenFetcherImpl::ParseGetAccessTokenSuccessResponse(
std::unique_ptr<std::string> response_body,
OAuth2AccessTokenConsumer::TokenResponse* token_response) {
CHECK(token_response);
std::unique_ptr<base::DictionaryValue> value =
ParseGetAccessTokenResponse(std::move(response_body));
if (!value)
return false;
// Refresh and id token are optional and don't cause an error if missing.
value->GetString(krefreshTokenKey, &token_response->refresh_token);
value->GetString(kIdTokenKey, &token_response->id_token);
int expires_in;
bool ok = value->GetString(kAccessTokenKey, &token_response->access_token) &&
value->GetInteger(kExpiresInKey, &expires_in);
if (ok) {
token_response->expiration_time =
base::Time::Now() + base::TimeDelta::FromSeconds(9 * expires_in / 10);
}
return ok;
}
// static
bool OAuth2AccessTokenFetcherImpl::ParseGetAccessTokenFailureResponse(
std::unique_ptr<std::string> response_body,
std::string* error) {
CHECK(error);
std::unique_ptr<base::DictionaryValue> value =
ParseGetAccessTokenResponse(std::move(response_body));
return value ? value->GetString(kErrorKey, error) : false;
}