blob: 9d6195c578fea6862b5eeea0d401191c1db2abf7 [file] [log] [blame]
Ed Tanousba9f9a62017-10-11 16:40:35 -07001#pragma once
2
Ed Tanousba9f9a62017-10-11 16:40:35 -07003#include <nlohmann/json.hpp>
4#include <pam_authenticate.hpp>
5#include <webassets.hpp>
Borawski.Lukasz16238972018-01-17 15:36:53 +01006#include <random>
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +01007#include "session_storage_singleton.hpp"
Ed Tanousba9f9a62017-10-11 16:40:35 -07008#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
16namespace crow {
17
18namespace PersistentData {
Borawski.Lukasz9d8fd302018-01-05 14:56:09 +010019
Ed Tanousc963aa42017-10-27 16:00:19 -070020class 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, Kamil2b7981f2018-01-31 13:24:59 +010026 struct context {};
Ed Tanousc963aa42017-10-27 16:00:19 -070027
28 Middleware() { read_data(); }
29
30 ~Middleware() {
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +010031 if (PersistentData::session_store->needs_write()) {
Ed Tanousc963aa42017-10-27 16:00:19 -070032 write_data();
33 }
34 }
35
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +010036 void before_handle(crow::request& req, response& res, context& ctx) {}
Ed Tanousc963aa42017-10-27 16:00:19 -070037
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, Kamil2b7981f2018-01-31 13:24:59 +010051 PersistentData::session_store->auth_tokens =
52 data.value("sessions", decltype(session_store->auth_tokens)());
Ed Tanousc963aa42017-10-27 16:00:19 -070053 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, Kamil2b7981f2018-01-31 13:24:59 +010074 data["sessions"] = PersistentData::session_store->auth_tokens;
Ed Tanousc963aa42017-10-27 16:00:19 -070075 data["system_uuid"] = system_uuid;
76 data["revision"] = json_revision;
77 persistent_file << data;
78 }
79
Ed Tanousc963aa42017-10-27 16:00:19 -070080 std::string system_uuid;
Ed Tanousba9f9a62017-10-11 16:40:35 -070081};
82
83} // namespaec PersistentData
84} // namespace crow