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