blob: 9e344d8f07256a9fbad7881c472975908aac4c93 [file] [log] [blame]
James Feist3909dc82020-04-03 10:58:55 -07001#pragma once
2
3#include "webroutes.hpp"
4
Ed Tanous04e438c2020-10-03 08:06:26 -07005#include <app.hpp>
James Feist3909dc82020-04-03 10:58:55 -07006#include <boost/algorithm/string/predicate.hpp>
7#include <boost/container/flat_set.hpp>
Ed Tanous04e438c2020-10-03 08:06:26 -07008#include <common.hpp>
9#include <http_request.hpp>
10#include <http_response.hpp>
James Feist3909dc82020-04-03 10:58:55 -070011#include <http_utility.hpp>
12#include <pam_authenticate.hpp>
James Feist3909dc82020-04-03 10:58:55 -070013
14#include <random>
Ed Tanousb5a76932020-09-29 16:16:58 -070015#include <utility>
James Feist3909dc82020-04-03 10:58:55 -070016
17namespace crow
18{
19
20namespace authorization
21{
22
23static void cleanupTempSession(Request& req)
24{
25 // TODO(ed) THis should really be handled by the persistent data
26 // middleware, but because it is upstream, it doesn't have access to the
27 // session information. Should the data middleware persist the current
28 // user session?
29 if (req.session != nullptr &&
30 req.session->persistence ==
Ed Tanous52cc1122020-07-18 13:51:21 -070031 persistent_data::PersistenceType::SINGLE_REQUEST)
James Feist3909dc82020-04-03 10:58:55 -070032 {
33 persistent_data::SessionStore::getInstance().removeSession(req.session);
34 }
35}
36
Alan Kuof16f6262020-12-08 19:29:59 +080037#ifdef BMCWEB_ENABLE_BASIC_AUTHENTICATION
Ed Tanous3174e4d2020-10-07 11:41:22 -070038static std::shared_ptr<persistent_data::UserSession>
Sunitha Harishc0ea7ae2020-10-30 02:37:30 -050039 performBasicAuth(const boost::asio::ip::address& clientIp,
Ed Tanous81ce6092020-12-17 16:54:55 +000040 std::string_view authHeader)
James Feist3909dc82020-04-03 10:58:55 -070041{
42 BMCWEB_LOG_DEBUG << "[AuthMiddleware] Basic authentication";
43
44 std::string authData;
Ed Tanous81ce6092020-12-17 16:54:55 +000045 std::string_view param = authHeader.substr(strlen("Basic "));
James Feist3909dc82020-04-03 10:58:55 -070046 if (!crow::utility::base64Decode(param, authData))
47 {
48 return nullptr;
49 }
50 std::size_t separator = authData.find(':');
51 if (separator == std::string::npos)
52 {
53 return nullptr;
54 }
55
56 std::string user = authData.substr(0, separator);
57 separator += 1;
58 if (separator > authData.size())
59 {
60 return nullptr;
61 }
62 std::string pass = authData.substr(separator);
63
64 BMCWEB_LOG_DEBUG << "[AuthMiddleware] Authenticating user: " << user;
Sunitha Harishc0ea7ae2020-10-30 02:37:30 -050065 BMCWEB_LOG_DEBUG << "[AuthMiddleware] User IPAddress: "
66 << clientIp.to_string();
James Feist3909dc82020-04-03 10:58:55 -070067
68 int pamrc = pamAuthenticateUser(user, pass);
69 bool isConfigureSelfOnly = pamrc == PAM_NEW_AUTHTOK_REQD;
70 if ((pamrc != PAM_SUCCESS) && !isConfigureSelfOnly)
71 {
72 return nullptr;
73 }
74
75 // TODO(ed) generateUserSession is a little expensive for basic
76 // auth, as it generates some random identifiers that will never be
77 // used. This should have a "fast" path for when user tokens aren't
78 // needed.
79 // This whole flow needs to be revisited anyway, as we can't be
80 // calling directly into pam for every request
Sunitha Harishd3239222021-02-24 15:33:29 +053081 std::string unsupportedClientId = "";
James Feist3909dc82020-04-03 10:58:55 -070082 return persistent_data::SessionStore::getInstance().generateUserSession(
Sunitha Harishd3239222021-02-24 15:33:29 +053083 user, clientIp.to_string(), unsupportedClientId,
84 persistent_data::PersistenceType::SINGLE_REQUEST, isConfigureSelfOnly);
James Feist3909dc82020-04-03 10:58:55 -070085}
Alan Kuof16f6262020-12-08 19:29:59 +080086#endif
James Feist3909dc82020-04-03 10:58:55 -070087
Alan Kuof16f6262020-12-08 19:29:59 +080088#ifdef BMCWEB_ENABLE_SESSION_AUTHENTICATION
Ed Tanous3174e4d2020-10-07 11:41:22 -070089static std::shared_ptr<persistent_data::UserSession>
Ed Tanous81ce6092020-12-17 16:54:55 +000090 performTokenAuth(std::string_view authHeader)
James Feist3909dc82020-04-03 10:58:55 -070091{
92 BMCWEB_LOG_DEBUG << "[AuthMiddleware] Token authentication";
93
Ed Tanous81ce6092020-12-17 16:54:55 +000094 std::string_view token = authHeader.substr(strlen("Token "));
James Feist3909dc82020-04-03 10:58:55 -070095 auto session =
96 persistent_data::SessionStore::getInstance().loginSessionByToken(token);
97 return session;
98}
Alan Kuof16f6262020-12-08 19:29:59 +080099#endif
James Feist3909dc82020-04-03 10:58:55 -0700100
Alan Kuof16f6262020-12-08 19:29:59 +0800101#ifdef BMCWEB_ENABLE_XTOKEN_AUTHENTICATION
Ed Tanous3174e4d2020-10-07 11:41:22 -0700102static std::shared_ptr<persistent_data::UserSession>
James Feist3909dc82020-04-03 10:58:55 -0700103 performXtokenAuth(const crow::Request& req)
104{
105 BMCWEB_LOG_DEBUG << "[AuthMiddleware] X-Auth-Token authentication";
106
107 std::string_view token = req.getHeaderValue("X-Auth-Token");
108 if (token.empty())
109 {
110 return nullptr;
111 }
112 auto session =
113 persistent_data::SessionStore::getInstance().loginSessionByToken(token);
114 return session;
115}
Alan Kuof16f6262020-12-08 19:29:59 +0800116#endif
James Feist3909dc82020-04-03 10:58:55 -0700117
Alan Kuof16f6262020-12-08 19:29:59 +0800118#ifdef BMCWEB_ENABLE_COOKIE_AUTHENTICATION
Ed Tanous3174e4d2020-10-07 11:41:22 -0700119static std::shared_ptr<persistent_data::UserSession>
James Feist3909dc82020-04-03 10:58:55 -0700120 performCookieAuth(const crow::Request& req)
121{
122 BMCWEB_LOG_DEBUG << "[AuthMiddleware] Cookie authentication";
123
124 std::string_view cookieValue = req.getHeaderValue("Cookie");
125 if (cookieValue.empty())
126 {
127 return nullptr;
128 }
129
130 auto startIndex = cookieValue.find("SESSION=");
131 if (startIndex == std::string::npos)
132 {
133 return nullptr;
134 }
135 startIndex += sizeof("SESSION=") - 1;
Ed Tanous81ce6092020-12-17 16:54:55 +0000136 auto endIndex = cookieValue.find(';', startIndex);
James Feist3909dc82020-04-03 10:58:55 -0700137 if (endIndex == std::string::npos)
138 {
139 endIndex = cookieValue.size();
140 }
141 std::string_view authKey =
142 cookieValue.substr(startIndex, endIndex - startIndex);
143
Ed Tanous3174e4d2020-10-07 11:41:22 -0700144 std::shared_ptr<persistent_data::UserSession> session =
James Feist3909dc82020-04-03 10:58:55 -0700145 persistent_data::SessionStore::getInstance().loginSessionByToken(
146 authKey);
147 if (session == nullptr)
148 {
149 return nullptr;
150 }
151#ifndef BMCWEB_INSECURE_DISABLE_CSRF_PREVENTION
152 // RFC7231 defines methods that need csrf protection
Ed Tanousb41187f2019-10-24 16:30:02 -0700153 if (req.method() != boost::beast::http::verb::get)
James Feist3909dc82020-04-03 10:58:55 -0700154 {
155 std::string_view csrf = req.getHeaderValue("X-XSRF-TOKEN");
156 // Make sure both tokens are filled
157 if (csrf.empty() || session->csrfToken.empty())
158 {
159 return nullptr;
160 }
161
Ed Tanous52cc1122020-07-18 13:51:21 -0700162 if (csrf.size() != persistent_data::sessionTokenSize)
James Feist3909dc82020-04-03 10:58:55 -0700163 {
164 return nullptr;
165 }
166 // Reject if csrf token not available
167 if (!crow::utility::constantTimeStringCompare(csrf, session->csrfToken))
168 {
169 return nullptr;
170 }
171 }
172#endif
173 return session;
174}
Alan Kuof16f6262020-12-08 19:29:59 +0800175#endif
James Feist3909dc82020-04-03 10:58:55 -0700176
Alexander Filippov96457602020-09-29 14:19:38 +0300177#ifdef BMCWEB_ENABLE_MUTUAL_TLS_AUTHENTICATION
Ed Tanous3174e4d2020-10-07 11:41:22 -0700178static std::shared_ptr<persistent_data::UserSession>
James Feist6964c982020-07-28 16:10:23 -0700179 performTLSAuth(const crow::Request& req, Response& res,
Ed Tanousb5a76932020-09-29 16:16:58 -0700180 const std::weak_ptr<persistent_data::UserSession>& session)
James Feist6964c982020-07-28 16:10:23 -0700181{
James Feist6964c982020-07-28 16:10:23 -0700182 if (auto sp = session.lock())
183 {
184 // set cookie only if this is req from the browser.
185 if (req.getHeaderValue("User-Agent").empty())
186 {
187 BMCWEB_LOG_DEBUG << " TLS session: " << sp->uniqueId
188 << " will be used for this request.";
189 return sp;
190 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700191 std::string_view cookieValue = req.getHeaderValue("Cookie");
192 if (cookieValue.empty() ||
193 cookieValue.find("SESSION=") == std::string::npos)
James Feist6964c982020-07-28 16:10:23 -0700194 {
Ed Tanous3174e4d2020-10-07 11:41:22 -0700195 // TODO: change this to not switch to cookie auth
196 res.addHeader("Set-Cookie", "XSRF-TOKEN=" + sp->csrfToken +
197 "; Secure\r\nSet-Cookie: SESSION=" +
198 sp->sessionToken +
199 "; Secure; HttpOnly\r\nSet-Cookie: "
200 "IsAuthenticated=true; Secure");
201 BMCWEB_LOG_DEBUG << " TLS session: " << sp->uniqueId
202 << " with cookie will be used for this request.";
203 return sp;
James Feist6964c982020-07-28 16:10:23 -0700204 }
205 }
James Feist6964c982020-07-28 16:10:23 -0700206 return nullptr;
207}
Alexander Filippov96457602020-09-29 14:19:38 +0300208#endif
James Feist6964c982020-07-28 16:10:23 -0700209
James Feist3909dc82020-04-03 10:58:55 -0700210// checks if request can be forwarded without authentication
211static bool isOnWhitelist(const crow::Request& req)
212{
213 // it's allowed to GET root node without authentication
Ed Tanousb41187f2019-10-24 16:30:02 -0700214 if (boost::beast::http::verb::get == req.method())
James Feist3909dc82020-04-03 10:58:55 -0700215 {
216 if (req.url == "/redfish/v1" || req.url == "/redfish/v1/" ||
217 req.url == "/redfish" || req.url == "/redfish/" ||
218 req.url == "/redfish/v1/odata" || req.url == "/redfish/v1/odata/")
219 {
220 return true;
221 }
Ed Tanousd4d25792020-09-29 15:15:03 -0700222 if (crow::webroutes::routes.find(std::string(req.url)) !=
223 crow::webroutes::routes.end())
James Feist3909dc82020-04-03 10:58:55 -0700224 {
225 return true;
226 }
227 }
228
229 // it's allowed to POST on session collection & login without
230 // authentication
Ed Tanousb41187f2019-10-24 16:30:02 -0700231 if (boost::beast::http::verb::post == req.method())
James Feist3909dc82020-04-03 10:58:55 -0700232 {
233 if ((req.url == "/redfish/v1/SessionService/Sessions") ||
234 (req.url == "/redfish/v1/SessionService/Sessions/") ||
235 (req.url == "/login"))
236 {
237 return true;
238 }
239 }
240
241 return false;
242}
243
Alexander Filippov96457602020-09-29 14:19:38 +0300244static void authenticate(
245 crow::Request& req, Response& res,
Ed Tanousf23b7292020-10-15 09:41:17 -0700246 [[maybe_unused]] const std::weak_ptr<persistent_data::UserSession>& session)
James Feist3909dc82020-04-03 10:58:55 -0700247{
248 if (isOnWhitelist(req))
249 {
250 return;
251 }
252
Ed Tanous52cc1122020-07-18 13:51:21 -0700253 const persistent_data::AuthConfigMethods& authMethodsConfig =
254 persistent_data::SessionStore::getInstance().getAuthMethodsConfig();
James Feist3909dc82020-04-03 10:58:55 -0700255
Alexander Filippov96457602020-09-29 14:19:38 +0300256#ifdef BMCWEB_ENABLE_MUTUAL_TLS_AUTHENTICATION
James Feist6964c982020-07-28 16:10:23 -0700257 if (req.session == nullptr && authMethodsConfig.tls)
258 {
Ed Tanousf23b7292020-10-15 09:41:17 -0700259 req.session = performTLSAuth(req, res, session);
James Feist6964c982020-07-28 16:10:23 -0700260 }
Alexander Filippov96457602020-09-29 14:19:38 +0300261#endif
Alan Kuof16f6262020-12-08 19:29:59 +0800262#ifdef BMCWEB_ENABLE_XTOKEN_AUTHENTICATION
James Feist3909dc82020-04-03 10:58:55 -0700263 if (req.session == nullptr && authMethodsConfig.xtoken)
264 {
265 req.session = performXtokenAuth(req);
266 }
Alan Kuof16f6262020-12-08 19:29:59 +0800267#endif
268#ifdef BMCWEB_ENABLE_COOKIE_AUTHENTICATION
James Feist3909dc82020-04-03 10:58:55 -0700269 if (req.session == nullptr && authMethodsConfig.cookie)
270 {
271 req.session = performCookieAuth(req);
272 }
Alan Kuof16f6262020-12-08 19:29:59 +0800273#endif
James Feist3909dc82020-04-03 10:58:55 -0700274 if (req.session == nullptr)
275 {
276 std::string_view authHeader = req.getHeaderValue("Authorization");
277 if (!authHeader.empty())
278 {
279 // Reject any kind of auth other than basic or token
280 if (boost::starts_with(authHeader, "Token ") &&
281 authMethodsConfig.sessionToken)
282 {
Alan Kuof16f6262020-12-08 19:29:59 +0800283#ifdef BMCWEB_ENABLE_SESSION_AUTHENTICATION
James Feist3909dc82020-04-03 10:58:55 -0700284 req.session = performTokenAuth(authHeader);
Alan Kuof16f6262020-12-08 19:29:59 +0800285#endif
James Feist3909dc82020-04-03 10:58:55 -0700286 }
287 else if (boost::starts_with(authHeader, "Basic ") &&
288 authMethodsConfig.basic)
289 {
Alan Kuof16f6262020-12-08 19:29:59 +0800290#ifdef BMCWEB_ENABLE_BASIC_AUTHENTICATION
Sunitha Harishc0ea7ae2020-10-30 02:37:30 -0500291 req.session = performBasicAuth(req.ipAddress, authHeader);
Alan Kuof16f6262020-12-08 19:29:59 +0800292#endif
James Feist3909dc82020-04-03 10:58:55 -0700293 }
294 }
295 }
296
297 if (req.session == nullptr)
298 {
299 BMCWEB_LOG_WARNING << "[AuthMiddleware] authorization failed";
300
301 // If it's a browser connecting, don't send the HTTP authenticate
302 // header, to avoid possible CSRF attacks with basic auth
303 if (http_helpers::requestPrefersHtml(req))
304 {
305 res.result(boost::beast::http::status::temporary_redirect);
306 res.addHeader("Location",
307 "/#/login?next=" + http_helpers::urlEncode(req.url));
308 }
309 else
310 {
311 res.result(boost::beast::http::status::unauthorized);
312 // only send the WWW-authenticate header if this isn't a xhr
313 // from the browser. most scripts,
314 if (req.getHeaderValue("User-Agent").empty())
315 {
316 res.addHeader("WWW-Authenticate", "Basic");
317 }
318 }
319
320 res.end();
321 return;
322 }
323}
324
325} // namespace authorization
326} // namespace crow