Remove adl_serializer uses for json
Several pieces of code seems to be using the adl_serializer from
nlohmann. This unfortunately has very undesirable behavior in some
cases, and makes a lot of things really difficult to track back to the
function that did the serialization, which has caused several bugs in
the past with incorrect types.
This patchset removes them, and opts for the inline version of the
nlohmann json serialization.
Tested:
Booted bmcweb, and logged in.
cat bmcweb_persistent_data.json showed persistent data written properly.
Logged into bmc through webui-vue
systemctl restart bmcweb
Then refreshed webui-vue, and didn't get logged out.
Change-Id: I92868629c54d08b37dd1d956f7c2e2a954f9b670
diff --git a/include/dbus_monitor.hpp b/include/dbus_monitor.hpp
index e177388..f770ec3 100644
--- a/include/dbus_monitor.hpp
+++ b/include/dbus_monitor.hpp
@@ -12,18 +12,6 @@
#include <variant>
-namespace nlohmann
-{
-template <typename... Args>
-struct adl_serializer<std::variant<Args...>>
-{
- static void to_json(json& j, const std::variant<Args...>& v)
- {
- std::visit([&](auto&& val) { j = val; }, v);
- }
-};
-} // namespace nlohmann
-
namespace crow
{
namespace dbus_monitor
diff --git a/include/persistent_data.hpp b/include/persistent_data.hpp
index 03457c7..9c86f65 100644
--- a/include/persistent_data.hpp
+++ b/include/persistent_data.hpp
@@ -168,13 +168,38 @@
std::filesystem::perms::owner_write |
std::filesystem::perms::group_read;
std::filesystem::permissions(filename, permission);
+ const auto& c = SessionStore::getInstance().getAuthMethodsConfig();
+ nlohmann::json data{{"auth_config",
+ {{"XToken", c.xtoken},
+ {"Cookie", c.cookie},
+ {"SessionToken", c.sessionToken},
+ {"BasicAuth", c.basic},
+ {"TLS", c.tls}}
- nlohmann::json data{
- {"sessions", SessionStore::getInstance().authTokens},
- {"auth_config", SessionStore::getInstance().getAuthMethodsConfig()},
- {"system_uuid", systemUuid},
- {"revision", jsonRevision},
- {"timeout", SessionStore::getInstance().getTimeoutInSeconds()}};
+ },
+ {"system_uuid", systemUuid},
+ {"revision", jsonRevision}};
+
+ nlohmann::json& sessions = data["sessions"];
+ sessions = nlohmann::json::array();
+ for (const auto& p : SessionStore::getInstance().authTokens)
+ {
+ if (p.second->persistence !=
+ persistent_data::PersistenceType::SINGLE_REQUEST)
+ {
+ sessions.push_back({
+ {"unique_id", p.second->uniqueId},
+ {"session_token", p.second->sessionToken},
+ {"username", p.second->username},
+ {"csrf_token", p.second->csrfToken},
+ {"timeout",
+ SessionStore::getInstance().getTimeoutInSeconds()},
+#ifdef BMCWEB_ENABLE_IBM_MANAGEMENT_CONSOLE
+ {"client_id", p.second->clientId},
+#endif
+ });
+ }
+ }
persistentFile << data;
}
diff --git a/include/sessions.hpp b/include/sessions.hpp
index 23de570..418f6f8 100644
--- a/include/sessions.hpp
+++ b/include/sessions.hpp
@@ -417,45 +417,3 @@
};
} // namespace persistent_data
-
-// to_json(...) definition for objects of UserSession type
-namespace nlohmann
-{
-template <>
-struct adl_serializer<std::shared_ptr<persistent_data::UserSession>>
-{
- static void to_json(nlohmann::json& j,
- const std::shared_ptr<persistent_data::UserSession>& p)
- {
- if (p->persistence != persistent_data::PersistenceType::SINGLE_REQUEST)
- {
-#ifdef BMCWEB_ENABLE_IBM_MANAGEMENT_CONSOLE
- j = nlohmann::json{
- {"unique_id", p->uniqueId}, {"session_token", p->sessionToken},
- {"username", p->username}, {"csrf_token", p->csrfToken},
- {"client_id", p->clientId}, { "client_ip", p->clientIp }};
-#else
- j = nlohmann::json{{"unique_id", p->uniqueId},
- {"session_token", p->sessionToken},
- {"username", p->username},
- {"csrf_token", p->csrfToken},
- {"client_ip", p->clientIp}};
-#endif
- }
- }
-};
-
-template <>
-struct adl_serializer<persistent_data::AuthConfigMethods>
-{
- static void to_json(nlohmann::json& j,
- const persistent_data::AuthConfigMethods& c)
- {
- j = nlohmann::json{{"XToken", c.xtoken},
- {"Cookie", c.cookie},
- {"SessionToken", c.sessionToken},
- {"BasicAuth", c.basic},
- {"TLS", c.tls}};
- }
-};
-} // namespace nlohmann