File: login_tuple.cc

package info (click to toggle)
weakforced 3.0.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,040 kB
  • sloc: cpp: 20,397; python: 2,002; sh: 700; makefile: 432
file content (217 lines) | stat: -rw-r--r-- 7,692 bytes parent folder | download
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
/*
 * This file is part of PowerDNS or weakforced.
 * Copyright -- PowerDNS.COM B.V. and its contributors
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of version 3 of the GNU General Public License as
 * published by the Free Software Foundation.
 *
 * In addition, for the avoidance of any doubt, permission is granted to
 * link this program with OpenSSL and to (re)distribute the binaries
 * produced as the result of such linking.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "login_tuple.hh"
#include "json11.hpp"

static DeviceCache dcache;

json11::Json LoginTuple::to_json() const
{
  using namespace json11;
  Json::object jattrs;
  Json::object jattrs_dev;

  for (auto& i : attrs_mv) {
    jattrs.insert(make_pair(i.first, Json(i.second)));
  }
  for (auto& i : attrs) {
    jattrs.insert(make_pair(i.first, Json(i.second)));
  }
  for (auto& i : device_attrs) {
    jattrs_dev.insert(make_pair(i.first, Json(i.second)));
  }

  return Json::object{
    {"login", login},
    {"success", success},
    {"t", (double)t}, 
    {"pwhash", pwhash},
    {"remote", remote.toString()},
    {"device_id", device_id},
    {"device_attrs", jattrs_dev},
    {"protocol", protocol},
    {"tls", tls},
    {"attrs", jattrs},
    {"policy_reject", policy_reject},
    {"fail_type", fail_type},
    {"session_id", session_id},
  };
}

std::string LoginTuple::serialize() const
{
  json11::Json msg=to_json();
  return msg.dump();
}

void LoginTuple::from_json(const json11::Json& msg, const std::shared_ptr<UserAgentParser> uap)
{
  login=msg["login"].string_value();
  pwhash=msg["pwhash"].string_value();
  t=msg["t"].number_value();
  success=msg["success"].bool_value();
  if (msg["remote"].is_string()) {
    remote=ComboAddress(msg["remote"].string_value());
    if (remote.isMappedIPv4()) {
      remote = remote.mapToIPv4();
    }
  }
  setLtAttrs(msg);
  setDeviceAttrs(msg, uap);
  device_id=msg["device_id"].string_value();
  protocol=msg["protocol"].string_value();
  tls=msg["tls"].bool_value();
  policy_reject=msg["policy_reject"].bool_value();
  fail_type=msg["fail_type"].string_value();
  session_id=msg["session_id"].string_value();
}

void LoginTuple::unserialize(const std::string& str) 
{
  string err;
  json11::Json msg=json11::Json::parse(str, err);
  from_json(msg);
}

void LoginTuple::setDeviceAttrs(const json11::Json& msg, const std::shared_ptr<UserAgentParser> uap)
{
  std::map<std::string, std::string> cached_attrs;
  json11::Json jattrs = msg["device_attrs"];
  if (jattrs.is_object()) {
    auto attrs_obj = jattrs.object_items();
    for (auto it=attrs_obj.begin(); it!=attrs_obj.end(); ++it) {
      string attr_name = it->first;
      if (it->second.is_string()) {
	device_attrs.insert(std::make_pair(attr_name, it->second.string_value()));
      }
    }
  }
  else if (dcache.readFromCache(msg["device_id"].string_value(), cached_attrs)) {
    device_attrs = std::move(cached_attrs);
  }
  else if (uap != nullptr) {  // client didn't supply, we will parse device_id ourselves
    std::string my_device_id=msg["device_id"].string_value();
    std::string my_protocol=msg["protocol"].string_value();

    // parse using the uap-cpp Parser
    if ((my_protocol.compare("http") == 0) ||
        (my_protocol.compare("https") == 0)) {
      UserAgent ua = uap->parse(my_device_id);
      device_attrs.insert(std::make_pair("device.family", ua.device.family));
      device_attrs.insert(std::make_pair("device.model", ua.device.model));
      device_attrs.insert(std::make_pair("device.brand", ua.device.brand));
      device_attrs.insert(std::make_pair("os.family", ua.os.family));
      device_attrs.insert(std::make_pair("os.major", ua.os.major));
      device_attrs.insert(std::make_pair("os.minor", ua.os.minor));
      device_attrs.insert(std::make_pair("browser.family", ua.browser.family));
      device_attrs.insert(std::make_pair("browser.major", ua.browser.major));
      device_attrs.insert(std::make_pair("browser.minor", ua.browser.minor));
    }
    else if ((my_protocol.compare("imap") == 0) ||
             (my_protocol.compare("imaps") == 0)) {
      IMAPClientIDParser imap_parser;
      IMAPClientID ic = imap_parser.parse(my_device_id);
      device_attrs.insert(std::make_pair("imapc.family", ic.imapc.family));
      device_attrs.insert(std::make_pair("imapc.major", ic.imapc.major));
      device_attrs.insert(std::make_pair("imapc.minor", ic.imapc.minor));
      device_attrs.insert(std::make_pair("os.family", ic.os.family));
      device_attrs.insert(std::make_pair("os.major", ic.os.major));
      device_attrs.insert(std::make_pair("os.minor", ic.os.minor));
    }
    else if (my_protocol.compare("mobileapi") == 0) {
      OXMobileAppDeviceParser oxmad_parser;
      OXMobileAppDevice oxmad = oxmad_parser.parse(my_device_id);
      device_attrs.insert(std::make_pair("os.family", oxmad.os.family));
      device_attrs.insert(std::make_pair("os.major", oxmad.os.major));
      device_attrs.insert(std::make_pair("os.minor", oxmad.os.minor));
      device_attrs.insert(std::make_pair("app.name", oxmad.app.name));
      device_attrs.insert(std::make_pair("app.brand", oxmad.app.brand));
      device_attrs.insert(std::make_pair("app.major", oxmad.app.major));
      device_attrs.insert(std::make_pair("app.minor", oxmad.app.minor));
      device_attrs.insert(std::make_pair("device.family", oxmad.device.family));
    }
    dcache.addToCache(my_device_id, device_attrs);
  }
}

void LoginTuple::setLtAttrs(const json11::Json& msg)
{
  json11::Json jattrs = msg["attrs"];
  if (jattrs.is_object()) {
    auto attrs_obj = jattrs.object_items();
    for (auto it = attrs_obj.begin(); it != attrs_obj.end(); ++it) {
      string attr_name = it->first;
      if (it->second.is_string()) {
        attrs.insert(std::make_pair(attr_name, it->second.string_value()));
      }
      else if (it->second.is_array()) {
        auto av_list = it->second.array_items();
        std::vector <std::string> myvec;
        for (auto avit = av_list.begin(); avit != av_list.end(); ++avit) {
          myvec.push_back(avit->string_value());
        }
        attrs_mv.insert(std::make_pair(attr_name, myvec));
      }
    }
  }
}

std::string LtAttrsToString(const LoginTuple& lt)
{
  std::ostringstream os;
  os << "attrs={";
  for (auto i= lt.attrs.begin(); i!=lt.attrs.end(); ++i) {
    os << i->first << "="<< "\"" << i->second << "\"";
    if (i != --(lt.attrs.end()))
      os << ", ";
  }
  for (auto i = lt.attrs_mv.begin(); i!=lt.attrs_mv.end(); ++i) {
    if (i == lt.attrs_mv.begin())
      os << ", ";
    os << i->first << "=[";
    std::vector<std::string> vec = i->second;
    for (auto j = vec.begin(); j!=vec.end(); ++j) {
      os << "\"" << *j << "\"";
      if (j != --(vec.end()))
	os << ", ";
    }
    os << "]";
    if (i != --(lt.attrs_mv.end()))
      os << ", ";
  }
  os << "} ";
  return os.str();
}

std::string DeviceAttrsToString(const LoginTuple& lt)
{
  std::ostringstream os;
  os << "device_attrs={";
  for (auto i= lt.device_attrs.begin(); i!=lt.device_attrs.end(); ++i) {
    os << i->first << "="<< "\"" << i->second << "\"";
    if (i != --(lt.device_attrs.end()))
      os << ", ";
  }
  os << "} ";
  return os.str();
}