xref: /openbmc/bmcweb/features/redfish/lib/redfish_sessions.hpp (revision f00032db72e7ee88946bb4839b5d5311a3e092b3)
12b7981f6SKowalski, Kamil /*
22b7981f6SKowalski, Kamil // Copyright (c) 2018 Intel Corporation
32b7981f6SKowalski, Kamil //
42b7981f6SKowalski, Kamil // Licensed under the Apache License, Version 2.0 (the "License");
52b7981f6SKowalski, Kamil // you may not use this file except in compliance with the License.
62b7981f6SKowalski, Kamil // You may obtain a copy of the License at
72b7981f6SKowalski, Kamil //
82b7981f6SKowalski, Kamil //      http://www.apache.org/licenses/LICENSE-2.0
92b7981f6SKowalski, Kamil //
102b7981f6SKowalski, Kamil // Unless required by applicable law or agreed to in writing, software
112b7981f6SKowalski, Kamil // distributed under the License is distributed on an "AS IS" BASIS,
122b7981f6SKowalski, Kamil // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
132b7981f6SKowalski, Kamil // See the License for the specific language governing permissions and
142b7981f6SKowalski, Kamil // limitations under the License.
152b7981f6SKowalski, Kamil */
162b7981f6SKowalski, Kamil #pragma once
1743a095abSBorawski.Lukasz 
18f4c4dcf4SKowalski, Kamil #include "error_messages.hpp"
192b7981f6SKowalski, Kamil #include "node.hpp"
204b1b8683SBorawski.Lukasz #include "persistent_data_middleware.hpp"
212b7981f6SKowalski, Kamil 
221abe55efSEd Tanous namespace redfish
231abe55efSEd Tanous {
242b7981f6SKowalski, Kamil 
252b7981f6SKowalski, Kamil class SessionCollection;
262b7981f6SKowalski, Kamil 
271abe55efSEd Tanous class Sessions : public Node
281abe55efSEd Tanous {
292b7981f6SKowalski, Kamil   public:
301abe55efSEd Tanous     Sessions(CrowApp& app) :
311abe55efSEd Tanous         Node(app, "/redfish/v1/SessionService/Sessions/<str>/", std::string())
321abe55efSEd Tanous     {
33e0d918bcSEd Tanous         entityPrivileges = {
34e0d918bcSEd Tanous             {boost::beast::http::verb::get, {{"Login"}}},
35e0d918bcSEd Tanous             {boost::beast::http::verb::head, {{"Login"}}},
36e0d918bcSEd Tanous             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
37e0d918bcSEd Tanous             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
38e0d918bcSEd Tanous             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
39e0d918bcSEd Tanous             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
402b7981f6SKowalski, Kamil     }
412b7981f6SKowalski, Kamil 
422b7981f6SKowalski, Kamil   private:
4355c7b7a2SEd Tanous     void doGet(crow::Response& res, const crow::Request& req,
441abe55efSEd Tanous                const std::vector<std::string>& params) override
451abe55efSEd Tanous     {
462b7981f6SKowalski, Kamil         auto session =
4755c7b7a2SEd Tanous             crow::persistent_data::SessionStore::getInstance().getSessionByUid(
484b1b8683SBorawski.Lukasz                 params[0]);
492b7981f6SKowalski, Kamil 
501abe55efSEd Tanous         if (session == nullptr)
511abe55efSEd Tanous         {
52f12894f8SJason M. Bills             messages::resourceNotFound(res, "Session", params[0]);
532b7981f6SKowalski, Kamil             res.end();
542b7981f6SKowalski, Kamil             return;
552b7981f6SKowalski, Kamil         }
562b7981f6SKowalski, Kamil 
570f74e643SEd Tanous         res.jsonValue["Id"] = session->uniqueId;
580f74e643SEd Tanous         res.jsonValue["UserName"] = session->username;
590f74e643SEd Tanous         res.jsonValue["@odata.id"] =
6055c7b7a2SEd Tanous             "/redfish/v1/SessionService/Sessions/" + session->uniqueId;
610f74e643SEd Tanous         res.jsonValue["@odata.type"] = "#Session.v1_0_2.Session";
620f74e643SEd Tanous         res.jsonValue["@odata.context"] =
630f74e643SEd Tanous             "/redfish/v1/$metadata#Session.Session";
640f74e643SEd Tanous         res.jsonValue["Name"] = "User Session";
650f74e643SEd Tanous         res.jsonValue["Description"] = "Manager User Session";
662b7981f6SKowalski, Kamil 
672b7981f6SKowalski, Kamil         res.end();
682b7981f6SKowalski, Kamil     }
692b7981f6SKowalski, Kamil 
7055c7b7a2SEd Tanous     void doDelete(crow::Response& res, const crow::Request& req,
711abe55efSEd Tanous                   const std::vector<std::string>& params) override
721abe55efSEd Tanous     {
732b7981f6SKowalski, Kamil         // Need only 1 param which should be id of session to be deleted
741abe55efSEd Tanous         if (params.size() != 1)
751abe55efSEd Tanous         {
76f4c4dcf4SKowalski, Kamil             // This should be handled by crow and never happen
771abe55efSEd Tanous             BMCWEB_LOG_ERROR << "Session DELETE has been called with invalid "
781abe55efSEd Tanous                                 "number of params";
79f4c4dcf4SKowalski, Kamil 
80f12894f8SJason M. Bills             messages::generalError(res);
812b7981f6SKowalski, Kamil             res.end();
822b7981f6SKowalski, Kamil             return;
832b7981f6SKowalski, Kamil         }
842b7981f6SKowalski, Kamil 
852b7981f6SKowalski, Kamil         auto session =
8655c7b7a2SEd Tanous             crow::persistent_data::SessionStore::getInstance().getSessionByUid(
874b1b8683SBorawski.Lukasz                 params[0]);
882b7981f6SKowalski, Kamil 
891abe55efSEd Tanous         if (session == nullptr)
901abe55efSEd Tanous         {
91f12894f8SJason M. Bills             messages::resourceNotFound(res, "Session", params[0]);
922b7981f6SKowalski, Kamil             res.end();
932b7981f6SKowalski, Kamil             return;
942b7981f6SKowalski, Kamil         }
952b7981f6SKowalski, Kamil 
96f4c4dcf4SKowalski, Kamil         // DELETE should return representation of object that will be removed
97f4c4dcf4SKowalski, Kamil         doGet(res, req, params);
98f4c4dcf4SKowalski, Kamil 
991abe55efSEd Tanous         crow::persistent_data::SessionStore::getInstance().removeSession(
1001abe55efSEd Tanous             session);
1012b7981f6SKowalski, Kamil     }
1022b7981f6SKowalski, Kamil 
1032b7981f6SKowalski, Kamil     /**
1042b7981f6SKowalski, Kamil      * This allows SessionCollection to reuse this class' doGet method, to
1051abe55efSEd Tanous      * maintain consistency of returned data, as Collection's doPost should
1061abe55efSEd Tanous      * return data for created member which should match member's doGet result
1071abe55efSEd Tanous      * in 100%
1082b7981f6SKowalski, Kamil      */
1092b7981f6SKowalski, Kamil     friend SessionCollection;
1102b7981f6SKowalski, Kamil };
1112b7981f6SKowalski, Kamil 
1121abe55efSEd Tanous class SessionCollection : public Node
1131abe55efSEd Tanous {
1142b7981f6SKowalski, Kamil   public:
1151abe55efSEd Tanous     SessionCollection(CrowApp& app) :
1161abe55efSEd Tanous         Node(app, "/redfish/v1/SessionService/Sessions/"), memberSession(app)
1171abe55efSEd Tanous     {
118e0d918bcSEd Tanous         entityPrivileges = {
119e0d918bcSEd Tanous             {boost::beast::http::verb::get, {{"Login"}}},
120e0d918bcSEd Tanous             {boost::beast::http::verb::head, {{"Login"}}},
121e0d918bcSEd Tanous             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
122e0d918bcSEd Tanous             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
123e0d918bcSEd Tanous             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
124e0d918bcSEd Tanous             {boost::beast::http::verb::post, {}}};
1252b7981f6SKowalski, Kamil     }
1262b7981f6SKowalski, Kamil 
1272b7981f6SKowalski, Kamil   private:
12855c7b7a2SEd Tanous     void doGet(crow::Response& res, const crow::Request& req,
1291abe55efSEd Tanous                const std::vector<std::string>& params) override
1301abe55efSEd Tanous     {
13155c7b7a2SEd Tanous         std::vector<const std::string*> sessionIds =
13255c7b7a2SEd Tanous             crow::persistent_data::SessionStore::getInstance().getUniqueIds(
13355c7b7a2SEd Tanous                 false, crow::persistent_data::PersistenceType::TIMEOUT);
1342b7981f6SKowalski, Kamil 
1350f74e643SEd Tanous         res.jsonValue["Members@odata.count"] = sessionIds.size();
1360f74e643SEd Tanous         res.jsonValue["Members"] = nlohmann::json::array();
1371abe55efSEd Tanous         for (const std::string* uid : sessionIds)
1381abe55efSEd Tanous         {
1390f74e643SEd Tanous             res.jsonValue["Members"].push_back(
1402b7981f6SKowalski, Kamil                 {{"@odata.id", "/redfish/v1/SessionService/Sessions/" + *uid}});
1412b7981f6SKowalski, Kamil         }
142*f00032dbSTanous         res.jsonValue["Members@odata.count"] = sessionIds.size();
1430f74e643SEd Tanous         res.jsonValue["@odata.type"] = "#SessionCollection.SessionCollection";
1440f74e643SEd Tanous         res.jsonValue["@odata.id"] = "/redfish/v1/SessionService/Sessions/";
1450f74e643SEd Tanous         res.jsonValue["@odata.context"] =
1460f74e643SEd Tanous             "/redfish/v1/$metadata#SessionCollection.SessionCollection";
1470f74e643SEd Tanous         res.jsonValue["Name"] = "Session Collection";
1480f74e643SEd Tanous         res.jsonValue["Description"] = "Session Collection";
1492b7981f6SKowalski, Kamil         res.end();
1502b7981f6SKowalski, Kamil     }
1512b7981f6SKowalski, Kamil 
15255c7b7a2SEd Tanous     void doPost(crow::Response& res, const crow::Request& req,
1531abe55efSEd Tanous                 const std::vector<std::string>& params) override
1541abe55efSEd Tanous     {
1559712f8acSEd Tanous         std::string username;
1569712f8acSEd Tanous         std::string password;
1579712f8acSEd Tanous         if (!json_util::readJson(req, res, "UserName", username, "Password",
1589712f8acSEd Tanous                                  password))
1591abe55efSEd Tanous         {
1602b7981f6SKowalski, Kamil             res.end();
1612b7981f6SKowalski, Kamil             return;
1622b7981f6SKowalski, Kamil         }
1632b7981f6SKowalski, Kamil 
164820ce598SEd Tanous         if (password.empty() || username.empty() ||
165820ce598SEd Tanous             res.result() != boost::beast::http::status::ok)
1661abe55efSEd Tanous         {
1671abe55efSEd Tanous             if (username.empty())
1681abe55efSEd Tanous             {
169a08b46ccSJason M. Bills                 messages::propertyMissing(res, "UserName");
170f4c4dcf4SKowalski, Kamil             }
171f4c4dcf4SKowalski, Kamil 
1721abe55efSEd Tanous             if (password.empty())
1731abe55efSEd Tanous             {
174a08b46ccSJason M. Bills                 messages::propertyMissing(res, "Password");
175820ce598SEd Tanous             }
176820ce598SEd Tanous             res.end();
177820ce598SEd Tanous 
178820ce598SEd Tanous             return;
179f4c4dcf4SKowalski, Kamil         }
1802b7981f6SKowalski, Kamil 
1811abe55efSEd Tanous         if (!pamAuthenticateUser(username, password))
1821abe55efSEd Tanous         {
183f12894f8SJason M. Bills             messages::resourceAtUriUnauthorized(res, std::string(req.url),
184f12894f8SJason M. Bills                                                 "Invalid username or password");
185820ce598SEd Tanous             res.end();
1862b7981f6SKowalski, Kamil 
187820ce598SEd Tanous             return;
1882b7981f6SKowalski, Kamil         }
1892b7981f6SKowalski, Kamil 
190820ce598SEd Tanous         // User is authenticated - create session
191820ce598SEd Tanous         std::shared_ptr<crow::persistent_data::UserSession> session =
192820ce598SEd Tanous             crow::persistent_data::SessionStore::getInstance()
193820ce598SEd Tanous                 .generateUserSession(username);
194820ce598SEd Tanous         res.addHeader("X-Auth-Token", session->sessionToken);
195820ce598SEd Tanous         res.addHeader("Location", "/redfish/v1/SessionService/Sessions/" +
196820ce598SEd Tanous                                       session->uniqueId);
197820ce598SEd Tanous         res.result(boost::beast::http::status::created);
198820ce598SEd Tanous         memberSession.doGet(res, req, {session->uniqueId});
1992b7981f6SKowalski, Kamil     }
2002b7981f6SKowalski, Kamil 
2012b7981f6SKowalski, Kamil     /**
2022b7981f6SKowalski, Kamil      * Member session to ensure consistency between collection's doPost and
2032b7981f6SKowalski, Kamil      * member's doGet, as they should return 100% matching data
2042b7981f6SKowalski, Kamil      */
2052b7981f6SKowalski, Kamil     Sessions memberSession;
2062b7981f6SKowalski, Kamil };
2072b7981f6SKowalski, Kamil 
2081abe55efSEd Tanous class SessionService : public Node
2091abe55efSEd Tanous {
2105d27b854SBorawski.Lukasz   public:
2111abe55efSEd Tanous     SessionService(CrowApp& app) : Node(app, "/redfish/v1/SessionService/")
2121abe55efSEd Tanous     {
2133ebd75f7SEd Tanous 
214e0d918bcSEd Tanous         entityPrivileges = {
215e0d918bcSEd Tanous             {boost::beast::http::verb::get, {{"Login"}}},
216e0d918bcSEd Tanous             {boost::beast::http::verb::head, {{"Login"}}},
217e0d918bcSEd Tanous             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
218e0d918bcSEd Tanous             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
219e0d918bcSEd Tanous             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
220e0d918bcSEd Tanous             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
2215d27b854SBorawski.Lukasz     }
2225d27b854SBorawski.Lukasz 
2235d27b854SBorawski.Lukasz   private:
22455c7b7a2SEd Tanous     void doGet(crow::Response& res, const crow::Request& req,
2251abe55efSEd Tanous                const std::vector<std::string>& params) override
2261abe55efSEd Tanous     {
2270f74e643SEd Tanous         res.jsonValue["@odata.type"] = "#SessionService.v1_0_2.SessionService";
2280f74e643SEd Tanous         res.jsonValue["@odata.id"] = "/redfish/v1/SessionService/";
2290f74e643SEd Tanous         res.jsonValue["@odata.context"] =
2300f74e643SEd Tanous             "/redfish/v1/$metadata#SessionService.SessionService";
2310f74e643SEd Tanous         res.jsonValue["Name"] = "Session Service";
2320f74e643SEd Tanous         res.jsonValue["Id"] = "SessionService";
2330f74e643SEd Tanous         res.jsonValue["Description"] = "Session Service";
2340f74e643SEd Tanous         res.jsonValue["SessionTimeout"] =
2350f74e643SEd Tanous             crow::persistent_data::SessionStore::getInstance()
2360f74e643SEd Tanous                 .getTimeoutInSeconds();
2370f74e643SEd Tanous         res.jsonValue["ServiceEnabled"] = true;
2380f74e643SEd Tanous 
2390f261533SEd Tanous         res.jsonValue["Sessions"] = {
2400f261533SEd Tanous             {"@odata.id", "/redfish/v1/SessionService/Sessions"}};
2410f261533SEd Tanous 
2425d27b854SBorawski.Lukasz         res.end();
2435d27b854SBorawski.Lukasz     }
2445d27b854SBorawski.Lukasz };
2455d27b854SBorawski.Lukasz 
2462b7981f6SKowalski, Kamil } // namespace redfish
247