Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 3 | #include <nlohmann/json.hpp> |
| 4 | #include <pam_authenticate.hpp> |
| 5 | #include <webassets.hpp> |
Borawski.Lukasz | 1623897 | 2018-01-17 15:36:53 +0100 | [diff] [blame] | 6 | #include <random> |
Kowalski, Kamil | 2b7981f | 2018-01-31 13:24:59 +0100 | [diff] [blame] | 7 | #include "session_storage_singleton.hpp" |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 8 | #include <crow/app.h> |
| 9 | #include <crow/http_request.h> |
| 10 | #include <crow/http_response.h> |
| 11 | #include <boost/container/flat_map.hpp> |
| 12 | #include <boost/uuid/uuid.hpp> |
| 13 | #include <boost/uuid/uuid_generators.hpp> |
| 14 | #include <boost/uuid/uuid_io.hpp> |
| 15 | |
| 16 | namespace crow { |
| 17 | |
| 18 | namespace PersistentData { |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 19 | |
Ed Tanous | c963aa4 | 2017-10-27 16:00:19 -0700 | [diff] [blame] | 20 | class Middleware { |
| 21 | // todo(ed) should read this from a fixed location somewhere, not CWD |
| 22 | static constexpr const char* filename = "bmcweb_persistent_data.json"; |
| 23 | int json_revision = 1; |
| 24 | |
| 25 | public: |
Kowalski, Kamil | 2b7981f | 2018-01-31 13:24:59 +0100 | [diff] [blame] | 26 | struct context {}; |
Ed Tanous | c963aa4 | 2017-10-27 16:00:19 -0700 | [diff] [blame] | 27 | |
| 28 | Middleware() { read_data(); } |
| 29 | |
| 30 | ~Middleware() { |
Kowalski, Kamil | 2b7981f | 2018-01-31 13:24:59 +0100 | [diff] [blame] | 31 | if (PersistentData::session_store->needs_write()) { |
Ed Tanous | c963aa4 | 2017-10-27 16:00:19 -0700 | [diff] [blame] | 32 | write_data(); |
| 33 | } |
| 34 | } |
| 35 | |
Kowalski, Kamil | 2b7981f | 2018-01-31 13:24:59 +0100 | [diff] [blame] | 36 | void before_handle(crow::request& req, response& res, context& ctx) {} |
Ed Tanous | c963aa4 | 2017-10-27 16:00:19 -0700 | [diff] [blame] | 37 | |
| 38 | void after_handle(request& req, response& res, context& ctx) {} |
| 39 | |
| 40 | // TODO(ed) this should really use protobuf, or some other serialization |
| 41 | // library, but adding another dependency is somewhat outside the scope of |
| 42 | // this application for the moment |
| 43 | void read_data() { |
| 44 | std::ifstream persistent_file(filename); |
| 45 | int file_revision = 0; |
| 46 | if (persistent_file.is_open()) { |
| 47 | // call with exceptions disabled |
| 48 | auto data = nlohmann::json::parse(persistent_file, nullptr, false); |
| 49 | if (!data.is_discarded()) { |
| 50 | file_revision = data.value("revision", 0); |
Kowalski, Kamil | 2b7981f | 2018-01-31 13:24:59 +0100 | [diff] [blame] | 51 | PersistentData::session_store->auth_tokens = |
| 52 | data.value("sessions", decltype(session_store->auth_tokens)()); |
Ed Tanous | c963aa4 | 2017-10-27 16:00:19 -0700 | [diff] [blame] | 53 | system_uuid = data.value("system_uuid", ""); |
| 54 | } |
| 55 | } |
| 56 | bool need_write = false; |
| 57 | |
| 58 | if (system_uuid.empty()) { |
| 59 | system_uuid = boost::uuids::to_string(boost::uuids::random_generator()()); |
| 60 | need_write = true; |
| 61 | } |
| 62 | if (file_revision < json_revision) { |
| 63 | need_write = true; |
| 64 | } |
| 65 | // write revision changes or system uuid changes immediately |
| 66 | if (need_write) { |
| 67 | write_data(); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | void write_data() { |
| 72 | std::ofstream persistent_file(filename); |
| 73 | nlohmann::json data; |
Kowalski, Kamil | 2b7981f | 2018-01-31 13:24:59 +0100 | [diff] [blame] | 74 | data["sessions"] = PersistentData::session_store->auth_tokens; |
Ed Tanous | c963aa4 | 2017-10-27 16:00:19 -0700 | [diff] [blame] | 75 | data["system_uuid"] = system_uuid; |
| 76 | data["revision"] = json_revision; |
| 77 | persistent_file << data; |
| 78 | } |
| 79 | |
Ed Tanous | c963aa4 | 2017-10-27 16:00:19 -0700 | [diff] [blame] | 80 | std::string system_uuid; |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 81 | }; |
| 82 | |
| 83 | } // namespaec PersistentData |
| 84 | } // namespace crow |