blob: 145bbb175536dfdc05f7900767a7aee9542e5304 [file] [log] [blame]
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +01001/*
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.Lukasz43a095a2018-02-19 15:39:01 +010017
Kowalski, Kamilf4c4dcf2018-01-29 14:55:35 +010018#include "error_messages.hpp"
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +010019#include "node.hpp"
Borawski.Lukasz4b1b8682018-04-04 12:50:16 +020020#include "persistent_data_middleware.hpp"
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +010021
Ed Tanous1abe55e2018-09-05 08:30:59 -070022namespace redfish
23{
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +010024
25class SessionCollection;
26
Ed Tanous1abe55e2018-09-05 08:30:59 -070027class 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 Tanous3ebd75f2018-03-05 18:20:01 -080037
Ed Tanous1abe55e2018-09-05 08:30:59 -070038 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, Kamil2b7981f2018-01-31 13:24:59 +010045 }
46
Ed Tanous1abe55e2018-09-05 08:30:59 -070047 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, Kamil2b7981f2018-01-31 13:24:59 +010054
Ed Tanous1abe55e2018-09-05 08:30:59 -070055 if (session == nullptr)
56 {
57 messages::addMessageToErrorJson(
58 res.jsonValue,
59 messages::resourceNotFound("Session", params[0]));
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +010060
Ed Tanous1abe55e2018-09-05 08:30:59 -070061 res.result(boost::beast::http::status::not_found);
62 res.end();
63 return;
64 }
Kowalski, Kamilf4c4dcf2018-01-29 14:55:35 +010065
Ed Tanous1abe55e2018-09-05 08:30:59 -070066 Node::json["Id"] = session->uniqueId;
67 Node::json["UserName"] = session->username;
68 Node::json["@odata.id"] =
69 "/redfish/v1/SessionService/Sessions/" + session->uniqueId;
Ed Tanouse0d918b2018-03-27 17:41:04 -070070
Ed Tanous1abe55e2018-09-05 08:30:59 -070071 res.jsonValue = Node::json;
72 res.end();
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +010073 }
74
Ed Tanous1abe55e2018-09-05 08:30:59 -070075 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, Kamil2b7981f2018-01-31 13:24:59 +010084
Ed Tanous1abe55e2018-09-05 08:30:59 -070085 res.result(boost::beast::http::status::bad_request);
86 messages::addMessageToErrorJson(res.jsonValue,
87 messages::generalError());
Kowalski, Kamilf4c4dcf2018-01-29 14:55:35 +010088
Ed Tanous1abe55e2018-09-05 08:30:59 -070089 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, Kamil2b7981f2018-01-31 13:24:59 +0100113 }
114
Ed Tanous1abe55e2018-09-05 08:30:59 -0700115 /**
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, Kamil2b7981f2018-01-31 13:24:59 +0100122};
123
Ed Tanous1abe55e2018-09-05 08:30:59 -0700124class 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 Tanous3ebd75f2018-03-05 18:20:01 -0800138
Ed Tanous1abe55e2018-09-05 08:30:59 -0700139 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, Kamil2b7981f2018-01-31 13:24:59 +0100146 }
147
Ed Tanous1abe55e2018-09-05 08:30:59 -0700148 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, Kamil2b7981f2018-01-31 13:24:59 +0100155
Ed Tanous1abe55e2018-09-05 08:30:59 -0700156 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 Tanouse0d918b2018-03-27 17:41:04 -0700163
Ed Tanous1abe55e2018-09-05 08:30:59 -0700164 res.jsonValue = Node::json;
165 res.end();
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +0100166 }
167
Ed Tanous1abe55e2018-09-05 08:30:59 -0700168 void doPost(crow::Response& res, const crow::Request& req,
169 const std::vector<std::string>& params) override
170 {
Ed Tanous820ce592018-10-04 15:54:21 -0700171 nlohmann::json postRequest;
172 if (!json_util::processJsonFromRequest(res, req, postRequest))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700173 {
174 res.end();
175 return;
176 }
Ed Tanousb9845d92018-07-24 14:38:06 -0700177
Ed Tanous820ce592018-10-04 15:54:21 -0700178 std::string username;
179 std::string password;
180 for (const auto& item : postRequest.items())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700181 {
Ed Tanous820ce592018-10-04 15:54:21 -0700182 const std::string* strVal =
183 item.value().get_ptr<const std::string*>();
184 if (item.key() == "UserName")
185 {
186 if (strVal == nullptr)
187 {
188 res.result(boost::beast::http::status::bad_request);
189 messages::addMessageToErrorJson(
190 res.jsonValue, messages::propertyValueTypeError(
191 item.value().dump(), item.key()));
192 continue;
193 }
194 username = *strVal;
195 }
196 else if (item.key() == "Password")
197 {
198 if (strVal == nullptr)
199 {
200 res.result(boost::beast::http::status::bad_request);
201 messages::addMessageToErrorJson(
202 res.jsonValue, messages::propertyValueTypeError(
203 item.value().dump(), item.key()));
204 continue;
205 }
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +0100206
Ed Tanous820ce592018-10-04 15:54:21 -0700207 password = *strVal;
208 }
209 else
210 {
211 res.result(boost::beast::http::status::bad_request);
212 messages::addMessageToErrorJson(
213 res.jsonValue, messages::propertyUnknown(item.key()));
214 continue;
215 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700216 }
217
Ed Tanous820ce592018-10-04 15:54:21 -0700218 if (password.empty() || username.empty() ||
219 res.result() != boost::beast::http::status::ok)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700220 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700221 if (username.empty())
222 {
Ed Tanous820ce592018-10-04 15:54:21 -0700223 res.result(boost::beast::http::status::bad_request);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700224 messages::addMessageToErrorJson(
Ed Tanous820ce592018-10-04 15:54:21 -0700225 res.jsonValue, messages::propertyMissing("UserName"));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700226 }
227
228 if (password.empty())
229 {
Ed Tanous820ce592018-10-04 15:54:21 -0700230 res.result(boost::beast::http::status::bad_request);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700231 messages::addMessageToErrorJson(
Ed Tanous820ce592018-10-04 15:54:21 -0700232 res.jsonValue, messages::propertyMissing("Password"));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700233 }
Ed Tanous820ce592018-10-04 15:54:21 -0700234 res.end();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700235
Ed Tanous820ce592018-10-04 15:54:21 -0700236 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700237 }
238
Ed Tanous1abe55e2018-09-05 08:30:59 -0700239 if (!pamAuthenticateUser(username, password))
240 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700241
Ed Tanous820ce592018-10-04 15:54:21 -0700242 res.result(boost::beast::http::status::unauthorized);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700243 messages::addMessageToErrorJson(
Ed Tanous820ce592018-10-04 15:54:21 -0700244 res.jsonValue,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700245 messages::resourceAtUriUnauthorized(
246 std::string(req.url), "Invalid username or password"));
Ed Tanous820ce592018-10-04 15:54:21 -0700247 res.end();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700248
Ed Tanous820ce592018-10-04 15:54:21 -0700249 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700250 }
251
Ed Tanous820ce592018-10-04 15:54:21 -0700252 // User is authenticated - create session
253 std::shared_ptr<crow::persistent_data::UserSession> session =
254 crow::persistent_data::SessionStore::getInstance()
255 .generateUserSession(username);
256 res.addHeader("X-Auth-Token", session->sessionToken);
257 res.addHeader("Location", "/redfish/v1/SessionService/Sessions/" +
258 session->uniqueId);
259 res.result(boost::beast::http::status::created);
260 memberSession.doGet(res, req, {session->uniqueId});
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +0100261 }
262
Ed Tanous1abe55e2018-09-05 08:30:59 -0700263 /**
264 * Member session to ensure consistency between collection's doPost and
265 * member's doGet, as they should return 100% matching data
266 */
267 Sessions memberSession;
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +0100268};
269
Ed Tanous1abe55e2018-09-05 08:30:59 -0700270class SessionService : public Node
271{
272 public:
273 SessionService(CrowApp& app) : Node(app, "/redfish/v1/SessionService/")
274 {
275 Node::json["@odata.type"] = "#SessionService.v1_0_2.SessionService";
276 Node::json["@odata.id"] = "/redfish/v1/SessionService/";
277 Node::json["@odata.context"] =
278 "/redfish/v1/$metadata#SessionService.SessionService";
279 Node::json["Name"] = "Session Service";
280 Node::json["Id"] = "SessionService";
281 Node::json["Description"] = "Session Service";
282 Node::json["SessionTimeout"] =
283 crow::persistent_data::SessionStore::getInstance()
284 .getTimeoutInSeconds();
285 Node::json["ServiceEnabled"] = true;
Ed Tanous3ebd75f2018-03-05 18:20:01 -0800286
Ed Tanous1abe55e2018-09-05 08:30:59 -0700287 entityPrivileges = {
288 {boost::beast::http::verb::get, {{"Login"}}},
289 {boost::beast::http::verb::head, {{"Login"}}},
290 {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
291 {boost::beast::http::verb::put, {{"ConfigureManager"}}},
292 {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
293 {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
294 }
Borawski.Lukasz5d27b852018-02-08 13:24:24 +0100295
Ed Tanous1abe55e2018-09-05 08:30:59 -0700296 private:
297 void doGet(crow::Response& res, const crow::Request& req,
298 const std::vector<std::string>& params) override
299 {
300 res.jsonValue = Node::json;
301 res.end();
302 }
Borawski.Lukasz5d27b852018-02-08 13:24:24 +0100303};
304
Ed Tanous1abe55e2018-09-05 08:30:59 -0700305} // namespace redfish