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 | |
Kowalski, Kamil | f4c4dcf | 2018-01-29 14:55:35 +0100 | [diff] [blame] | 18 | #include "error_messages.hpp" |
Kowalski, Kamil | 2b7981f | 2018-01-31 13:24:59 +0100 | [diff] [blame] | 19 | #include "node.hpp" |
Borawski.Lukasz | 4b1b868 | 2018-04-04 12:50:16 +0200 | [diff] [blame] | 20 | #include "persistent_data_middleware.hpp" |
Kowalski, Kamil | 2b7981f | 2018-01-31 13:24:59 +0100 | [diff] [blame] | 21 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 22 | namespace redfish |
| 23 | { |
Kowalski, Kamil | 2b7981f | 2018-01-31 13:24:59 +0100 | [diff] [blame] | 24 | |
| 25 | class SessionCollection; |
| 26 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 27 | class Sessions : public Node |
| 28 | { |
| 29 | public: |
| 30 | Sessions(CrowApp& app) : |
| 31 | Node(app, "/redfish/v1/SessionService/Sessions/<str>/", std::string()) |
| 32 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 33 | entityPrivileges = { |
| 34 | {boost::beast::http::verb::get, {{"Login"}}}, |
| 35 | {boost::beast::http::verb::head, {{"Login"}}}, |
| 36 | {boost::beast::http::verb::patch, {{"ConfigureManager"}}}, |
| 37 | {boost::beast::http::verb::put, {{"ConfigureManager"}}}, |
Joseph Reynolds | 900f949 | 2019-11-25 15:37:29 -0600 | [diff] [blame] | 38 | {boost::beast::http::verb::delete_, |
| 39 | {{"ConfigureManager"}, {"ConfigureSelf"}}}, |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 40 | {boost::beast::http::verb::post, {{"ConfigureManager"}}}}; |
Kowalski, Kamil | 2b7981f | 2018-01-31 13:24:59 +0100 | [diff] [blame] | 41 | } |
| 42 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 43 | private: |
| 44 | void doGet(crow::Response& res, const crow::Request& req, |
| 45 | const std::vector<std::string>& params) override |
| 46 | { |
Joseph Reynolds | 900f949 | 2019-11-25 15:37:29 -0600 | [diff] [blame] | 47 | // Note that control also reaches here via doPost and doDelete. |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 48 | auto session = |
| 49 | crow::persistent_data::SessionStore::getInstance().getSessionByUid( |
| 50 | params[0]); |
Kowalski, Kamil | 2b7981f | 2018-01-31 13:24:59 +0100 | [diff] [blame] | 51 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 52 | if (session == nullptr) |
| 53 | { |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 54 | messages::resourceNotFound(res, "Session", params[0]); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 55 | res.end(); |
| 56 | return; |
| 57 | } |
Kowalski, Kamil | f4c4dcf | 2018-01-29 14:55:35 +0100 | [diff] [blame] | 58 | |
Ed Tanous | 0f74e64 | 2018-11-12 15:17:05 -0800 | [diff] [blame] | 59 | res.jsonValue["Id"] = session->uniqueId; |
| 60 | res.jsonValue["UserName"] = session->username; |
| 61 | res.jsonValue["@odata.id"] = |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 62 | "/redfish/v1/SessionService/Sessions/" + session->uniqueId; |
Ed Tanous | 0f74e64 | 2018-11-12 15:17:05 -0800 | [diff] [blame] | 63 | res.jsonValue["@odata.type"] = "#Session.v1_0_2.Session"; |
| 64 | res.jsonValue["@odata.context"] = |
| 65 | "/redfish/v1/$metadata#Session.Session"; |
| 66 | res.jsonValue["Name"] = "User Session"; |
| 67 | res.jsonValue["Description"] = "Manager User Session"; |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 68 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 69 | res.end(); |
Kowalski, Kamil | 2b7981f | 2018-01-31 13:24:59 +0100 | [diff] [blame] | 70 | } |
| 71 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 72 | void doDelete(crow::Response& res, const crow::Request& req, |
| 73 | const std::vector<std::string>& params) override |
| 74 | { |
| 75 | // Need only 1 param which should be id of session to be deleted |
| 76 | if (params.size() != 1) |
| 77 | { |
| 78 | // This should be handled by crow and never happen |
| 79 | BMCWEB_LOG_ERROR << "Session DELETE has been called with invalid " |
| 80 | "number of params"; |
Kowalski, Kamil | 2b7981f | 2018-01-31 13:24:59 +0100 | [diff] [blame] | 81 | |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 82 | messages::generalError(res); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 83 | res.end(); |
| 84 | return; |
| 85 | } |
| 86 | |
| 87 | auto session = |
| 88 | crow::persistent_data::SessionStore::getInstance().getSessionByUid( |
| 89 | params[0]); |
| 90 | |
| 91 | if (session == nullptr) |
| 92 | { |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 93 | messages::resourceNotFound(res, "Session", params[0]); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 94 | res.end(); |
| 95 | return; |
| 96 | } |
| 97 | |
Joseph Reynolds | 900f949 | 2019-11-25 15:37:29 -0600 | [diff] [blame] | 98 | // Perform a proper ConfigureSelf authority check. If a |
| 99 | // session is being used to DELETE some other user's session, |
| 100 | // then the ConfigureSelf privilege does not apply. In that |
| 101 | // case, perform the authority check again without the user's |
| 102 | // ConfigureSelf privilege. |
| 103 | if (session->username != req.session->username) |
| 104 | { |
| 105 | if (!isAllowedWithoutConfigureSelf(req)) |
| 106 | { |
| 107 | BMCWEB_LOG_WARNING << "DELETE Session denied access"; |
| 108 | messages::insufficientPrivilege(res); |
| 109 | res.end(); |
| 110 | return; |
| 111 | } |
| 112 | } |
| 113 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 114 | // DELETE should return representation of object that will be removed |
| 115 | doGet(res, req, params); |
| 116 | |
| 117 | crow::persistent_data::SessionStore::getInstance().removeSession( |
| 118 | session); |
Kowalski, Kamil | 2b7981f | 2018-01-31 13:24:59 +0100 | [diff] [blame] | 119 | } |
| 120 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 121 | /** |
| 122 | * This allows SessionCollection to reuse this class' doGet method, to |
| 123 | * maintain consistency of returned data, as Collection's doPost should |
| 124 | * return data for created member which should match member's doGet result |
| 125 | * in 100% |
| 126 | */ |
| 127 | friend SessionCollection; |
Kowalski, Kamil | 2b7981f | 2018-01-31 13:24:59 +0100 | [diff] [blame] | 128 | }; |
| 129 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 130 | class SessionCollection : public Node |
| 131 | { |
| 132 | public: |
| 133 | SessionCollection(CrowApp& app) : |
| 134 | Node(app, "/redfish/v1/SessionService/Sessions/"), memberSession(app) |
| 135 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 136 | entityPrivileges = { |
| 137 | {boost::beast::http::verb::get, {{"Login"}}}, |
| 138 | {boost::beast::http::verb::head, {{"Login"}}}, |
| 139 | {boost::beast::http::verb::patch, {{"ConfigureManager"}}}, |
| 140 | {boost::beast::http::verb::put, {{"ConfigureManager"}}}, |
| 141 | {boost::beast::http::verb::delete_, {{"ConfigureManager"}}}, |
| 142 | {boost::beast::http::verb::post, {}}}; |
Kowalski, Kamil | 2b7981f | 2018-01-31 13:24:59 +0100 | [diff] [blame] | 143 | } |
| 144 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 145 | private: |
| 146 | void doGet(crow::Response& res, const crow::Request& req, |
| 147 | const std::vector<std::string>& params) override |
| 148 | { |
| 149 | std::vector<const std::string*> sessionIds = |
| 150 | crow::persistent_data::SessionStore::getInstance().getUniqueIds( |
| 151 | false, crow::persistent_data::PersistenceType::TIMEOUT); |
Kowalski, Kamil | 2b7981f | 2018-01-31 13:24:59 +0100 | [diff] [blame] | 152 | |
Ed Tanous | 0f74e64 | 2018-11-12 15:17:05 -0800 | [diff] [blame] | 153 | res.jsonValue["Members@odata.count"] = sessionIds.size(); |
| 154 | res.jsonValue["Members"] = nlohmann::json::array(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 155 | for (const std::string* uid : sessionIds) |
| 156 | { |
Ed Tanous | 0f74e64 | 2018-11-12 15:17:05 -0800 | [diff] [blame] | 157 | res.jsonValue["Members"].push_back( |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 158 | {{"@odata.id", "/redfish/v1/SessionService/Sessions/" + *uid}}); |
| 159 | } |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 160 | res.jsonValue["Members@odata.count"] = sessionIds.size(); |
Ed Tanous | 0f74e64 | 2018-11-12 15:17:05 -0800 | [diff] [blame] | 161 | res.jsonValue["@odata.type"] = "#SessionCollection.SessionCollection"; |
| 162 | res.jsonValue["@odata.id"] = "/redfish/v1/SessionService/Sessions/"; |
| 163 | res.jsonValue["@odata.context"] = |
| 164 | "/redfish/v1/$metadata#SessionCollection.SessionCollection"; |
| 165 | res.jsonValue["Name"] = "Session Collection"; |
| 166 | res.jsonValue["Description"] = "Session Collection"; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 167 | res.end(); |
Kowalski, Kamil | 2b7981f | 2018-01-31 13:24:59 +0100 | [diff] [blame] | 168 | } |
| 169 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 170 | void doPost(crow::Response& res, const crow::Request& req, |
| 171 | const std::vector<std::string>& params) override |
| 172 | { |
Ed Tanous | 9712f8a | 2018-09-21 13:38:49 -0700 | [diff] [blame] | 173 | std::string username; |
| 174 | std::string password; |
| 175 | if (!json_util::readJson(req, res, "UserName", username, "Password", |
| 176 | password)) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 177 | { |
| 178 | res.end(); |
| 179 | return; |
| 180 | } |
Ed Tanous | b9845d9 | 2018-07-24 14:38:06 -0700 | [diff] [blame] | 181 | |
Ed Tanous | 820ce59 | 2018-10-04 15:54:21 -0700 | [diff] [blame] | 182 | if (password.empty() || username.empty() || |
| 183 | res.result() != boost::beast::http::status::ok) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 184 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 185 | if (username.empty()) |
| 186 | { |
Jason M. Bills | a08b46c | 2018-11-06 15:01:08 -0800 | [diff] [blame] | 187 | messages::propertyMissing(res, "UserName"); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | if (password.empty()) |
| 191 | { |
Jason M. Bills | a08b46c | 2018-11-06 15:01:08 -0800 | [diff] [blame] | 192 | messages::propertyMissing(res, "Password"); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 193 | } |
Ed Tanous | 820ce59 | 2018-10-04 15:54:21 -0700 | [diff] [blame] | 194 | res.end(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 195 | |
Ed Tanous | 820ce59 | 2018-10-04 15:54:21 -0700 | [diff] [blame] | 196 | return; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 197 | } |
| 198 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 199 | if (!pamAuthenticateUser(username, password)) |
| 200 | { |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 201 | messages::resourceAtUriUnauthorized(res, std::string(req.url), |
| 202 | "Invalid username or password"); |
Ed Tanous | 820ce59 | 2018-10-04 15:54:21 -0700 | [diff] [blame] | 203 | res.end(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 204 | |
Ed Tanous | 820ce59 | 2018-10-04 15:54:21 -0700 | [diff] [blame] | 205 | return; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 206 | } |
| 207 | |
Ed Tanous | 820ce59 | 2018-10-04 15:54:21 -0700 | [diff] [blame] | 208 | // User is authenticated - create session |
| 209 | std::shared_ptr<crow::persistent_data::UserSession> session = |
| 210 | crow::persistent_data::SessionStore::getInstance() |
| 211 | .generateUserSession(username); |
| 212 | res.addHeader("X-Auth-Token", session->sessionToken); |
| 213 | res.addHeader("Location", "/redfish/v1/SessionService/Sessions/" + |
| 214 | session->uniqueId); |
| 215 | res.result(boost::beast::http::status::created); |
| 216 | memberSession.doGet(res, req, {session->uniqueId}); |
Kowalski, Kamil | 2b7981f | 2018-01-31 13:24:59 +0100 | [diff] [blame] | 217 | } |
| 218 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 219 | /** |
| 220 | * Member session to ensure consistency between collection's doPost and |
| 221 | * member's doGet, as they should return 100% matching data |
| 222 | */ |
| 223 | Sessions memberSession; |
Kowalski, Kamil | 2b7981f | 2018-01-31 13:24:59 +0100 | [diff] [blame] | 224 | }; |
| 225 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 226 | class SessionService : public Node |
| 227 | { |
| 228 | public: |
| 229 | SessionService(CrowApp& app) : Node(app, "/redfish/v1/SessionService/") |
| 230 | { |
Ed Tanous | 3ebd75f | 2018-03-05 18:20:01 -0800 | [diff] [blame] | 231 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 232 | entityPrivileges = { |
| 233 | {boost::beast::http::verb::get, {{"Login"}}}, |
| 234 | {boost::beast::http::verb::head, {{"Login"}}}, |
| 235 | {boost::beast::http::verb::patch, {{"ConfigureManager"}}}, |
| 236 | {boost::beast::http::verb::put, {{"ConfigureManager"}}}, |
| 237 | {boost::beast::http::verb::delete_, {{"ConfigureManager"}}}, |
| 238 | {boost::beast::http::verb::post, {{"ConfigureManager"}}}}; |
| 239 | } |
Borawski.Lukasz | 5d27b85 | 2018-02-08 13:24:24 +0100 | [diff] [blame] | 240 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 241 | private: |
| 242 | void doGet(crow::Response& res, const crow::Request& req, |
| 243 | const std::vector<std::string>& params) override |
| 244 | { |
Ed Tanous | 0f74e64 | 2018-11-12 15:17:05 -0800 | [diff] [blame] | 245 | res.jsonValue["@odata.type"] = "#SessionService.v1_0_2.SessionService"; |
| 246 | res.jsonValue["@odata.id"] = "/redfish/v1/SessionService/"; |
| 247 | res.jsonValue["@odata.context"] = |
| 248 | "/redfish/v1/$metadata#SessionService.SessionService"; |
| 249 | res.jsonValue["Name"] = "Session Service"; |
| 250 | res.jsonValue["Id"] = "SessionService"; |
| 251 | res.jsonValue["Description"] = "Session Service"; |
| 252 | res.jsonValue["SessionTimeout"] = |
| 253 | crow::persistent_data::SessionStore::getInstance() |
| 254 | .getTimeoutInSeconds(); |
| 255 | res.jsonValue["ServiceEnabled"] = true; |
| 256 | |
Ed Tanous | 0f26153 | 2019-02-08 11:13:29 -0800 | [diff] [blame] | 257 | res.jsonValue["Sessions"] = { |
| 258 | {"@odata.id", "/redfish/v1/SessionService/Sessions"}}; |
| 259 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 260 | res.end(); |
| 261 | } |
Borawski.Lukasz | 5d27b85 | 2018-02-08 13:24:24 +0100 | [diff] [blame] | 262 | }; |
| 263 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 264 | } // namespace redfish |