blob: addb28701b81c234b75c70704a2cf1dd81b9b444 [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 {
Ed Tanous1abe55e2018-09-05 08:30:59 -070033 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, Kamil2b7981f2018-01-31 13:24:59 +010040 }
41
Ed Tanous1abe55e2018-09-05 08:30:59 -070042 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, Kamil2b7981f2018-01-31 13:24:59 +010049
Ed Tanous1abe55e2018-09-05 08:30:59 -070050 if (session == nullptr)
51 {
Jason M. Billsf12894f2018-10-09 12:45:45 -070052 messages::resourceNotFound(res, "Session", params[0]);
Ed Tanous1abe55e2018-09-05 08:30:59 -070053 res.end();
54 return;
55 }
Kowalski, Kamilf4c4dcf2018-01-29 14:55:35 +010056
Ed Tanous0f74e642018-11-12 15:17:05 -080057 res.jsonValue["Id"] = session->uniqueId;
58 res.jsonValue["UserName"] = session->username;
59 res.jsonValue["@odata.id"] =
Ed Tanous1abe55e2018-09-05 08:30:59 -070060 "/redfish/v1/SessionService/Sessions/" + session->uniqueId;
Ed Tanous0f74e642018-11-12 15:17:05 -080061 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 Tanouse0d918b2018-03-27 17:41:04 -070066
Ed Tanous1abe55e2018-09-05 08:30:59 -070067 res.end();
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +010068 }
69
Ed Tanous1abe55e2018-09-05 08:30:59 -070070 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, Kamil2b7981f2018-01-31 13:24:59 +010079
Jason M. Billsf12894f2018-10-09 12:45:45 -070080 messages::generalError(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -070081 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. Billsf12894f2018-10-09 12:45:45 -070091 messages::resourceNotFound(res, "Session", params[0]);
Ed Tanous1abe55e2018-09-05 08:30:59 -070092 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, Kamil2b7981f2018-01-31 13:24:59 +0100101 }
102
Ed Tanous1abe55e2018-09-05 08:30:59 -0700103 /**
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, Kamil2b7981f2018-01-31 13:24:59 +0100110};
111
Ed Tanous1abe55e2018-09-05 08:30:59 -0700112class SessionCollection : public Node
113{
114 public:
115 SessionCollection(CrowApp& app) :
116 Node(app, "/redfish/v1/SessionService/Sessions/"), memberSession(app)
117 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700118 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, Kamil2b7981f2018-01-31 13:24:59 +0100125 }
126
Ed Tanous1abe55e2018-09-05 08:30:59 -0700127 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, Kamil2b7981f2018-01-31 13:24:59 +0100134
Ed Tanous0f74e642018-11-12 15:17:05 -0800135 res.jsonValue["Members@odata.count"] = sessionIds.size();
136 res.jsonValue["Members"] = nlohmann::json::array();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700137 for (const std::string* uid : sessionIds)
138 {
Ed Tanous0f74e642018-11-12 15:17:05 -0800139 res.jsonValue["Members"].push_back(
Ed Tanous1abe55e2018-09-05 08:30:59 -0700140 {{"@odata.id", "/redfish/v1/SessionService/Sessions/" + *uid}});
141 }
Ed Tanous0f74e642018-11-12 15:17:05 -0800142 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";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700148 res.end();
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +0100149 }
150
Ed Tanous1abe55e2018-09-05 08:30:59 -0700151 void doPost(crow::Response& res, const crow::Request& req,
152 const std::vector<std::string>& params) override
153 {
Ed Tanous9712f8a2018-09-21 13:38:49 -0700154 std::string username;
155 std::string password;
156 if (!json_util::readJson(req, res, "UserName", username, "Password",
157 password))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700158 {
159 res.end();
160 return;
161 }
Ed Tanousb9845d92018-07-24 14:38:06 -0700162
Ed Tanous820ce592018-10-04 15:54:21 -0700163 if (password.empty() || username.empty() ||
164 res.result() != boost::beast::http::status::ok)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700165 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700166 if (username.empty())
167 {
Jason M. Billsa08b46c2018-11-06 15:01:08 -0800168 messages::propertyMissing(res, "UserName");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700169 }
170
171 if (password.empty())
172 {
Jason M. Billsa08b46c2018-11-06 15:01:08 -0800173 messages::propertyMissing(res, "Password");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700174 }
Ed Tanous820ce592018-10-04 15:54:21 -0700175 res.end();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700176
Ed Tanous820ce592018-10-04 15:54:21 -0700177 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700178 }
179
Ed Tanous1abe55e2018-09-05 08:30:59 -0700180 if (!pamAuthenticateUser(username, password))
181 {
Jason M. Billsf12894f2018-10-09 12:45:45 -0700182 messages::resourceAtUriUnauthorized(res, std::string(req.url),
183 "Invalid username or password");
Ed Tanous820ce592018-10-04 15:54:21 -0700184 res.end();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700185
Ed Tanous820ce592018-10-04 15:54:21 -0700186 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700187 }
188
Ed Tanous820ce592018-10-04 15:54:21 -0700189 // User is authenticated - create session
190 std::shared_ptr<crow::persistent_data::UserSession> session =
191 crow::persistent_data::SessionStore::getInstance()
192 .generateUserSession(username);
193 res.addHeader("X-Auth-Token", session->sessionToken);
194 res.addHeader("Location", "/redfish/v1/SessionService/Sessions/" +
195 session->uniqueId);
196 res.result(boost::beast::http::status::created);
197 memberSession.doGet(res, req, {session->uniqueId});
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +0100198 }
199
Ed Tanous1abe55e2018-09-05 08:30:59 -0700200 /**
201 * Member session to ensure consistency between collection's doPost and
202 * member's doGet, as they should return 100% matching data
203 */
204 Sessions memberSession;
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +0100205};
206
Ed Tanous1abe55e2018-09-05 08:30:59 -0700207class SessionService : public Node
208{
209 public:
210 SessionService(CrowApp& app) : Node(app, "/redfish/v1/SessionService/")
211 {
Ed Tanous3ebd75f2018-03-05 18:20:01 -0800212
Ed Tanous1abe55e2018-09-05 08:30:59 -0700213 entityPrivileges = {
214 {boost::beast::http::verb::get, {{"Login"}}},
215 {boost::beast::http::verb::head, {{"Login"}}},
216 {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
217 {boost::beast::http::verb::put, {{"ConfigureManager"}}},
218 {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
219 {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
220 }
Borawski.Lukasz5d27b852018-02-08 13:24:24 +0100221
Ed Tanous1abe55e2018-09-05 08:30:59 -0700222 private:
223 void doGet(crow::Response& res, const crow::Request& req,
224 const std::vector<std::string>& params) override
225 {
Ed Tanous0f74e642018-11-12 15:17:05 -0800226 res.jsonValue["@odata.type"] = "#SessionService.v1_0_2.SessionService";
227 res.jsonValue["@odata.id"] = "/redfish/v1/SessionService/";
228 res.jsonValue["@odata.context"] =
229 "/redfish/v1/$metadata#SessionService.SessionService";
230 res.jsonValue["Name"] = "Session Service";
231 res.jsonValue["Id"] = "SessionService";
232 res.jsonValue["Description"] = "Session Service";
233 res.jsonValue["SessionTimeout"] =
234 crow::persistent_data::SessionStore::getInstance()
235 .getTimeoutInSeconds();
236 res.jsonValue["ServiceEnabled"] = true;
237
Ed Tanous1abe55e2018-09-05 08:30:59 -0700238 res.end();
239 }
Borawski.Lukasz5d27b852018-02-08 13:24:24 +0100240};
241
Ed Tanous1abe55e2018-09-05 08:30:59 -0700242} // namespace redfish