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