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 <crow/app.h> |
| 4 | #include <crow/http_request.h> |
| 5 | #include <crow/http_response.h> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 6 | |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 7 | #include <boost/container/flat_map.hpp> |
| 8 | #include <boost/uuid/uuid.hpp> |
| 9 | #include <boost/uuid/uuid_generators.hpp> |
| 10 | #include <boost/uuid/uuid_io.hpp> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 11 | #include <nlohmann/json.hpp> |
| 12 | #include <pam_authenticate.hpp> |
| 13 | #include <random> |
| 14 | #include <sessions.hpp> |
| 15 | #include <webassets.hpp> |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 16 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 17 | namespace crow |
| 18 | { |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 19 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 20 | namespace persistent_data |
| 21 | { |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 22 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 23 | class Middleware |
| 24 | { |
| 25 | // todo(ed) should read this from a fixed location somewhere, not CWD |
| 26 | static constexpr const char* filename = "bmcweb_persistent_data.json"; |
| 27 | int jsonRevision = 1; |
Ed Tanous | c963aa4 | 2017-10-27 16:00:19 -0700 | [diff] [blame] | 28 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 29 | public: |
| 30 | struct Context |
| 31 | { |
| 32 | }; |
Ed Tanous | c963aa4 | 2017-10-27 16:00:19 -0700 | [diff] [blame] | 33 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 34 | Middleware() |
| 35 | { |
| 36 | readData(); |
Ed Tanous | c963aa4 | 2017-10-27 16:00:19 -0700 | [diff] [blame] | 37 | } |
Ed Tanous | c963aa4 | 2017-10-27 16:00:19 -0700 | [diff] [blame] | 38 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 39 | ~Middleware() |
| 40 | { |
| 41 | if (persistent_data::SessionStore::getInstance().needsWrite()) |
| 42 | { |
| 43 | writeData(); |
Kowalski, Kamil | 5cef0f7 | 2018-02-15 15:26:51 +0100 | [diff] [blame] | 44 | } |
Ed Tanous | c963aa4 | 2017-10-27 16:00:19 -0700 | [diff] [blame] | 45 | } |
Ed Tanous | c963aa4 | 2017-10-27 16:00:19 -0700 | [diff] [blame] | 46 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 47 | void beforeHandle(crow::Request& req, Response& res, Context& ctx) |
| 48 | { |
Ed Tanous | c963aa4 | 2017-10-27 16:00:19 -0700 | [diff] [blame] | 49 | } |
Ed Tanous | c963aa4 | 2017-10-27 16:00:19 -0700 | [diff] [blame] | 50 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 51 | void afterHandle(Request& req, Response& res, Context& ctx) |
| 52 | { |
| 53 | } |
Ed Tanous | c963aa4 | 2017-10-27 16:00:19 -0700 | [diff] [blame] | 54 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 55 | // TODO(ed) this should really use protobuf, or some other serialization |
| 56 | // library, but adding another dependency is somewhat outside the scope of |
| 57 | // this application for the moment |
| 58 | void readData() |
| 59 | { |
| 60 | std::ifstream persistentFile(filename); |
| 61 | int fileRevision = 0; |
| 62 | if (persistentFile.is_open()) |
| 63 | { |
| 64 | // call with exceptions disabled |
| 65 | auto data = nlohmann::json::parse(persistentFile, nullptr, false); |
| 66 | if (data.is_discarded()) |
| 67 | { |
| 68 | BMCWEB_LOG_ERROR |
| 69 | << "Error parsing persistent data in json file."; |
| 70 | } |
| 71 | else |
| 72 | { |
| 73 | for (const auto& item : data.items()) |
| 74 | { |
| 75 | if (item.key() == "revision") |
| 76 | { |
| 77 | fileRevision = 0; |
| 78 | |
| 79 | const uint64_t* uintPtr = |
| 80 | item.value().get_ptr<const uint64_t*>(); |
| 81 | if (uintPtr == nullptr) |
| 82 | { |
| 83 | BMCWEB_LOG_ERROR << "Failed to read revision flag"; |
| 84 | } |
| 85 | else |
| 86 | { |
| 87 | fileRevision = *uintPtr; |
| 88 | } |
| 89 | } |
| 90 | else if (item.key() == "system_uuid") |
| 91 | { |
| 92 | const std::string* jSystemUuid = |
| 93 | item.value().get_ptr<const std::string*>(); |
| 94 | if (jSystemUuid != nullptr) |
| 95 | { |
| 96 | systemUuid = *jSystemUuid; |
| 97 | } |
| 98 | } |
| 99 | else if (item.key() == "sessions") |
| 100 | { |
| 101 | for (const auto& elem : item.value()) |
| 102 | { |
| 103 | std::shared_ptr<UserSession> newSession = |
| 104 | UserSession::fromJson(elem); |
| 105 | |
| 106 | if (newSession == nullptr) |
| 107 | { |
| 108 | BMCWEB_LOG_ERROR << "Problem reading session " |
| 109 | "from persistent store"; |
| 110 | continue; |
| 111 | } |
| 112 | |
| 113 | BMCWEB_LOG_DEBUG |
| 114 | << "Restored session: " << newSession->csrfToken |
| 115 | << " " << newSession->uniqueId << " " |
| 116 | << newSession->sessionToken; |
| 117 | SessionStore::getInstance().authTokens.emplace( |
| 118 | newSession->sessionToken, newSession); |
| 119 | } |
| 120 | } |
| 121 | else |
| 122 | { |
| 123 | // Do nothing in the case of extra fields. We may have |
| 124 | // cases where fields are added in the future, and we |
| 125 | // want to at least attempt to gracefully support |
| 126 | // downgrades in that case, even if we don't officially |
| 127 | // support it |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | bool needWrite = false; |
| 133 | |
| 134 | if (systemUuid.empty()) |
| 135 | { |
| 136 | systemUuid = |
| 137 | boost::uuids::to_string(boost::uuids::random_generator()()); |
| 138 | needWrite = true; |
| 139 | } |
| 140 | if (fileRevision < jsonRevision) |
| 141 | { |
| 142 | needWrite = true; |
| 143 | } |
| 144 | // write revision changes or system uuid changes immediately |
| 145 | if (needWrite) |
| 146 | { |
| 147 | writeData(); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | void writeData() |
| 152 | { |
| 153 | std::ofstream persistentFile(filename); |
| 154 | nlohmann::json data{ |
| 155 | {"sessions", SessionStore::getInstance().authTokens}, |
| 156 | {"system_uuid", systemUuid}, |
| 157 | {"revision", jsonRevision}}; |
| 158 | persistentFile << data; |
| 159 | } |
| 160 | |
| 161 | std::string systemUuid{""}; |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 162 | }; |
| 163 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 164 | } // namespace persistent_data |
| 165 | } // namespace crow |