void registry_inventory(Mongoose::Request &request, Mongoose::StreamResponse &response) { if (!is_loggedin(request, response, password)) return; Plugin::RegistryRequestMessage rrm; nscapi::protobuf::functions::create_simple_header(rrm.mutable_header()); Plugin::RegistryRequestMessage::Request *payload = rrm.add_payload(); if (request.get("all", "true") == "true") payload->mutable_inventory()->set_fetch_all(true); std::string type = request.get("type", "query"); if (type == "query") payload->mutable_inventory()->add_type(Plugin::Registry_ItemType_QUERY); else if (type == "command") payload->mutable_inventory()->add_type(Plugin::Registry_ItemType_COMMAND); else if (type == "module") payload->mutable_inventory()->add_type(Plugin::Registry_ItemType_MODULE); else if (type == "query-alias") payload->mutable_inventory()->add_type(Plugin::Registry_ItemType_QUERY_ALIAS); else if (type == "all") payload->mutable_inventory()->add_type(Plugin::Registry_ItemType_ALL); else { response.setCode(HTTP_SERVER_ERROR); response << "500 Invalid type. Possible types are: query, command, plugin, query-alias, all"; return; } std::string pb_response, json_response; core->registry_query(rrm.SerializeAsString(), pb_response); core->protobuf_to_json("RegistryResponseMessage", pb_response, json_response); response << json_response; }
void auth_token(Mongoose::Request &request, Mongoose::StreamResponse &response) { if (password.empty() || password != request.get("password")) { response.setCode(HTTP_FORBIDDEN); response << "403 Invalid password"; } else { std::string token = tokens.generate(); response.setHeader("__TOKEN", token); response << "{ \"status\" : \"ok\", \"auth token\": \"" << token << "\" }"; } }
void settings_controller::get_key(Mongoose::Request &request, boost::smatch &what, Mongoose::StreamResponse &response) { if (!session->is_loggedin("settings", request, response)) return; if (!validate_arguments(2, what, response)) { return; } std::string path = what.str(1); std::string key = what.str(2); if (!session->can("settings.get", request, response)) return; Plugin::SettingsRequestMessage rm; Plugin::SettingsRequestMessage::Request *payload = rm.add_payload(); payload->mutable_query()->mutable_node()->set_path(path); payload->mutable_query()->mutable_node()->set_key(key); payload->mutable_query()->set_recursive(false); payload->set_plugin_id(plugin_id); std::string str_response; core->settings_query(rm.SerializeAsString(), str_response); Plugin::SettingsResponseMessage pb_response; pb_response.ParseFromString(str_response); json_spirit::Object node; BOOST_FOREACH(const Plugin::SettingsResponseMessage::Response r, pb_response.payload()) { if (!r.has_query()) { response.setCode(HTTP_NOT_FOUND); response.append("Key not found: " + path + "/" + key); return; } const Plugin::SettingsResponseMessage::Response::Query &i = r.query(); node["path"] = i.node().path(); node["key"] = i.node().key(); if (i.value().has_string_data()) { node["value"] = i.value().string_data(); } else if (i.value().has_int_data()) { node["value"] = i.value().int_data(); } else if (i.value().has_bool_data()) { node["value"] = i.value().bool_data(); } } response.append(json_spirit::write(node)); }
bool is_loggedin(Mongoose::Request &request, Mongoose::StreamResponse &response, std::string gpassword, bool respond = true) { std::string token = request.readHeader("TOKEN"); if (token.empty()) token = request.get("__TOKEN", ""); bool auth = false; if (token.empty()) { std::string password = request.readHeader("password"); if (password.empty()) password = request.get("password", ""); auth = !password.empty() && password == gpassword; } else { auth = tokens.validate(token); } if (!auth) { if (respond) { response.setCode(HTTP_FORBIDDEN); response << "403 Please login first"; } return false; } return true; }
void settings_controller::get_section(Mongoose::Request &request, boost::smatch &what, Mongoose::StreamResponse &response) { if (!session->is_loggedin("settings.list", request, response)) return; if (!validate_arguments(1, what, response)) { return; } std::string path = what.str(1); Plugin::SettingsRequestMessage rm; Plugin::SettingsRequestMessage::Request *payload = rm.add_payload(); payload->mutable_query()->mutable_node()->set_path(path); payload->mutable_query()->set_recursive(false); payload->set_plugin_id(plugin_id); payload = rm.add_payload(); payload->mutable_query()->mutable_node()->set_path(path); payload->mutable_query()->set_recursive(true); payload->set_plugin_id(plugin_id); std::string str_response; core->settings_query(rm.SerializeAsString(), str_response); Plugin::SettingsResponseMessage pb_response; pb_response.ParseFromString(str_response); json_spirit::Object node; node["path"] = path; if (pb_response.payload_size() != 2) { response.setCode(HTTP_SERVER_ERROR); response.append("Failed to fetch keys"); return; } const Plugin::SettingsResponseMessage::Response rKeys = pb_response.payload(0); if (!rKeys.has_query()) { response.setCode(HTTP_NOT_FOUND); response.append("Key not found: " + path); return; } json_spirit::Array keys; BOOST_FOREACH(const std::string &s, rKeys.query().value().list_data()) { keys.push_back(s); } node["keys"] = keys; const Plugin::SettingsResponseMessage::Response rPath = pb_response.payload(1); if (!rPath.has_query()) { response.setCode(HTTP_NOT_FOUND); response.append("Key not found: " + path); return; } json_spirit::Array paths; BOOST_FOREACH(const std::string &s, rPath.query().value().list_data()) { paths.push_back(s); } node["paths"] = paths; response.append(json_spirit::write(node)); }
void redirect_index(Mongoose::Request&, Mongoose::StreamResponse &response) { response.setCode(302); response.setHeader("Location", "/index.html"); }