23 #include <curl/curl.h> 30 const boost::property_tree::ptree &cfg,
33 , bus_sub_(ctx,
zmqpp::socket_type::sub)
38 if (
config_.get<
bool>(
"module_config.want_ssl",
true))
39 flags |= CURL_GLOBAL_SSL;
40 if ((ret = curl_global_init(flags)) != 0)
42 throw std::runtime_error(
"Failed to initialize curl: return code: " +
45 bus_sub_.connect(
"inproc://zmq-bus-pub");
52 curl_global_cleanup();
66 WARN(
"Unexpected message content.");
69 msg >> src >> type >> card >> bits;
72 INFO(
"WS-Notifier cannot handle this type of credential yet.");
81 for (
auto &&itr :
config_.get_child(
"module_config.sources"))
83 auto name = itr.second.get<std::string>(
"");
87 for (
auto &&itr :
config_.get_child(
"module_config.targets"))
90 target.
url_ = itr.second.get<std::string>(
"url");
93 target.
verify_host_ = itr.second.get<
bool>(
"verify_host",
true);
94 target.
verify_peer_ = itr.second.get<
bool>(
"verify_peer",
true);
95 target.
CA_info_file_ = itr.second.get<std::string>(
"ca_file",
"");
97 INFO(
"WS-Notifier remote target: " 104 targets_.push_back(std::move(target));
109 const std::string &card_hex,
113 card.card_id(card_hex);
114 card.nb_bits(nb_bits);
118 auto curl = curl_easy_init();
121 if (!target.CA_info_file_.empty())
122 curl_easy_setopt(curl, CURLOPT_CAINFO, target.CA_info_file_.c_str());
123 if (!target.verify_host_)
124 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
125 if (!target.verify_peer_)
126 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
128 curl_easy_cleanup(curl);
132 ERROR(
"Cannot initialize curl_easy.");
142 static size_t write_callback(
char * ,
size_t size,
size_t nmemb,
149 void *curl,
const std::string &auth_source,
const Cred::RFIDCard &card,
154 std::string post_fields =
155 fmt::format(
"card_id={}&auth_source={}&card_id_raw={}", card.to_int(),
156 auth_source, card.to_raw_int());
158 curl_easy_setopt(curl, CURLOPT_URL, target.url_.c_str());
159 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_fields.c_str());
162 curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT_MS, target.connect_timeout_);
163 curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, target.request_timeout_);
165 curl_easy_setopt(curl, CURLOPT_WRITEDATA,
nullptr);
166 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &write_callback);
168 auto res = curl_easy_perform(curl);
171 WARN(
"curl_easy_perform() failed: " << curl_easy_strerror(res));
void send_to_target(void *curl, const std::string &auth_source, const Cred::RFIDCard &card, const TargetInfo &target) noexcept
This define message formatting for data source SIMPLE_WIEGAND.
std::vector< TargetInfo > targets_
This is the header file for a generated source file, GitSHA1.cpp.
void process_config()
Process the configuration file.
void send_card_info_to_remote(const std::string &auth_source, const std::string &card, int nb_bits)
Send an HTTP request to the remote webservice to let it know a card was read.
bool verify_peer_
If SSL is enabled, do we perform certificate validation ?
std::string green(const T &in)
std::string CA_info_file_
Path to a CA bundle file.
All modules that provides features to Leosac shall be in this namespace.
Base class for module implementation.
void handle_msg_bus()
Process a message that was read on the bus.
zmqpp::reactor reactor_
The reactor object we poll() on in the main loop.
WebServiceNotifier(zmqpp::context &ctx, zmqpp::socket *pipe, const boost::property_tree::ptree &cfg, CoreUtilsPtr utils)
std::shared_ptr< CoreUtils > CoreUtilsPtr
Some information for each webservice target.
bool verify_host_
If SSL is enabled, do we perform certificate hostname validation ?
boost::property_tree::ptree config_
The configuration tree passed to the start_module function.
zmqpp::socket bus_sub_
Read internal message bus.
std::string format(const std::string &escape_code, const T &in)
Return a string containing the escape code, a string representation of T and the clear escape string...