summaryrefslogtreecommitdiffstats
path: root/components/auto_login_parser/auto_login_parser.cc
blob: 38fc633c8183e974e2ceb75a76498eff808a49bd (plain)
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
// Copyright (c) 2013 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 "components/auto_login_parser/auto_login_parser.h"

#include <utility>
#include <vector>

#include "base/logging.h"
#include "base/strings/string_split.h"
#include "net/base/escape.h"
#include "net/url_request/url_request.h"

namespace auto_login_parser {

namespace {

const char kHeaderName[] = "X-Auto-Login";

bool MatchRealm(const std::string& realm, RealmRestriction restriction) {
  switch (restriction) {
    case ONLY_GOOGLE_COM:
      return realm == "com.google";
    case ALLOW_ANY_REALM:
      return true;
    default:
      NOTREACHED();
      return false;
  }
}

}  // namespace

HeaderData::HeaderData() {}
HeaderData::~HeaderData() {}

bool ParseHeader(const std::string& header,
                 RealmRestriction realm_restriction,
                 HeaderData* header_data) {
  // TODO(pliard): Investigate/fix potential internationalization issue. It
  // seems that "account" from the x-auto-login header might contain non-ASCII
  // characters.
  if (header.empty())
    return false;

  base::StringPairs pairs;
  if (!base::SplitStringIntoKeyValuePairs(header, '=', '&', &pairs))
    return false;

  // Parse the information from the |header| string.
  HeaderData local_params;
  for (base::StringPairs::const_iterator it = pairs.begin(); it != pairs.end();
       ++it) {
    const std::string& key = it->first;
    const std::string& value = it->second;
    std::string unescaped_value(
        net::UnescapeURLComponent(value, net::UnescapeRule::URL_SPECIAL_CHARS));
    if (key == "realm") {
      if (!MatchRealm(unescaped_value, realm_restriction))
        return false;
      local_params.realm = unescaped_value;
    } else if (key == "account") {
      local_params.account = unescaped_value;
    } else if (key == "args") {
      local_params.args = unescaped_value;
    }
  }
  if (local_params.realm.empty() || local_params.args.empty())
    return false;

  *header_data = local_params;
  return true;
}

bool ParserHeaderInResponse(net::URLRequest* request,
                            RealmRestriction realm_restriction,
                            HeaderData* header_data) {
  std::string header_string;
  request->GetResponseHeaderByName(kHeaderName, &header_string);
  return ParseHeader(header_string, realm_restriction, header_data);
}

}  // namespace auto_login_parser