auto_login_parser.cc 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // Copyright (c) 2013 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "android_webview/browser/renderer_host/auto_login_parser.h"
  5. #include <utility>
  6. #include <vector>
  7. #include "base/notreached.h"
  8. #include "base/strings/escape.h"
  9. #include "base/strings/string_split.h"
  10. namespace android_webview {
  11. namespace {
  12. bool MatchRealm(const std::string& realm, RealmRestriction restriction) {
  13. switch (restriction) {
  14. case ONLY_GOOGLE_COM:
  15. return realm == "com.google";
  16. case ALLOW_ANY_REALM:
  17. return true;
  18. default:
  19. NOTREACHED();
  20. return false;
  21. }
  22. }
  23. } // namespace
  24. HeaderData::HeaderData() {}
  25. HeaderData::~HeaderData() {}
  26. bool ParseHeader(const std::string& header,
  27. RealmRestriction realm_restriction,
  28. HeaderData* header_data) {
  29. // TODO(pliard): Investigate/fix potential internationalization issue. It
  30. // seems that "account" from the x-auto-login header might contain non-ASCII
  31. // characters.
  32. if (header.empty())
  33. return false;
  34. base::StringPairs pairs;
  35. if (!base::SplitStringIntoKeyValuePairs(header, '=', '&', &pairs))
  36. return false;
  37. // Parse the information from the |header| string.
  38. HeaderData local_params;
  39. for (base::StringPairs::const_iterator it = pairs.begin(); it != pairs.end();
  40. ++it) {
  41. const std::string& key = it->first;
  42. const std::string& value = it->second;
  43. std::string unescaped_value = base::UnescapeBinaryURLComponent(value);
  44. if (key == "realm") {
  45. if (!MatchRealm(unescaped_value, realm_restriction))
  46. return false;
  47. local_params.realm = unescaped_value;
  48. } else if (key == "account") {
  49. local_params.account = unescaped_value;
  50. } else if (key == "args") {
  51. local_params.args = unescaped_value;
  52. }
  53. }
  54. if (local_params.realm.empty() || local_params.args.empty())
  55. return false;
  56. *header_data = local_params;
  57. return true;
  58. }
  59. } // namespace android_webview