Refactor session key storage
Refactor session key storage to actually have an interface that isn't
map, and provide reasonable lookup functions, as well as update the
consumers of those functions.
This also implements session timeouts.
Change-Id: Ica46716805782cfbb7c4ee5569bc7e468c260bc3
diff --git a/include/redfish_v1.hpp b/include/redfish_v1.hpp
index 009ae61..4de009e 100644
--- a/include/redfish_v1.hpp
+++ b/include/redfish_v1.hpp
@@ -185,11 +185,12 @@
{"Members@odata.count", users.size()}};
nlohmann::json member_array = nlohmann::json::array();
int user_index = 0;
- for (auto& user : users) {
+ for (int user_index = 0; user_index < users.size();
+ user_index++) {
member_array.push_back(
{{"@odata.id",
"/redfish/v1/AccountService/Accounts/" +
- std::to_string(++user_index)}});
+ std::to_string(user_index)}});
}
res.json_value["Members"] = member_array;
}
@@ -244,8 +245,8 @@
CROW_ROUTE(app, "/redfish/v1/SessionService/Sessions/")
.methods("POST"_method, "GET"_method)([&](const crow::request& req,
crow::response& res) {
- auto& data_middleware =
- app.template get_middleware<PersistentData::Middleware>();
+ auto& session_store =
+ app.template get_middleware<PersistentData::Middleware>().sessions;
if (req.method == "POST"_method) {
// call with exceptions disabled
auto login_credentials =
@@ -278,7 +279,7 @@
res.end();
return;
}
- auto session = data_middleware.generate_user_session(username);
+ auto session = session_store.generate_user_session(username);
res.code = 200;
res.add_header("X-Auth-Token", session.session_token);
res.json_value = {
@@ -291,6 +292,8 @@
{"Description", "Manager User Session"},
{"UserName", username}};
} else { // assume get
+ std::vector<const std::string*> session_ids =
+ session_store.get_unique_ids();
res.json_value = {
{"@odata.context",
"/redfish/v1/$metadata#SessionCollection.SessionCollection"},
@@ -298,14 +301,14 @@
{"@odata.type", "#SessionCollection.SessionCollection"},
{"Name", "Session Collection"},
{"Description", "Session Collection"},
- {"Members@odata.count", data_middleware.auth_tokens.size()}
+ {"Members@odata.count", session_ids.size()}
};
nlohmann::json member_array = nlohmann::json::array();
- for (auto& session : data_middleware.auth_tokens) {
- member_array.push_back({{"@odata.id",
- "/redfish/v1/SessionService/Sessions/" +
- session.second.unique_id}});
+ for (auto session_uid : session_ids) {
+ member_array.push_back(
+ {{"@odata.id",
+ "/redfish/v1/SessionService/Sessions/" + *session_uid}});
}
res.json_value["Members"] = member_array;
}
@@ -313,37 +316,32 @@
});
CROW_ROUTE(app, "/redfish/v1/SessionService/Sessions/<str>/")
- .methods("GET"_method, "DELETE"_method)([&](const crow::request& req,
- crow::response& res,
- const std::string& session) {
- auto& data_middleware =
- app.template get_middleware<PersistentData::Middleware>();
+ .methods("GET"_method, "DELETE"_method)([&](
+ const crow::request& req, crow::response& res,
+ const std::string& session_id) {
+ auto& session_store =
+ app.template get_middleware<PersistentData::Middleware>().sessions;
// TODO(Ed) this is inefficient
- auto session_it = data_middleware.auth_tokens.begin();
- while (session_it != data_middleware.auth_tokens.end()) {
- if (session_it->second.unique_id == session) {
- break;
- }
- session_it++;
- }
+ auto session = session_store.get_session_by_uid(session_id);
- if (session_it == data_middleware.auth_tokens.end()) {
+ if (session == nullptr) {
res.code = 404;
res.end();
return;
}
if (req.method == "DELETE"_method) {
- data_middleware.auth_tokens.erase(session_it);
+ session_store.remove_session(session);
res.code = 200;
} else { // assume get
res.json_value = {
{"@odata.context", "/redfish/v1/$metadata#Session.Session"},
- {"@odata.id", "/redfish/v1/SessionService/Sessions/" + session},
+ {"@odata.id",
+ "/redfish/v1/SessionService/Sessions/" + session->unique_id},
{"@odata.type", "#Session.v1_0_3.Session"},
- {"Id", session_it->second.unique_id},
+ {"Id", session->unique_id},
{"Name", "User Session"},
{"Description", "Manager User Session"},
- {"UserName", session_it->second.username}};
+ {"UserName", session->username}};
}
res.end();
});
@@ -447,7 +445,6 @@
int port = std::stoi(port_str.c_str());
auto type_it = service_types.find(port);
if (type_it != service_types.end()) {
- type_it->second;
res.json_value[type_it->second] = {{"ProtocolEnabled", true},
{"Port", port}};
}