| Kowalski, Kamil | 2b7981f | 2018-01-31 13:24:59 +0100 | [diff] [blame] | 1 | /* | 
|  | 2 | // Copyright (c) 2018 Intel Corporation | 
|  | 3 | // | 
|  | 4 | // Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | // you may not use this file except in compliance with the License. | 
|  | 6 | // You may obtain a copy of the License at | 
|  | 7 | // | 
|  | 8 | //      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | // | 
|  | 10 | // Unless required by applicable law or agreed to in writing, software | 
|  | 11 | // distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | // See the License for the specific language governing permissions and | 
|  | 14 | // limitations under the License. | 
|  | 15 | */ | 
|  | 16 | #pragma once | 
| Borawski.Lukasz | 43a095a | 2018-02-19 15:39:01 +0100 | [diff] [blame] | 17 |  | 
| Paul Fertser | ce22f60 | 2024-06-03 20:53:16 +0000 | [diff] [blame] | 18 | #include "account_service.hpp" | 
| Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 19 | #include "app.hpp" | 
| Paul Fertser | 29aab24 | 2024-06-12 19:28:47 +0000 | [diff] [blame] | 20 | #include "cookies.hpp" | 
| Kowalski, Kamil | f4c4dcf | 2018-01-29 14:55:35 +0100 | [diff] [blame] | 21 | #include "error_messages.hpp" | 
| Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 22 | #include "http/utility.hpp" | 
| Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 23 | #include "persistent_data.hpp" | 
| Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 24 | #include "query.hpp" | 
|  | 25 | #include "registries/privilege_registry.hpp" | 
|  | 26 | #include "utils/json_utils.hpp" | 
| John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 27 |  | 
| Ed Tanous | ef4c65b | 2023-04-24 15:28:50 -0700 | [diff] [blame] | 28 | #include <boost/url/format.hpp> | 
|  | 29 |  | 
| Ed Tanous | 89cda63 | 2024-04-16 08:45:54 -0700 | [diff] [blame] | 30 | #include <string> | 
|  | 31 | #include <vector> | 
|  | 32 |  | 
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 33 | namespace redfish | 
|  | 34 | { | 
| Kowalski, Kamil | 2b7981f | 2018-01-31 13:24:59 +0100 | [diff] [blame] | 35 |  | 
| Ed Tanous | 4f48d5f | 2021-06-21 08:27:45 -0700 | [diff] [blame] | 36 | inline void fillSessionObject(crow::Response& res, | 
|  | 37 | const persistent_data::UserSession& session) | 
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 38 | { | 
| Ed Tanous | faa34cc | 2021-06-03 13:27:02 -0700 | [diff] [blame] | 39 | res.jsonValue["Id"] = session.uniqueId; | 
|  | 40 | res.jsonValue["UserName"] = session.username; | 
| Paul Fertser | ce22f60 | 2024-06-03 20:53:16 +0000 | [diff] [blame] | 41 | nlohmann::json::array_t roles; | 
|  | 42 | roles.emplace_back(redfish::getRoleIdFromPrivilege(session.userRole)); | 
|  | 43 | res.jsonValue["Roles"] = std::move(roles); | 
| Ed Tanous | ef4c65b | 2023-04-24 15:28:50 -0700 | [diff] [blame] | 44 | res.jsonValue["@odata.id"] = boost::urls::format( | 
|  | 45 | "/redfish/v1/SessionService/Sessions/{}", session.uniqueId); | 
| Paul Fertser | ce22f60 | 2024-06-03 20:53:16 +0000 | [diff] [blame] | 46 | res.jsonValue["@odata.type"] = "#Session.v1_7_0.Session"; | 
| Ed Tanous | faa34cc | 2021-06-03 13:27:02 -0700 | [diff] [blame] | 47 | res.jsonValue["Name"] = "User Session"; | 
|  | 48 | res.jsonValue["Description"] = "Manager User Session"; | 
|  | 49 | res.jsonValue["ClientOriginIPAddress"] = session.clientIp; | 
| Ed Tanous | bb759e3 | 2022-08-02 17:07:54 -0700 | [diff] [blame] | 50 | if (session.clientId) | 
|  | 51 | { | 
|  | 52 | res.jsonValue["Context"] = *session.clientId; | 
|  | 53 | } | 
| Ed Tanous | faa34cc | 2021-06-03 13:27:02 -0700 | [diff] [blame] | 54 | } | 
| Kowalski, Kamil | 2b7981f | 2018-01-31 13:24:59 +0100 | [diff] [blame] | 55 |  | 
| Ed Tanous | 724340d | 2022-03-14 09:10:07 -0700 | [diff] [blame] | 56 | inline void | 
| Ed Tanous | a1e0871 | 2022-07-07 16:10:39 -0700 | [diff] [blame] | 57 | handleSessionHead(crow::App& app, const crow::Request& req, | 
|  | 58 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, | 
|  | 59 | const std::string& /*sessionId*/) | 
| Ed Tanous | 724340d | 2022-03-14 09:10:07 -0700 | [diff] [blame] | 60 | { | 
| Carson Labrado | 3ba0007 | 2022-06-06 19:40:56 +0000 | [diff] [blame] | 61 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) | 
| Ed Tanous | 45ca1b8 | 2022-03-25 13:07:27 -0700 | [diff] [blame] | 62 | { | 
|  | 63 | return; | 
|  | 64 | } | 
| Ed Tanous | a1e0871 | 2022-07-07 16:10:39 -0700 | [diff] [blame] | 65 | asyncResp->res.addHeader( | 
|  | 66 | boost::beast::http::field::link, | 
|  | 67 | "</redfish/v1/JsonSchemas/Session/Session.json>; rel=describedby"); | 
|  | 68 | } | 
|  | 69 |  | 
|  | 70 | inline void | 
|  | 71 | handleSessionGet(crow::App& app, const crow::Request& req, | 
|  | 72 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, | 
|  | 73 | const std::string& sessionId) | 
|  | 74 | { | 
| Ed Tanous | 65ffbcb | 2023-05-16 08:54:11 -0700 | [diff] [blame] | 75 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) | 
|  | 76 | { | 
|  | 77 | return; | 
|  | 78 | } | 
|  | 79 | asyncResp->res.addHeader( | 
|  | 80 | boost::beast::http::field::link, | 
|  | 81 | "</redfish/v1/JsonSchemas/Session/Session.json>; rel=describedby"); | 
| Ed Tanous | a1e0871 | 2022-07-07 16:10:39 -0700 | [diff] [blame] | 82 |  | 
| Ed Tanous | 724340d | 2022-03-14 09:10:07 -0700 | [diff] [blame] | 83 | // Note that control also reaches here via doPost and doDelete. | 
|  | 84 | auto session = | 
|  | 85 | persistent_data::SessionStore::getInstance().getSessionByUid(sessionId); | 
|  | 86 |  | 
|  | 87 | if (session == nullptr) | 
|  | 88 | { | 
|  | 89 | messages::resourceNotFound(asyncResp->res, "Session", sessionId); | 
|  | 90 | return; | 
|  | 91 | } | 
|  | 92 |  | 
|  | 93 | fillSessionObject(asyncResp->res, *session); | 
|  | 94 | } | 
|  | 95 |  | 
|  | 96 | inline void | 
| Ed Tanous | 45ca1b8 | 2022-03-25 13:07:27 -0700 | [diff] [blame] | 97 | handleSessionDelete(crow::App& app, const crow::Request& req, | 
| Ed Tanous | 724340d | 2022-03-14 09:10:07 -0700 | [diff] [blame] | 98 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, | 
|  | 99 | const std::string& sessionId) | 
|  | 100 | { | 
| Carson Labrado | 3ba0007 | 2022-06-06 19:40:56 +0000 | [diff] [blame] | 101 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) | 
| Ed Tanous | 45ca1b8 | 2022-03-25 13:07:27 -0700 | [diff] [blame] | 102 | { | 
|  | 103 | return; | 
|  | 104 | } | 
| Ed Tanous | 724340d | 2022-03-14 09:10:07 -0700 | [diff] [blame] | 105 | auto session = | 
|  | 106 | persistent_data::SessionStore::getInstance().getSessionByUid(sessionId); | 
|  | 107 |  | 
|  | 108 | if (session == nullptr) | 
|  | 109 | { | 
|  | 110 | messages::resourceNotFound(asyncResp->res, "Session", sessionId); | 
|  | 111 | return; | 
|  | 112 | } | 
|  | 113 |  | 
|  | 114 | // Perform a proper ConfigureSelf authority check.  If a | 
|  | 115 | // session is being used to DELETE some other user's session, | 
|  | 116 | // then the ConfigureSelf privilege does not apply.  In that | 
|  | 117 | // case, perform the authority check again without the user's | 
|  | 118 | // ConfigureSelf privilege. | 
| wukaihua-fii-na | 0fd2986 | 2022-05-18 09:19:16 +0800 | [diff] [blame] | 119 | if (req.session != nullptr && !session->username.empty() && | 
|  | 120 | session->username != req.session->username) | 
| Ed Tanous | 724340d | 2022-03-14 09:10:07 -0700 | [diff] [blame] | 121 | { | 
|  | 122 | Privileges effectiveUserPrivileges = | 
| Ninad Palsule | 3e72c20 | 2023-03-27 17:19:55 -0500 | [diff] [blame] | 123 | redfish::getUserPrivileges(*req.session); | 
| Ed Tanous | 724340d | 2022-03-14 09:10:07 -0700 | [diff] [blame] | 124 |  | 
|  | 125 | if (!effectiveUserPrivileges.isSupersetOf({"ConfigureUsers"})) | 
|  | 126 | { | 
|  | 127 | messages::insufficientPrivilege(asyncResp->res); | 
|  | 128 | return; | 
|  | 129 | } | 
|  | 130 | } | 
|  | 131 |  | 
| Paul Fertser | 29aab24 | 2024-06-12 19:28:47 +0000 | [diff] [blame] | 132 | if (session->cookieAuth) | 
|  | 133 | { | 
|  | 134 | bmcweb::clearSessionCookies(asyncResp->res); | 
|  | 135 | } | 
|  | 136 |  | 
| Ed Tanous | 724340d | 2022-03-14 09:10:07 -0700 | [diff] [blame] | 137 | persistent_data::SessionStore::getInstance().removeSession(session); | 
|  | 138 | messages::success(asyncResp->res); | 
|  | 139 | } | 
|  | 140 |  | 
|  | 141 | inline nlohmann::json getSessionCollectionMembers() | 
|  | 142 | { | 
| Ed Tanous | 89cda63 | 2024-04-16 08:45:54 -0700 | [diff] [blame] | 143 | std::vector<std::string> sessionIds = | 
|  | 144 | persistent_data::SessionStore::getInstance().getAllUniqueIds(); | 
| Ed Tanous | 724340d | 2022-03-14 09:10:07 -0700 | [diff] [blame] | 145 | nlohmann::json ret = nlohmann::json::array(); | 
| Ed Tanous | 89cda63 | 2024-04-16 08:45:54 -0700 | [diff] [blame] | 146 | for (const std::string& uid : sessionIds) | 
| Ed Tanous | 724340d | 2022-03-14 09:10:07 -0700 | [diff] [blame] | 147 | { | 
| Ed Tanous | 1476687 | 2022-03-15 10:44:42 -0700 | [diff] [blame] | 148 | nlohmann::json::object_t session; | 
| Ed Tanous | ef4c65b | 2023-04-24 15:28:50 -0700 | [diff] [blame] | 149 | session["@odata.id"] = | 
| Ed Tanous | 89cda63 | 2024-04-16 08:45:54 -0700 | [diff] [blame] | 150 | boost::urls::format("/redfish/v1/SessionService/Sessions/{}", uid); | 
| Patrick Williams | b2ba307 | 2023-05-12 10:27:39 -0500 | [diff] [blame] | 151 | ret.emplace_back(std::move(session)); | 
| Ed Tanous | 724340d | 2022-03-14 09:10:07 -0700 | [diff] [blame] | 152 | } | 
|  | 153 | return ret; | 
|  | 154 | } | 
|  | 155 |  | 
| Ed Tanous | a1e0871 | 2022-07-07 16:10:39 -0700 | [diff] [blame] | 156 | inline void handleSessionCollectionHead( | 
| Ed Tanous | 45ca1b8 | 2022-03-25 13:07:27 -0700 | [diff] [blame] | 157 | crow::App& app, const crow::Request& req, | 
| Ed Tanous | 724340d | 2022-03-14 09:10:07 -0700 | [diff] [blame] | 158 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) | 
|  | 159 | { | 
| Carson Labrado | 3ba0007 | 2022-06-06 19:40:56 +0000 | [diff] [blame] | 160 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) | 
| Ed Tanous | 45ca1b8 | 2022-03-25 13:07:27 -0700 | [diff] [blame] | 161 | { | 
|  | 162 | return; | 
|  | 163 | } | 
| Ed Tanous | a1e0871 | 2022-07-07 16:10:39 -0700 | [diff] [blame] | 164 | asyncResp->res.addHeader( | 
|  | 165 | boost::beast::http::field::link, | 
|  | 166 | "</redfish/v1/JsonSchemas/SessionCollection.json>; rel=describedby"); | 
|  | 167 | } | 
|  | 168 |  | 
|  | 169 | inline void handleSessionCollectionGet( | 
|  | 170 | crow::App& app, const crow::Request& req, | 
|  | 171 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) | 
|  | 172 | { | 
| Ed Tanous | 01a89a1 | 2022-08-05 09:18:54 -0700 | [diff] [blame] | 173 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) | 
|  | 174 | { | 
|  | 175 | return; | 
|  | 176 | } | 
|  | 177 | asyncResp->res.addHeader( | 
|  | 178 | boost::beast::http::field::link, | 
|  | 179 | "</redfish/v1/JsonSchemas/SessionCollection.json>; rel=describedby"); | 
|  | 180 |  | 
| Ed Tanous | 724340d | 2022-03-14 09:10:07 -0700 | [diff] [blame] | 181 | asyncResp->res.jsonValue["Members"] = getSessionCollectionMembers(); | 
|  | 182 | asyncResp->res.jsonValue["Members@odata.count"] = | 
|  | 183 | asyncResp->res.jsonValue["Members"].size(); | 
|  | 184 | asyncResp->res.jsonValue["@odata.type"] = | 
|  | 185 | "#SessionCollection.SessionCollection"; | 
|  | 186 | asyncResp->res.jsonValue["@odata.id"] = | 
| Gunnar Mills | 7a859ff | 2024-03-04 23:04:45 -0700 | [diff] [blame] | 187 | "/redfish/v1/SessionService/Sessions"; | 
| Ed Tanous | 724340d | 2022-03-14 09:10:07 -0700 | [diff] [blame] | 188 | asyncResp->res.jsonValue["Name"] = "Session Collection"; | 
|  | 189 | asyncResp->res.jsonValue["Description"] = "Session Collection"; | 
|  | 190 | } | 
|  | 191 |  | 
|  | 192 | inline void handleSessionCollectionMembersGet( | 
| Ed Tanous | 45ca1b8 | 2022-03-25 13:07:27 -0700 | [diff] [blame] | 193 | crow::App& app, const crow::Request& req, | 
| Ed Tanous | 724340d | 2022-03-14 09:10:07 -0700 | [diff] [blame] | 194 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) | 
|  | 195 | { | 
| Carson Labrado | 3ba0007 | 2022-06-06 19:40:56 +0000 | [diff] [blame] | 196 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) | 
| Ed Tanous | 45ca1b8 | 2022-03-25 13:07:27 -0700 | [diff] [blame] | 197 | { | 
|  | 198 | return; | 
|  | 199 | } | 
| Ed Tanous | 724340d | 2022-03-14 09:10:07 -0700 | [diff] [blame] | 200 | asyncResp->res.jsonValue = getSessionCollectionMembers(); | 
|  | 201 | } | 
|  | 202 |  | 
| Ed Tanous | 4ee8e21 | 2022-05-28 09:42:51 -0700 | [diff] [blame] | 203 | inline void handleSessionCollectionPost( | 
| Ed Tanous | 45ca1b8 | 2022-03-25 13:07:27 -0700 | [diff] [blame] | 204 | crow::App& app, const crow::Request& req, | 
| Ed Tanous | 724340d | 2022-03-14 09:10:07 -0700 | [diff] [blame] | 205 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) | 
|  | 206 | { | 
| Carson Labrado | 3ba0007 | 2022-06-06 19:40:56 +0000 | [diff] [blame] | 207 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) | 
| Ed Tanous | 45ca1b8 | 2022-03-25 13:07:27 -0700 | [diff] [blame] | 208 | { | 
|  | 209 | return; | 
|  | 210 | } | 
| Ed Tanous | 724340d | 2022-03-14 09:10:07 -0700 | [diff] [blame] | 211 | std::string username; | 
|  | 212 | std::string password; | 
| Ed Tanous | bb759e3 | 2022-08-02 17:07:54 -0700 | [diff] [blame] | 213 | std::optional<std::string> clientId; | 
| Ed Tanous | 724340d | 2022-03-14 09:10:07 -0700 | [diff] [blame] | 214 | if (!json_util::readJsonPatch(req, asyncResp->res, "UserName", username, | 
| Ed Tanous | d678d4f | 2023-01-07 14:40:40 -0800 | [diff] [blame] | 215 | "Password", password, "Context", clientId)) | 
| Ed Tanous | 724340d | 2022-03-14 09:10:07 -0700 | [diff] [blame] | 216 | { | 
|  | 217 | return; | 
|  | 218 | } | 
|  | 219 |  | 
|  | 220 | if (password.empty() || username.empty() || | 
|  | 221 | asyncResp->res.result() != boost::beast::http::status::ok) | 
|  | 222 | { | 
|  | 223 | if (username.empty()) | 
|  | 224 | { | 
|  | 225 | messages::propertyMissing(asyncResp->res, "UserName"); | 
|  | 226 | } | 
|  | 227 |  | 
|  | 228 | if (password.empty()) | 
|  | 229 | { | 
|  | 230 | messages::propertyMissing(asyncResp->res, "Password"); | 
|  | 231 | } | 
|  | 232 |  | 
|  | 233 | return; | 
|  | 234 | } | 
|  | 235 |  | 
|  | 236 | int pamrc = pamAuthenticateUser(username, password); | 
|  | 237 | bool isConfigureSelfOnly = pamrc == PAM_NEW_AUTHTOK_REQD; | 
|  | 238 | if ((pamrc != PAM_SUCCESS) && !isConfigureSelfOnly) | 
|  | 239 | { | 
| Ed Tanous | 39662a3 | 2023-02-06 15:09:46 -0800 | [diff] [blame] | 240 | messages::resourceAtUriUnauthorized(asyncResp->res, req.url(), | 
| Ed Tanous | 724340d | 2022-03-14 09:10:07 -0700 | [diff] [blame] | 241 | "Invalid username or password"); | 
|  | 242 | return; | 
|  | 243 | } | 
| Ed Tanous | 724340d | 2022-03-14 09:10:07 -0700 | [diff] [blame] | 244 |  | 
|  | 245 | // User is authenticated - create session | 
|  | 246 | std::shared_ptr<persistent_data::UserSession> session = | 
|  | 247 | persistent_data::SessionStore::getInstance().generateUserSession( | 
|  | 248 | username, req.ipAddress, clientId, | 
| Ed Tanous | 89cda63 | 2024-04-16 08:45:54 -0700 | [diff] [blame] | 249 | persistent_data::SessionType::Session, isConfigureSelfOnly); | 
| Brad Bishop | 02e53ae | 2022-07-29 14:38:40 -0400 | [diff] [blame] | 250 | if (session == nullptr) | 
|  | 251 | { | 
|  | 252 | messages::internalError(asyncResp->res); | 
|  | 253 | return; | 
|  | 254 | } | 
|  | 255 |  | 
| Paul Fertser | 29aab24 | 2024-06-12 19:28:47 +0000 | [diff] [blame] | 256 | // When session is created by webui-vue give it session cookies as a | 
|  | 257 | // non-standard Redfish extension. This is needed for authentication for | 
|  | 258 | // WebSockets-based functionality. | 
|  | 259 | if (!req.getHeaderValue("X-Requested-With").empty()) | 
|  | 260 | { | 
|  | 261 | bmcweb::setSessionCookies(asyncResp->res, *session); | 
|  | 262 | } | 
|  | 263 | else | 
|  | 264 | { | 
|  | 265 | asyncResp->res.addHeader("X-Auth-Token", session->sessionToken); | 
|  | 266 | } | 
|  | 267 |  | 
| Ed Tanous | 724340d | 2022-03-14 09:10:07 -0700 | [diff] [blame] | 268 | asyncResp->res.addHeader( | 
|  | 269 | "Location", "/redfish/v1/SessionService/Sessions/" + session->uniqueId); | 
|  | 270 | asyncResp->res.result(boost::beast::http::status::created); | 
|  | 271 | if (session->isConfigureSelfOnly) | 
|  | 272 | { | 
|  | 273 | messages::passwordChangeRequired( | 
|  | 274 | asyncResp->res, | 
| Ed Tanous | ef4c65b | 2023-04-24 15:28:50 -0700 | [diff] [blame] | 275 | boost::urls::format("/redfish/v1/AccountService/Accounts/{}", | 
|  | 276 | session->username)); | 
| Ed Tanous | 724340d | 2022-03-14 09:10:07 -0700 | [diff] [blame] | 277 | } | 
|  | 278 |  | 
| Paul Fertser | 478c5a5 | 2024-06-26 22:27:59 +0000 | [diff] [blame] | 279 | crow::getUserInfo(asyncResp, username, session, [asyncResp, session]() { | 
|  | 280 | fillSessionObject(asyncResp->res, *session); | 
|  | 281 | }); | 
| Ed Tanous | 724340d | 2022-03-14 09:10:07 -0700 | [diff] [blame] | 282 | } | 
| Ed Tanous | a1e0871 | 2022-07-07 16:10:39 -0700 | [diff] [blame] | 283 | inline void handleSessionServiceHead( | 
|  | 284 | crow::App& app, const crow::Request& req, | 
|  | 285 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) | 
|  | 286 | { | 
| Ed Tanous | a1e0871 | 2022-07-07 16:10:39 -0700 | [diff] [blame] | 287 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) | 
|  | 288 | { | 
|  | 289 | return; | 
|  | 290 | } | 
|  | 291 | asyncResp->res.addHeader( | 
|  | 292 | boost::beast::http::field::link, | 
|  | 293 | "</redfish/v1/JsonSchemas/SessionService/SessionService.json>; rel=describedby"); | 
|  | 294 | } | 
| Ed Tanous | 724340d | 2022-03-14 09:10:07 -0700 | [diff] [blame] | 295 | inline void | 
| Ed Tanous | 45ca1b8 | 2022-03-25 13:07:27 -0700 | [diff] [blame] | 296 | handleSessionServiceGet(crow::App& app, const crow::Request& req, | 
| Ed Tanous | 724340d | 2022-03-14 09:10:07 -0700 | [diff] [blame] | 297 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) | 
|  | 298 |  | 
|  | 299 | { | 
| Gunnar Mills | 78e3900 | 2023-05-17 11:52:44 -0500 | [diff] [blame] | 300 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) | 
|  | 301 | { | 
|  | 302 | return; | 
|  | 303 | } | 
|  | 304 | asyncResp->res.addHeader( | 
|  | 305 | boost::beast::http::field::link, | 
|  | 306 | "</redfish/v1/JsonSchemas/SessionService/SessionService.json>; rel=describedby"); | 
|  | 307 |  | 
| Ed Tanous | 724340d | 2022-03-14 09:10:07 -0700 | [diff] [blame] | 308 | asyncResp->res.jsonValue["@odata.type"] = | 
|  | 309 | "#SessionService.v1_0_2.SessionService"; | 
| Gunnar Mills | 7a859ff | 2024-03-04 23:04:45 -0700 | [diff] [blame] | 310 | asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/SessionService"; | 
| Ed Tanous | 724340d | 2022-03-14 09:10:07 -0700 | [diff] [blame] | 311 | asyncResp->res.jsonValue["Name"] = "Session Service"; | 
|  | 312 | asyncResp->res.jsonValue["Id"] = "SessionService"; | 
|  | 313 | asyncResp->res.jsonValue["Description"] = "Session Service"; | 
|  | 314 | asyncResp->res.jsonValue["SessionTimeout"] = | 
|  | 315 | persistent_data::SessionStore::getInstance().getTimeoutInSeconds(); | 
|  | 316 | asyncResp->res.jsonValue["ServiceEnabled"] = true; | 
|  | 317 |  | 
| Ed Tanous | 1476687 | 2022-03-15 10:44:42 -0700 | [diff] [blame] | 318 | asyncResp->res.jsonValue["Sessions"]["@odata.id"] = | 
|  | 319 | "/redfish/v1/SessionService/Sessions"; | 
| Ed Tanous | 724340d | 2022-03-14 09:10:07 -0700 | [diff] [blame] | 320 | } | 
|  | 321 |  | 
|  | 322 | inline void handleSessionServicePatch( | 
| Ed Tanous | 45ca1b8 | 2022-03-25 13:07:27 -0700 | [diff] [blame] | 323 | crow::App& app, const crow::Request& req, | 
| Ed Tanous | 724340d | 2022-03-14 09:10:07 -0700 | [diff] [blame] | 324 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) | 
|  | 325 | { | 
| Carson Labrado | 3ba0007 | 2022-06-06 19:40:56 +0000 | [diff] [blame] | 326 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) | 
| Ed Tanous | 45ca1b8 | 2022-03-25 13:07:27 -0700 | [diff] [blame] | 327 | { | 
|  | 328 | return; | 
|  | 329 | } | 
| Ed Tanous | 724340d | 2022-03-14 09:10:07 -0700 | [diff] [blame] | 330 | std::optional<int64_t> sessionTimeout; | 
|  | 331 | if (!json_util::readJsonPatch(req, asyncResp->res, "SessionTimeout", | 
|  | 332 | sessionTimeout)) | 
|  | 333 | { | 
|  | 334 | return; | 
|  | 335 | } | 
|  | 336 |  | 
|  | 337 | if (sessionTimeout) | 
|  | 338 | { | 
| Ed Tanous | 8ece0e4 | 2024-01-02 13:16:50 -0800 | [diff] [blame] | 339 | // The minimum & maximum allowed values for session timeout | 
| Ed Tanous | 724340d | 2022-03-14 09:10:07 -0700 | [diff] [blame] | 340 | // are 30 seconds and 86400 seconds respectively as per the | 
|  | 341 | // session service schema mentioned at | 
|  | 342 | // https://redfish.dmtf.org/schemas/v1/SessionService.v1_1_7.json | 
|  | 343 |  | 
|  | 344 | if (*sessionTimeout <= 86400 && *sessionTimeout >= 30) | 
|  | 345 | { | 
|  | 346 | std::chrono::seconds sessionTimeoutInseconds(*sessionTimeout); | 
|  | 347 | persistent_data::SessionStore::getInstance().updateSessionTimeout( | 
|  | 348 | sessionTimeoutInseconds); | 
|  | 349 | messages::propertyValueModified(asyncResp->res, "SessionTimeOut", | 
|  | 350 | std::to_string(*sessionTimeout)); | 
|  | 351 | } | 
|  | 352 | else | 
|  | 353 | { | 
| Ed Tanous | e2616cc | 2022-06-27 12:45:55 -0700 | [diff] [blame] | 354 | messages::propertyValueNotInList(asyncResp->res, *sessionTimeout, | 
| Ed Tanous | 724340d | 2022-03-14 09:10:07 -0700 | [diff] [blame] | 355 | "SessionTimeOut"); | 
|  | 356 | } | 
|  | 357 | } | 
|  | 358 | } | 
|  | 359 |  | 
| Ed Tanous | faa34cc | 2021-06-03 13:27:02 -0700 | [diff] [blame] | 360 | inline void requestRoutesSession(App& app) | 
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 361 | { | 
| Ed Tanous | faa34cc | 2021-06-03 13:27:02 -0700 | [diff] [blame] | 362 | BMCWEB_ROUTE(app, "/redfish/v1/SessionService/Sessions/<str>/") | 
| Ed Tanous | a1e0871 | 2022-07-07 16:10:39 -0700 | [diff] [blame] | 363 | .privileges(redfish::privileges::headSession) | 
|  | 364 | .methods(boost::beast::http::verb::head)( | 
|  | 365 | std::bind_front(handleSessionHead, std::ref(app))); | 
|  | 366 |  | 
|  | 367 | BMCWEB_ROUTE(app, "/redfish/v1/SessionService/Sessions/<str>/") | 
| Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 368 | .privileges(redfish::privileges::getSession) | 
| Ed Tanous | 45ca1b8 | 2022-03-25 13:07:27 -0700 | [diff] [blame] | 369 | .methods(boost::beast::http::verb::get)( | 
|  | 370 | std::bind_front(handleSessionGet, std::ref(app))); | 
| Kowalski, Kamil | 2b7981f | 2018-01-31 13:24:59 +0100 | [diff] [blame] | 371 |  | 
| Ed Tanous | faa34cc | 2021-06-03 13:27:02 -0700 | [diff] [blame] | 372 | BMCWEB_ROUTE(app, "/redfish/v1/SessionService/Sessions/<str>/") | 
| Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 373 | .privileges(redfish::privileges::deleteSession) | 
| Ed Tanous | 45ca1b8 | 2022-03-25 13:07:27 -0700 | [diff] [blame] | 374 | .methods(boost::beast::http::verb::delete_)( | 
|  | 375 | std::bind_front(handleSessionDelete, std::ref(app))); | 
| Ed Tanous | faa34cc | 2021-06-03 13:27:02 -0700 | [diff] [blame] | 376 |  | 
|  | 377 | BMCWEB_ROUTE(app, "/redfish/v1/SessionService/Sessions/") | 
| Ed Tanous | a1e0871 | 2022-07-07 16:10:39 -0700 | [diff] [blame] | 378 | .privileges(redfish::privileges::headSessionCollection) | 
|  | 379 | .methods(boost::beast::http::verb::head)( | 
|  | 380 | std::bind_front(handleSessionCollectionHead, std::ref(app))); | 
|  | 381 |  | 
|  | 382 | BMCWEB_ROUTE(app, "/redfish/v1/SessionService/Sessions/") | 
| Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 383 | .privileges(redfish::privileges::getSessionCollection) | 
| Ed Tanous | 45ca1b8 | 2022-03-25 13:07:27 -0700 | [diff] [blame] | 384 | .methods(boost::beast::http::verb::get)( | 
|  | 385 | std::bind_front(handleSessionCollectionGet, std::ref(app))); | 
| Ed Tanous | faa34cc | 2021-06-03 13:27:02 -0700 | [diff] [blame] | 386 |  | 
| Ed Tanous | e76cd86 | 2022-03-14 09:12:00 -0700 | [diff] [blame] | 387 | // Note, the next two routes technically don't match the privilege | 
| Ed Tanous | 724340d | 2022-03-14 09:10:07 -0700 | [diff] [blame] | 388 | // registry given the way login mechanisms work.  The base privilege | 
|  | 389 | // registry lists this endpoint as requiring login privilege, but because | 
|  | 390 | // this is the endpoint responsible for giving the login privilege, and it | 
|  | 391 | // is itself its own route, it needs to not require Login | 
| Ed Tanous | faa34cc | 2021-06-03 13:27:02 -0700 | [diff] [blame] | 392 | BMCWEB_ROUTE(app, "/redfish/v1/SessionService/Sessions/") | 
|  | 393 | .privileges({}) | 
| Ed Tanous | 45ca1b8 | 2022-03-25 13:07:27 -0700 | [diff] [blame] | 394 | .methods(boost::beast::http::verb::post)( | 
|  | 395 | std::bind_front(handleSessionCollectionPost, std::ref(app))); | 
| Kowalski, Kamil | 2b7981f | 2018-01-31 13:24:59 +0100 | [diff] [blame] | 396 |  | 
| Ed Tanous | e76cd86 | 2022-03-14 09:12:00 -0700 | [diff] [blame] | 397 | BMCWEB_ROUTE(app, "/redfish/v1/SessionService/Sessions/Members/") | 
|  | 398 | .privileges({}) | 
| Ed Tanous | 45ca1b8 | 2022-03-25 13:07:27 -0700 | [diff] [blame] | 399 | .methods(boost::beast::http::verb::post)( | 
|  | 400 | std::bind_front(handleSessionCollectionPost, std::ref(app))); | 
| Ed Tanous | e76cd86 | 2022-03-14 09:12:00 -0700 | [diff] [blame] | 401 |  | 
| Ed Tanous | faa34cc | 2021-06-03 13:27:02 -0700 | [diff] [blame] | 402 | BMCWEB_ROUTE(app, "/redfish/v1/SessionService/") | 
| Ed Tanous | a1e0871 | 2022-07-07 16:10:39 -0700 | [diff] [blame] | 403 | .privileges(redfish::privileges::headSessionService) | 
|  | 404 | .methods(boost::beast::http::verb::head)( | 
|  | 405 | std::bind_front(handleSessionServiceHead, std::ref(app))); | 
|  | 406 |  | 
|  | 407 | BMCWEB_ROUTE(app, "/redfish/v1/SessionService/") | 
| Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 408 | .privileges(redfish::privileges::getSessionService) | 
| Ed Tanous | 45ca1b8 | 2022-03-25 13:07:27 -0700 | [diff] [blame] | 409 | .methods(boost::beast::http::verb::get)( | 
|  | 410 | std::bind_front(handleSessionServiceGet, std::ref(app))); | 
| Borawski.Lukasz | 5d27b85 | 2018-02-08 13:24:24 +0100 | [diff] [blame] | 411 |  | 
| Ed Tanous | faa34cc | 2021-06-03 13:27:02 -0700 | [diff] [blame] | 412 | BMCWEB_ROUTE(app, "/redfish/v1/SessionService/") | 
| Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 413 | .privileges(redfish::privileges::patchSessionService) | 
| Ed Tanous | 45ca1b8 | 2022-03-25 13:07:27 -0700 | [diff] [blame] | 414 | .methods(boost::beast::http::verb::patch)( | 
|  | 415 | std::bind_front(handleSessionServicePatch, std::ref(app))); | 
| Ed Tanous | faa34cc | 2021-06-03 13:27:02 -0700 | [diff] [blame] | 416 | } | 
| Borawski.Lukasz | 5d27b85 | 2018-02-08 13:24:24 +0100 | [diff] [blame] | 417 |  | 
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 418 | } // namespace redfish |