OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 "chrome/browser/sync/engine/net/gaia_authenticator.h" | 5 #include "chrome/browser/sync/engine/net/gaia_authenticator.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <utility> | 8 #include <utility> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
12 #include "base/port.h" | 12 #include "base/port.h" |
13 #include "base/string_util.h" | 13 #include "base/string_split.h" |
14 #include "chrome/browser/sync/engine/all_status.h" | 14 #include "chrome/browser/sync/engine/all_status.h" |
15 #include "chrome/browser/sync/engine/net/http_return.h" | 15 #include "chrome/browser/sync/engine/net/http_return.h" |
16 #include "chrome/browser/sync/engine/net/url_translator.h" | 16 #include "chrome/browser/sync/engine/net/url_translator.h" |
17 #include "chrome/browser/sync/util/event_sys-inl.h" | 17 #include "chrome/browser/sync/util/event_sys-inl.h" |
18 #include "googleurl/src/gurl.h" | 18 #include "googleurl/src/gurl.h" |
19 | 19 |
20 using std::pair; | 20 using std::pair; |
21 using std::string; | 21 using std::string; |
22 using std::vector; | 22 using std::vector; |
23 | 23 |
24 // TODO(timsteele): Integrate the following two functions to string_util.h or | |
25 // somewhere that makes them unit-testable. | |
26 bool SplitStringIntoKeyValues(const string& line, | |
27 char key_value_delimiter, | |
28 string* key, vector<string>* values) { | |
29 key->clear(); | |
30 values->clear(); | |
31 | |
32 // find the key string | |
33 size_t end_key_pos = line.find_first_of(key_value_delimiter); | |
34 if (end_key_pos == string::npos) { | |
35 DLOG(INFO) << "cannot parse key from line: " << line; | |
36 return false; // no key | |
37 } | |
38 key->assign(line, 0, end_key_pos); | |
39 | |
40 // find the values string | |
41 string remains(line, end_key_pos, line.size() - end_key_pos); | |
42 size_t begin_values_pos = remains.find_first_not_of(key_value_delimiter); | |
43 if (begin_values_pos == string::npos) { | |
44 DLOG(INFO) << "cannot parse value from line: " << line; | |
45 return false; // no value | |
46 } | |
47 string values_string(remains, begin_values_pos, | |
48 remains.size() - begin_values_pos); | |
49 | |
50 // construct the values vector | |
51 values->push_back(values_string); | |
52 return true; | |
53 } | |
54 | |
55 bool SplitStringIntoKeyValuePairs(const string& line, | |
56 char key_value_delimiter, | |
57 char key_value_pair_delimiter, | |
58 vector<pair<string, string> >* kv_pairs) { | |
59 kv_pairs->clear(); | |
60 | |
61 vector<string> pairs; | |
62 SplitString(line, key_value_pair_delimiter, &pairs); | |
63 | |
64 bool success = true; | |
65 for (size_t i = 0; i < pairs.size(); ++i) { | |
66 string key; | |
67 vector<string> value; | |
68 if (!SplitStringIntoKeyValues(pairs[i], | |
69 key_value_delimiter, | |
70 &key, &value)) { | |
71 // Don't return here, to allow for keys without associated | |
72 // values; just record that our split failed. | |
73 success = false; | |
74 } | |
75 DCHECK_LE(value.size(), 1U); | |
76 kv_pairs->push_back(make_pair(key, value.empty()? "" : value[0])); | |
77 } | |
78 return success; | |
79 } | |
80 | |
81 namespace browser_sync { | 24 namespace browser_sync { |
82 | 25 |
83 static const char kGaiaV1IssueAuthTokenPath[] = "/accounts/IssueAuthToken"; | 26 static const char kGaiaV1IssueAuthTokenPath[] = "/accounts/IssueAuthToken"; |
84 | 27 |
85 static const char kGetUserInfoPath[] = "/accounts/GetUserInfo"; | 28 static const char kGetUserInfoPath[] = "/accounts/GetUserInfo"; |
86 | 29 |
87 // Sole constructor with initializers for all fields. | 30 // Sole constructor with initializers for all fields. |
88 GaiaAuthenticator::GaiaAuthenticator(const string& user_agent, | 31 GaiaAuthenticator::GaiaAuthenticator(const string& user_agent, |
89 const string& service_id, | 32 const string& service_id, |
90 const string& gaia_url) | 33 const string& gaia_url) |
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
281 } | 224 } |
282 | 225 |
283 // Check if we received a valid AuthToken; if not, ignore it. | 226 // Check if we received a valid AuthToken; if not, ignore it. |
284 if (RC_FORBIDDEN == server_response_code) { | 227 if (RC_FORBIDDEN == server_response_code) { |
285 // Server says we're not authenticated. | 228 // Server says we're not authenticated. |
286 ExtractAuthErrorFrom(message_text, results); | 229 ExtractAuthErrorFrom(message_text, results); |
287 return false; | 230 return false; |
288 } else if (RC_REQUEST_OK == server_response_code) { | 231 } else if (RC_REQUEST_OK == server_response_code) { |
289 typedef vector<pair<string, string> > Tokens; | 232 typedef vector<pair<string, string> > Tokens; |
290 Tokens tokens; | 233 Tokens tokens; |
291 SplitStringIntoKeyValuePairs(message_text, '=', '\n', &tokens); | 234 base::SplitStringIntoKeyValuePairs(message_text, '=', '\n', &tokens); |
292 for (Tokens::iterator i = tokens.begin(); i != tokens.end(); ++i) { | 235 for (Tokens::iterator i = tokens.begin(); i != tokens.end(); ++i) { |
293 if ("accountType" == i->first) { | 236 if ("accountType" == i->first) { |
294 // We never authenticate an email as a hosted account. | 237 // We never authenticate an email as a hosted account. |
295 DCHECK_EQ("GOOGLE", i->second); | 238 DCHECK_EQ("GOOGLE", i->second); |
296 results->signin = GMAIL_SIGNIN; | 239 results->signin = GMAIL_SIGNIN; |
297 } else if ("email" == i->first) { | 240 } else if ("email" == i->first) { |
298 results->primary_email = i->second; | 241 results->primary_email = i->second; |
299 } | 242 } |
300 } | 243 } |
301 return true; | 244 return true; |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
349 return true; | 292 return true; |
350 } | 293 } |
351 return false; | 294 return false; |
352 } | 295 } |
353 | 296 |
354 // Helper method that extracts tokens from a successful reply, and saves them | 297 // Helper method that extracts tokens from a successful reply, and saves them |
355 // in the right fields. | 298 // in the right fields. |
356 void GaiaAuthenticator::ExtractTokensFrom(const string& response, | 299 void GaiaAuthenticator::ExtractTokensFrom(const string& response, |
357 AuthResults* results) { | 300 AuthResults* results) { |
358 vector<pair<string, string> > tokens; | 301 vector<pair<string, string> > tokens; |
359 SplitStringIntoKeyValuePairs(response, '=', '\n', &tokens); | 302 base::SplitStringIntoKeyValuePairs(response, '=', '\n', &tokens); |
360 for (vector<pair<string, string> >::iterator i = tokens.begin(); | 303 for (vector<pair<string, string> >::iterator i = tokens.begin(); |
361 i != tokens.end(); ++i) { | 304 i != tokens.end(); ++i) { |
362 if (i->first == "SID") { | 305 if (i->first == "SID") { |
363 results->sid = i->second; | 306 results->sid = i->second; |
364 } else if (i->first == "LSID") { | 307 } else if (i->first == "LSID") { |
365 results->lsid = i->second; | 308 results->lsid = i->second; |
366 } else if (i->first == "Auth") { | 309 } else if (i->first == "Auth") { |
367 results->auth_token = i->second; | 310 results->auth_token = i->second; |
368 } | 311 } |
369 } | 312 } |
370 } | 313 } |
371 | 314 |
372 // Helper method that extracts tokens from a failure response, and saves them | 315 // Helper method that extracts tokens from a failure response, and saves them |
373 // in the right fields. | 316 // in the right fields. |
374 void GaiaAuthenticator::ExtractAuthErrorFrom(const string& response, | 317 void GaiaAuthenticator::ExtractAuthErrorFrom(const string& response, |
375 AuthResults* results) { | 318 AuthResults* results) { |
376 vector<pair<string, string> > tokens; | 319 vector<pair<string, string> > tokens; |
377 SplitStringIntoKeyValuePairs(response, '=', '\n', &tokens); | 320 base::SplitStringIntoKeyValuePairs(response, '=', '\n', &tokens); |
378 for (vector<pair<string, string> >::iterator i = tokens.begin(); | 321 for (vector<pair<string, string> >::iterator i = tokens.begin(); |
379 i != tokens.end(); ++i) { | 322 i != tokens.end(); ++i) { |
380 if (i->first == "Error") { | 323 if (i->first == "Error") { |
381 results->error_msg = i->second; | 324 results->error_msg = i->second; |
382 } else if (i->first == "Url") { | 325 } else if (i->first == "Url") { |
383 results->auth_error_url = i->second; | 326 results->auth_error_url = i->second; |
384 } else if (i->first == "CaptchaToken") { | 327 } else if (i->first == "CaptchaToken") { |
385 results->captcha_token = i->second; | 328 results->captcha_token = i->second; |
386 } else if (i->first == "CaptchaUrl") { | 329 } else if (i->first == "CaptchaUrl") { |
387 results->captcha_url = i->second; | 330 results->captcha_url = i->second; |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
442 const string& password, | 385 const string& password, |
443 SaveCredentials should_save_credentials, | 386 SaveCredentials should_save_credentials, |
444 SignIn try_first) { | 387 SignIn try_first) { |
445 DCHECK_EQ(MessageLoop::current(), message_loop_); | 388 DCHECK_EQ(MessageLoop::current(), message_loop_); |
446 const string empty; | 389 const string empty; |
447 return Authenticate(user_name, password, should_save_credentials, empty, | 390 return Authenticate(user_name, password, should_save_credentials, empty, |
448 empty, try_first); | 391 empty, try_first); |
449 } | 392 } |
450 | 393 |
451 } // namespace browser_sync | 394 } // namespace browser_sync |
OLD | NEW |