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
|
// Copyright (c) 2010 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 "chrome/common/net/gaia/gaia_authenticator2.h"
#include <string>
#include <utility>
#include <vector>
#include "base/string_split.h"
#include "base/string_util.h"
#include "chrome/common/net/gaia/gaia_auth_consumer.h"
#include "chrome/common/net/gaia/google_service_auth_error.h"
#include "chrome/common/net/http_return.h"
#include "chrome/common/net/url_request_context_getter.h"
#include "net/base/load_flags.h"
#include "net/url_request/url_request_status.h"
#include "third_party/libjingle/source/talk/base/urlencode.h"
// TODO(chron): Add sourceless version of this formatter.
// static
const char GaiaAuthenticator2::kClientLoginFormat[] =
"Email=%s&"
"Passwd=%s&"
"PersistentCookie=%s&"
"accountType=%s&"
"source=%s&"
"service=%s";
// static
const char GaiaAuthenticator2::kClientLoginCaptchaFormat[] =
"Email=%s&"
"Passwd=%s&"
"PersistentCookie=%s&"
"accountType=%s&"
"source=%s&"
"service=%s&"
"logintoken=%s&"
"logincaptcha=%s";
// static
const char GaiaAuthenticator2::kIssueAuthTokenFormat[] =
"SID=%s&"
"LSID=%s&"
"service=%s&"
"Session=true";
// static
const char GaiaAuthenticator2::kGetUserInfoFormat[] =
"LSID=%s";
// static
const char GaiaAuthenticator2::kAccountDeletedError[] = "AccountDeleted";
// static
const char GaiaAuthenticator2::kAccountDisabledError[] = "AccountDisabled";
// static
const char GaiaAuthenticator2::kCaptchaError[] = "CaptchaRequired";
// static
const char GaiaAuthenticator2::kServiceUnavailableError[] =
"ServiceUnavailable";
// static
const char GaiaAuthenticator2::kErrorParam[] = "Error";
// static
const char GaiaAuthenticator2::kErrorUrlParam[] = "Url";
// static
const char GaiaAuthenticator2::kCaptchaUrlParam[] = "CaptchaUrl";
// static
const char GaiaAuthenticator2::kCaptchaTokenParam[] = "CaptchaToken";
// static
const char GaiaAuthenticator2::kCaptchaUrlPrefix[] =
"http://www.google.com/accounts/";
// static
const char GaiaAuthenticator2::kCookiePersistence[] = "true";
// static
// TODO(johnnyg): When hosted accounts are supported by sync,
// we can always use "HOSTED_OR_GOOGLE"
const char GaiaAuthenticator2::kAccountTypeHostedOrGoogle[] =
"HOSTED_OR_GOOGLE";
const char GaiaAuthenticator2::kAccountTypeGoogle[] =
"GOOGLE";
// static
const char GaiaAuthenticator2::kSecondFactor[] = "Info=InvalidSecondFactor";
// TODO(chron): These urls are also in auth_response_handler.h.
// The URLs for different calls in the Google Accounts programmatic login API.
const char GaiaAuthenticator2::kClientLoginUrl[] =
"https://www.google.com/accounts/ClientLogin";
const char GaiaAuthenticator2::kIssueAuthTokenUrl[] =
"https://www.google.com/accounts/IssueAuthToken";
const char GaiaAuthenticator2::kGetUserInfoUrl[] =
"https://www.google.com/accounts/GetUserInfo";
GaiaAuthenticator2::GaiaAuthenticator2(GaiaAuthConsumer* consumer,
const std::string& source,
URLRequestContextGetter* getter)
: consumer_(consumer),
getter_(getter),
source_(source),
client_login_gurl_(kClientLoginUrl),
issue_auth_token_gurl_(kIssueAuthTokenUrl),
get_user_info_gurl_(kGetUserInfoUrl),
fetch_pending_(false) {}
GaiaAuthenticator2::~GaiaAuthenticator2() {}
bool GaiaAuthenticator2::HasPendingFetch() {
return fetch_pending_;
}
void GaiaAuthenticator2::CancelRequest() {
fetcher_.reset();
fetch_pending_ = false;
}
// static
URLFetcher* GaiaAuthenticator2::CreateGaiaFetcher(
URLRequestContextGetter* getter,
const std::string& body,
const GURL& gaia_gurl,
URLFetcher::Delegate* delegate) {
URLFetcher* to_return =
URLFetcher::Create(0,
gaia_gurl,
URLFetcher::POST,
delegate);
to_return->set_request_context(getter);
to_return->set_load_flags(net::LOAD_DO_NOT_SEND_COOKIES);
to_return->set_upload_data("application/x-www-form-urlencoded", body);
return to_return;
}
// static
std::string GaiaAuthenticator2::MakeClientLoginBody(
const std::string& username,
const std::string& password,
const std::string& source,
const char* service,
const std::string& login_token,
const std::string& login_captcha,
HostedAccountsSetting allow_hosted_accounts) {
std::string encoded_username = UrlEncodeString(username);
std::string encoded_password = UrlEncodeString(password);
std::string encoded_login_token = UrlEncodeString(login_token);
std::string encoded_login_captcha = UrlEncodeString(login_captcha);
const char* account_type = allow_hosted_accounts == HostedAccountsAllowed ?
kAccountTypeHostedOrGoogle :
kAccountTypeGoogle;
if (login_token.empty() || login_captcha.empty()) {
return StringPrintf(kClientLoginFormat,
encoded_username.c_str(),
encoded_password.c_str(),
kCookiePersistence,
account_type,
source.c_str(),
service);
}
return StringPrintf(kClientLoginCaptchaFormat,
encoded_username.c_str(),
encoded_password.c_str(),
kCookiePersistence,
account_type,
source.c_str(),
service,
encoded_login_token.c_str(),
encoded_login_captcha.c_str());
}
// static
std::string GaiaAuthenticator2::MakeIssueAuthTokenBody(
const std::string& sid,
const std::string& lsid,
const char* const service) {
std::string encoded_sid = UrlEncodeString(sid);
std::string encoded_lsid = UrlEncodeString(lsid);
return StringPrintf(kIssueAuthTokenFormat,
encoded_sid.c_str(),
encoded_lsid.c_str(),
service);
}
// static
std::string GaiaAuthenticator2::MakeGetUserInfoBody(const std::string& lsid) {
std::string encoded_lsid = UrlEncodeString(lsid);
return StringPrintf(kGetUserInfoFormat, encoded_lsid.c_str());
}
// Helper method that extracts tokens from a successful reply.
// static
void GaiaAuthenticator2::ParseClientLoginResponse(const std::string& data,
std::string* sid,
std::string* lsid,
std::string* token) {
using std::vector;
using std::pair;
using std::string;
vector<pair<string, string> > tokens;
base::SplitStringIntoKeyValuePairs(data, '=', '\n', &tokens);
for (vector<pair<string, string> >::iterator i = tokens.begin();
i != tokens.end(); ++i) {
if (i->first == "SID") {
sid->assign(i->second);
} else if (i->first == "LSID") {
lsid->assign(i->second);
} else if (i->first == "Auth") {
token->assign(i->second);
}
}
}
// static
void GaiaAuthenticator2::ParseClientLoginFailure(const std::string& data,
std::string* error,
std::string* error_url,
std::string* captcha_url,
std::string* captcha_token) {
using std::vector;
using std::pair;
using std::string;
vector<pair<string, string> > tokens;
base::SplitStringIntoKeyValuePairs(data, '=', '\n', &tokens);
for (vector<pair<string, string> >::iterator i = tokens.begin();
i != tokens.end(); ++i) {
if (i->first == kErrorParam) {
error->assign(i->second);
} else if (i->first == kErrorUrlParam) {
error_url->assign(i->second);
} else if (i->first == kCaptchaUrlParam) {
captcha_url->assign(i->second);
} else if (i->first == kCaptchaTokenParam) {
captcha_token->assign(i->second);
}
}
}
void GaiaAuthenticator2::StartClientLogin(
const std::string& username,
const std::string& password,
const char* const service,
const std::string& login_token,
const std::string& login_captcha,
HostedAccountsSetting allow_hosted_accounts) {
DCHECK(!fetch_pending_) << "Tried to fetch two things at once!";
// This class is thread agnostic, so be sure to call this only on the
// same thread each time.
VLOG(1) << "Starting new ClientLogin fetch for:" << username;
// Must outlive fetcher_.
request_body_ = MakeClientLoginBody(username,
password,
source_,
service,
login_token,
login_captcha,
allow_hosted_accounts);
fetcher_.reset(CreateGaiaFetcher(getter_,
request_body_,
client_login_gurl_,
this));
fetch_pending_ = true;
fetcher_->Start();
}
void GaiaAuthenticator2::StartIssueAuthToken(const std::string& sid,
const std::string& lsid,
const char* const service) {
DCHECK(!fetch_pending_) << "Tried to fetch two things at once!";
VLOG(1) << "Starting IssueAuthToken for: " << service;
requested_service_ = service;
request_body_ = MakeIssueAuthTokenBody(sid, lsid, service);
fetcher_.reset(CreateGaiaFetcher(getter_,
request_body_,
issue_auth_token_gurl_,
this));
fetch_pending_ = true;
fetcher_->Start();
}
void GaiaAuthenticator2::StartGetUserInfo(const std::string& lsid,
const std::string& info_key) {
DCHECK(!fetch_pending_) << "Tried to fetch two things at once!";
VLOG(1) << "Starting GetUserInfo for lsid=" << lsid;
request_body_ = MakeGetUserInfoBody(lsid);
fetcher_.reset(CreateGaiaFetcher(getter_,
request_body_,
get_user_info_gurl_,
this));
fetch_pending_ = true;
requested_info_key_ = info_key;
fetcher_->Start();
}
// static
GoogleServiceAuthError GaiaAuthenticator2::GenerateAuthError(
const std::string& data,
const URLRequestStatus& status) {
if (!status.is_success()) {
if (status.status() == URLRequestStatus::CANCELED) {
return GoogleServiceAuthError(GoogleServiceAuthError::REQUEST_CANCELED);
} else {
LOG(WARNING) << "Could not reach Google Accounts servers: errno "
<< status.os_error();
return GoogleServiceAuthError::FromConnectionError(status.os_error());
}
} else {
if (IsSecondFactorSuccess(data)) {
return GoogleServiceAuthError(GoogleServiceAuthError::TWO_FACTOR);
}
std::string error;
std::string url;
std::string captcha_url;
std::string captcha_token;
ParseClientLoginFailure(data, &error, &url, &captcha_url, &captcha_token);
LOG(WARNING) << "ClientLogin failed with " << error;
if (error == kCaptchaError) {
GURL image_url(kCaptchaUrlPrefix + captcha_url);
GURL unlock_url(url);
return GoogleServiceAuthError::FromCaptchaChallenge(
captcha_token, image_url, unlock_url);
}
if (error == kAccountDeletedError)
return GoogleServiceAuthError(GoogleServiceAuthError::ACCOUNT_DELETED);
if (error == kAccountDisabledError)
return GoogleServiceAuthError(GoogleServiceAuthError::ACCOUNT_DISABLED);
if (error == kServiceUnavailableError) {
return GoogleServiceAuthError(
GoogleServiceAuthError::SERVICE_UNAVAILABLE);
}
return GoogleServiceAuthError(
GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS);
}
NOTREACHED();
}
void GaiaAuthenticator2::OnClientLoginFetched(const std::string& data,
const URLRequestStatus& status,
int response_code) {
if (status.is_success() && response_code == RC_REQUEST_OK) {
VLOG(1) << "ClientLogin successful!";
std::string sid;
std::string lsid;
std::string token;
ParseClientLoginResponse(data, &sid, &lsid, &token);
consumer_->OnClientLoginSuccess(
GaiaAuthConsumer::ClientLoginResult(sid, lsid, token, data));
} else {
consumer_->OnClientLoginFailure(GenerateAuthError(data, status));
}
}
void GaiaAuthenticator2::OnIssueAuthTokenFetched(
const std::string& data,
const URLRequestStatus& status,
int response_code) {
if (status.is_success() && response_code == RC_REQUEST_OK) {
// Only the bare token is returned in the body of this Gaia call
// without any padding.
consumer_->OnIssueAuthTokenSuccess(requested_service_, data);
} else {
consumer_->OnIssueAuthTokenFailure(requested_service_,
GenerateAuthError(data, status));
}
}
void GaiaAuthenticator2::OnGetUserInfoFetched(
const std::string& data,
const URLRequestStatus& status,
int response_code) {
using std::vector;
using std::string;
using std::pair;
if (status.is_success() && response_code == RC_REQUEST_OK) {
vector<pair<string, string> > tokens;
base::SplitStringIntoKeyValuePairs(data, '=', '\n', &tokens);
for (vector<pair<string, string> >::iterator i = tokens.begin();
i != tokens.end(); ++i) {
if (i->first == requested_info_key_) {
consumer_->OnGetUserInfoSuccess(i->first, i->second);
return;
}
}
consumer_->OnGetUserInfoKeyNotFound(requested_info_key_);
} else {
consumer_->OnGetUserInfoFailure(GenerateAuthError(data, status));
}
}
void GaiaAuthenticator2::OnURLFetchComplete(const URLFetcher* source,
const GURL& url,
const URLRequestStatus& status,
int response_code,
const ResponseCookies& cookies,
const std::string& data) {
fetch_pending_ = false;
if (url == client_login_gurl_) {
OnClientLoginFetched(data, status, response_code);
} else if (url == issue_auth_token_gurl_) {
OnIssueAuthTokenFetched(data, status, response_code);
} else if (url == get_user_info_gurl_) {
OnGetUserInfoFetched(data, status, response_code);
} else {
NOTREACHED();
}
}
// static
bool GaiaAuthenticator2::IsSecondFactorSuccess(
const std::string& alleged_error) {
return alleged_error.find(kSecondFactor) !=
std::string::npos;
}
|