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