blob: 68449c0c3bdb6fd670c66cee77a1619397a4848d [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 {
Jason M. Billsf12894f2018-10-09 12:45:45 -070057 messages::resourceNotFound(res, "Session", params[0]);
Ed Tanous1abe55e2018-09-05 08:30:59 -070058 res.end();
59 return;
60 }
Kowalski, Kamilf4c4dcf2018-01-29 14:55:35 +010061
Ed Tanous1abe55e2018-09-05 08:30:59 -070062 Node::json["Id"] = session->uniqueId;
63 Node::json["UserName"] = session->username;
64 Node::json["@odata.id"] =
65 "/redfish/v1/SessionService/Sessions/" + session->uniqueId;
Ed Tanouse0d918b2018-03-27 17:41:04 -070066
Ed Tanous1abe55e2018-09-05 08:30:59 -070067 res.jsonValue = Node::json;
68 res.end();
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +010069 }
70
Ed Tanous1abe55e2018-09-05 08:30:59 -070071 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, Kamil2b7981f2018-01-31 13:24:59 +010080
Jason M. Billsf12894f2018-10-09 12:45:45 -070081 messages::generalError(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -070082 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. Billsf12894f2018-10-09 12:45:45 -070092 messages::resourceNotFound(res, "Session", params[0]);
Ed Tanous1abe55e2018-09-05 08:30:59 -070093 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, Kamil2b7981f2018-01-31 13:24:59 +0100102 }
103
Ed Tanous1abe55e2018-09-05 08:30:59 -0700104 /**
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, Kamil2b7981f2018-01-31 13:24:59 +0100111};
112
Ed Tanous1abe55e2018-09-05 08:30:59 -0700113class 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 Tanous3ebd75f2018-03-05 18:20:01 -0800127
Ed Tanous1abe55e2018-09-05 08:30:59 -0700128 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, Kamil2b7981f2018-01-31 13:24:59 +0100135 }
136
Ed Tanous1abe55e2018-09-05 08:30:59 -0700137 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, Kamil2b7981f2018-01-31 13:24:59 +0100144
Ed Tanous1abe55e2018-09-05 08:30:59 -0700145 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 Tanouse0d918b2018-03-27 17:41:04 -0700152
Ed Tanous1abe55e2018-09-05 08:30:59 -0700153 res.jsonValue = Node::json;
154 res.end();
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +0100155 }
156
Ed Tanous1abe55e2018-09-05 08:30:59 -0700157 void doPost(crow::Response& res, const crow::Request& req,
158 const std::vector<std::string>& params) override
159 {
Ed Tanous9712f8a2018-09-21 13:38:49 -0700160 std::string username;
161 std::string password;
162 if (!json_util::readJson(req, res, "UserName", username, "Password",
163 password))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700164 {
165 res.end();
166 return;
167 }
Ed Tanousb9845d92018-07-24 14:38:06 -0700168
Ed Tanous820ce592018-10-04 15:54:21 -0700169 if (password.empty() || username.empty() ||
170 res.result() != boost::beast::http::status::ok)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700171 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700172 if (username.empty())
173 {
Jason M. Billsa08b46c2018-11-06 15:01:08 -0800174 messages::propertyMissing(res, "UserName");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700175 }
176
177 if (password.empty())
178 {
Jason M. Billsa08b46c2018-11-06 15:01:08 -0800179 messages::propertyMissing(res, "Password");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700180 }
Ed Tanous820ce592018-10-04 15:54:21 -0700181 res.end();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700182
Ed Tanous820ce592018-10-04 15:54:21 -0700183 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700184 }
185
Ed Tanous1abe55e2018-09-05 08:30:59 -0700186 if (!pamAuthenticateUser(username, password))
187 {
Jason M. Billsf12894f2018-10-09 12:45:45 -0700188 messages::resourceAtUriUnauthorized(res, std::string(req.url),
189 "Invalid username or password");
Ed Tanous820ce592018-10-04 15:54:21 -0700190 res.end();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700191
Ed Tanous820ce592018-10-04 15:54:21 -0700192 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700193 }
194
Ed Tanous820ce592018-10-04 15:54:21 -0700195 // 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, Kamil2b7981f2018-01-31 13:24:59 +0100204 }
205
Ed Tanous1abe55e2018-09-05 08:30:59 -0700206 /**
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, Kamil2b7981f2018-01-31 13:24:59 +0100211};
212
Ed Tanous1abe55e2018-09-05 08:30:59 -0700213class 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 Tanous3ebd75f2018-03-05 18:20:01 -0800229
Ed Tanous1abe55e2018-09-05 08:30:59 -0700230 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.Lukasz5d27b852018-02-08 13:24:24 +0100238
Ed Tanous1abe55e2018-09-05 08:30:59 -0700239 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.Lukasz5d27b852018-02-08 13:24:24 +0100246};
247
Ed Tanous1abe55e2018-09-05 08:30:59 -0700248} // namespace redfish