blob: 6a9e6ad69b1c14244e173fbcbbf495f81a2e25de [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/container/flat_set.hpp>
Ed Tanous04e438c2020-10-03 08:06:26 -07007#include <common.hpp>
Ed Tanousd4b6c662021-03-10 13:29:30 -08008#include <forward_unauthorized.hpp>
Ed Tanous04e438c2020-10-03 08:06:26 -07009#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
Nan Zhoud055a342022-05-25 01:15:34 +000020namespace authentication
James Feist3909dc82020-04-03 10:58:55 -070021{
22
Ed Tanous02cad962022-06-30 16:50:15 -070023static void cleanupTempSession(const Request& req)
James Feist3909dc82020-04-03 10:58:55 -070024{
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
Ed Tanous11ba3972022-07-11 09:50:41 -070044 if (!authHeader.starts_with("Basic "))
John Edward Broadbent97a056a2021-09-10 14:56:19 -070045 {
46 return nullptr;
47 }
48
Ed Tanous81ce6092020-12-17 16:54:55 +000049 std::string_view param = authHeader.substr(strlen("Basic "));
John Edward Broadbent97a056a2021-09-10 14:56:19 -070050 std::string authData;
51
James Feist3909dc82020-04-03 10:58:55 -070052 if (!crow::utility::base64Decode(param, authData))
53 {
54 return nullptr;
55 }
56 std::size_t separator = authData.find(':');
57 if (separator == std::string::npos)
58 {
59 return nullptr;
60 }
61
62 std::string user = authData.substr(0, separator);
63 separator += 1;
64 if (separator > authData.size())
65 {
66 return nullptr;
67 }
68 std::string pass = authData.substr(separator);
69
70 BMCWEB_LOG_DEBUG << "[AuthMiddleware] Authenticating user: " << user;
Sunitha Harishc0ea7ae2020-10-30 02:37:30 -050071 BMCWEB_LOG_DEBUG << "[AuthMiddleware] User IPAddress: "
72 << clientIp.to_string();
James Feist3909dc82020-04-03 10:58:55 -070073
74 int pamrc = pamAuthenticateUser(user, pass);
75 bool isConfigureSelfOnly = pamrc == PAM_NEW_AUTHTOK_REQD;
76 if ((pamrc != PAM_SUCCESS) && !isConfigureSelfOnly)
77 {
78 return nullptr;
79 }
80
81 // TODO(ed) generateUserSession is a little expensive for basic
82 // auth, as it generates some random identifiers that will never be
83 // used. This should have a "fast" path for when user tokens aren't
84 // needed.
85 // This whole flow needs to be revisited anyway, as we can't be
86 // calling directly into pam for every request
Ed Tanouse05aec52022-01-25 10:28:56 -080087 std::string unsupportedClientId;
James Feist3909dc82020-04-03 10:58:55 -070088 return persistent_data::SessionStore::getInstance().generateUserSession(
Jiaqing Zhao41d61c82021-12-07 13:21:47 +080089 user, clientIp, unsupportedClientId,
Sunitha Harishd3239222021-02-24 15:33:29 +053090 persistent_data::PersistenceType::SINGLE_REQUEST, isConfigureSelfOnly);
James Feist3909dc82020-04-03 10:58:55 -070091}
Alan Kuof16f6262020-12-08 19:29:59 +080092#endif
James Feist3909dc82020-04-03 10:58:55 -070093
Alan Kuof16f6262020-12-08 19:29:59 +080094#ifdef BMCWEB_ENABLE_SESSION_AUTHENTICATION
Ed Tanous3174e4d2020-10-07 11:41:22 -070095static std::shared_ptr<persistent_data::UserSession>
Ed Tanous81ce6092020-12-17 16:54:55 +000096 performTokenAuth(std::string_view authHeader)
James Feist3909dc82020-04-03 10:58:55 -070097{
98 BMCWEB_LOG_DEBUG << "[AuthMiddleware] Token authentication";
Ed Tanous11ba3972022-07-11 09:50:41 -070099 if (!authHeader.starts_with("Token "))
John Edward Broadbent97a056a2021-09-10 14:56:19 -0700100 {
101 return nullptr;
102 }
Ed Tanous81ce6092020-12-17 16:54:55 +0000103 std::string_view token = authHeader.substr(strlen("Token "));
John Edward Broadbent59b98b22021-07-13 15:36:32 -0700104 auto sessionOut =
James Feist3909dc82020-04-03 10:58:55 -0700105 persistent_data::SessionStore::getInstance().loginSessionByToken(token);
John Edward Broadbent59b98b22021-07-13 15:36:32 -0700106 return sessionOut;
James Feist3909dc82020-04-03 10:58:55 -0700107}
Alan Kuof16f6262020-12-08 19:29:59 +0800108#endif
James Feist3909dc82020-04-03 10:58:55 -0700109
Alan Kuof16f6262020-12-08 19:29:59 +0800110#ifdef BMCWEB_ENABLE_XTOKEN_AUTHENTICATION
Ed Tanous3174e4d2020-10-07 11:41:22 -0700111static std::shared_ptr<persistent_data::UserSession>
John Edward Broadbent59b98b22021-07-13 15:36:32 -0700112 performXtokenAuth(const boost::beast::http::header<true>& reqHeader)
James Feist3909dc82020-04-03 10:58:55 -0700113{
114 BMCWEB_LOG_DEBUG << "[AuthMiddleware] X-Auth-Token authentication";
115
John Edward Broadbent59b98b22021-07-13 15:36:32 -0700116 std::string_view token = reqHeader["X-Auth-Token"];
James Feist3909dc82020-04-03 10:58:55 -0700117 if (token.empty())
118 {
119 return nullptr;
120 }
John Edward Broadbent59b98b22021-07-13 15:36:32 -0700121 auto sessionOut =
James Feist3909dc82020-04-03 10:58:55 -0700122 persistent_data::SessionStore::getInstance().loginSessionByToken(token);
John Edward Broadbent59b98b22021-07-13 15:36:32 -0700123 return sessionOut;
James Feist3909dc82020-04-03 10:58:55 -0700124}
Alan Kuof16f6262020-12-08 19:29:59 +0800125#endif
James Feist3909dc82020-04-03 10:58:55 -0700126
Alan Kuof16f6262020-12-08 19:29:59 +0800127#ifdef BMCWEB_ENABLE_COOKIE_AUTHENTICATION
Ed Tanous3174e4d2020-10-07 11:41:22 -0700128static std::shared_ptr<persistent_data::UserSession>
John Edward Broadbent59b98b22021-07-13 15:36:32 -0700129 performCookieAuth(boost::beast::http::verb method,
130 const boost::beast::http::header<true>& reqHeader)
James Feist3909dc82020-04-03 10:58:55 -0700131{
132 BMCWEB_LOG_DEBUG << "[AuthMiddleware] Cookie authentication";
133
John Edward Broadbent59b98b22021-07-13 15:36:32 -0700134 std::string_view cookieValue = reqHeader["Cookie"];
James Feist3909dc82020-04-03 10:58:55 -0700135 if (cookieValue.empty())
136 {
137 return nullptr;
138 }
139
140 auto startIndex = cookieValue.find("SESSION=");
141 if (startIndex == std::string::npos)
142 {
143 return nullptr;
144 }
145 startIndex += sizeof("SESSION=") - 1;
Ed Tanous81ce6092020-12-17 16:54:55 +0000146 auto endIndex = cookieValue.find(';', startIndex);
James Feist3909dc82020-04-03 10:58:55 -0700147 if (endIndex == std::string::npos)
148 {
149 endIndex = cookieValue.size();
150 }
151 std::string_view authKey =
152 cookieValue.substr(startIndex, endIndex - startIndex);
153
John Edward Broadbent59b98b22021-07-13 15:36:32 -0700154 std::shared_ptr<persistent_data::UserSession> sessionOut =
James Feist3909dc82020-04-03 10:58:55 -0700155 persistent_data::SessionStore::getInstance().loginSessionByToken(
156 authKey);
John Edward Broadbent59b98b22021-07-13 15:36:32 -0700157 if (sessionOut == nullptr)
James Feist3909dc82020-04-03 10:58:55 -0700158 {
159 return nullptr;
160 }
161#ifndef BMCWEB_INSECURE_DISABLE_CSRF_PREVENTION
162 // RFC7231 defines methods that need csrf protection
John Edward Broadbent59b98b22021-07-13 15:36:32 -0700163 if (method != boost::beast::http::verb::get)
James Feist3909dc82020-04-03 10:58:55 -0700164 {
John Edward Broadbent59b98b22021-07-13 15:36:32 -0700165 std::string_view csrf = reqHeader["X-XSRF-TOKEN"];
James Feist3909dc82020-04-03 10:58:55 -0700166 // Make sure both tokens are filled
John Edward Broadbent59b98b22021-07-13 15:36:32 -0700167 if (csrf.empty() || sessionOut->csrfToken.empty())
James Feist3909dc82020-04-03 10:58:55 -0700168 {
169 return nullptr;
170 }
171
Ed Tanous52cc1122020-07-18 13:51:21 -0700172 if (csrf.size() != persistent_data::sessionTokenSize)
James Feist3909dc82020-04-03 10:58:55 -0700173 {
174 return nullptr;
175 }
176 // Reject if csrf token not available
John Edward Broadbent59b98b22021-07-13 15:36:32 -0700177 if (!crow::utility::constantTimeStringCompare(csrf,
178 sessionOut->csrfToken))
James Feist3909dc82020-04-03 10:58:55 -0700179 {
180 return nullptr;
181 }
182 }
183#endif
John Edward Broadbent59b98b22021-07-13 15:36:32 -0700184 return sessionOut;
James Feist3909dc82020-04-03 10:58:55 -0700185}
Alan Kuof16f6262020-12-08 19:29:59 +0800186#endif
James Feist3909dc82020-04-03 10:58:55 -0700187
Alexander Filippov96457602020-09-29 14:19:38 +0300188#ifdef BMCWEB_ENABLE_MUTUAL_TLS_AUTHENTICATION
Ed Tanous3174e4d2020-10-07 11:41:22 -0700189static std::shared_ptr<persistent_data::UserSession>
John Edward Broadbent59b98b22021-07-13 15:36:32 -0700190 performTLSAuth(Response& res,
191 const boost::beast::http::header<true>& reqHeader,
Ed Tanousb5a76932020-09-29 16:16:58 -0700192 const std::weak_ptr<persistent_data::UserSession>& session)
James Feist6964c982020-07-28 16:10:23 -0700193{
James Feist6964c982020-07-28 16:10:23 -0700194 if (auto sp = session.lock())
195 {
196 // set cookie only if this is req from the browser.
John Edward Broadbent59b98b22021-07-13 15:36:32 -0700197 if (reqHeader["User-Agent"].empty())
James Feist6964c982020-07-28 16:10:23 -0700198 {
199 BMCWEB_LOG_DEBUG << " TLS session: " << sp->uniqueId
200 << " will be used for this request.";
201 return sp;
202 }
John Edward Broadbent59b98b22021-07-13 15:36:32 -0700203 std::string_view cookieValue = reqHeader["Cookie"];
Ed Tanous3174e4d2020-10-07 11:41:22 -0700204 if (cookieValue.empty() ||
205 cookieValue.find("SESSION=") == std::string::npos)
James Feist6964c982020-07-28 16:10:23 -0700206 {
Ed Tanous3174e4d2020-10-07 11:41:22 -0700207 // TODO: change this to not switch to cookie auth
Gunnar Mills636be392021-03-15 12:47:07 -0500208 res.addHeader(
209 "Set-Cookie",
210 "XSRF-TOKEN=" + sp->csrfToken +
211 "; SameSite=Strict; Secure\r\nSet-Cookie: SESSION=" +
212 sp->sessionToken +
213 "; SameSite=Strict; Secure; HttpOnly\r\nSet-Cookie: "
214 "IsAuthenticated=true; Secure");
Ed Tanous3174e4d2020-10-07 11:41:22 -0700215 BMCWEB_LOG_DEBUG << " TLS session: " << sp->uniqueId
216 << " with cookie will be used for this request.";
217 return sp;
James Feist6964c982020-07-28 16:10:23 -0700218 }
219 }
James Feist6964c982020-07-28 16:10:23 -0700220 return nullptr;
221}
Alexander Filippov96457602020-09-29 14:19:38 +0300222#endif
James Feist6964c982020-07-28 16:10:23 -0700223
James Feist3909dc82020-04-03 10:58:55 -0700224// checks if request can be forwarded without authentication
Nan Zhou8682c5a2021-11-13 11:00:07 -0800225[[maybe_unused]] static bool isOnAllowlist(std::string_view url,
226 boost::beast::http::verb method)
James Feist3909dc82020-04-03 10:58:55 -0700227{
John Edward Broadbent59b98b22021-07-13 15:36:32 -0700228 if (boost::beast::http::verb::get == method)
James Feist3909dc82020-04-03 10:58:55 -0700229 {
John Edward Broadbent59b98b22021-07-13 15:36:32 -0700230 if (url == "/redfish/v1" || url == "/redfish/v1/" ||
231 url == "/redfish" || url == "/redfish/" ||
232 url == "/redfish/v1/odata" || url == "/redfish/v1/odata/")
James Feist3909dc82020-04-03 10:58:55 -0700233 {
234 return true;
235 }
John Edward Broadbent59b98b22021-07-13 15:36:32 -0700236 if (crow::webroutes::routes.find(std::string(url)) !=
Ed Tanousd4d25792020-09-29 15:15:03 -0700237 crow::webroutes::routes.end())
James Feist3909dc82020-04-03 10:58:55 -0700238 {
239 return true;
240 }
241 }
242
243 // it's allowed to POST on session collection & login without
244 // authentication
John Edward Broadbent59b98b22021-07-13 15:36:32 -0700245 if (boost::beast::http::verb::post == method)
James Feist3909dc82020-04-03 10:58:55 -0700246 {
John Edward Broadbent59b98b22021-07-13 15:36:32 -0700247 if ((url == "/redfish/v1/SessionService/Sessions") ||
248 (url == "/redfish/v1/SessionService/Sessions/") ||
Ed Tanouse76cd862022-03-14 09:12:00 -0700249 (url == "/redfish/v1/SessionService/Sessions/Members") ||
250 (url == "/redfish/v1/SessionService/Sessions/Members/") ||
John Edward Broadbent59b98b22021-07-13 15:36:32 -0700251 (url == "/login"))
James Feist3909dc82020-04-03 10:58:55 -0700252 {
253 return true;
254 }
255 }
256
257 return false;
258}
259
Nan Zhou8682c5a2021-11-13 11:00:07 -0800260[[maybe_unused]] static std::shared_ptr<persistent_data::UserSession>
261 authenticate(
Ed Tanous02cad962022-06-30 16:50:15 -0700262 const boost::asio::ip::address& ipAddress [[maybe_unused]],
Nan Zhou3acced22022-07-12 23:21:02 +0000263 Response& res [[maybe_unused]],
264 boost::beast::http::verb method [[maybe_unused]],
Nan Zhou8682c5a2021-11-13 11:00:07 -0800265 const boost::beast::http::header<true>& reqHeader,
266 [[maybe_unused]] const std::shared_ptr<persistent_data::UserSession>&
267 session)
James Feist3909dc82020-04-03 10:58:55 -0700268{
Ed Tanous52cc1122020-07-18 13:51:21 -0700269 const persistent_data::AuthConfigMethods& authMethodsConfig =
270 persistent_data::SessionStore::getInstance().getAuthMethodsConfig();
James Feist3909dc82020-04-03 10:58:55 -0700271
John Edward Broadbent59b98b22021-07-13 15:36:32 -0700272 std::shared_ptr<persistent_data::UserSession> sessionOut = nullptr;
Alexander Filippov96457602020-09-29 14:19:38 +0300273#ifdef BMCWEB_ENABLE_MUTUAL_TLS_AUTHENTICATION
John Edward Broadbent59b98b22021-07-13 15:36:32 -0700274 if (authMethodsConfig.tls)
James Feist6964c982020-07-28 16:10:23 -0700275 {
John Edward Broadbent59b98b22021-07-13 15:36:32 -0700276 sessionOut = performTLSAuth(res, reqHeader, session);
James Feist6964c982020-07-28 16:10:23 -0700277 }
Alexander Filippov96457602020-09-29 14:19:38 +0300278#endif
Alan Kuof16f6262020-12-08 19:29:59 +0800279#ifdef BMCWEB_ENABLE_XTOKEN_AUTHENTICATION
John Edward Broadbent59b98b22021-07-13 15:36:32 -0700280 if (sessionOut == nullptr && authMethodsConfig.xtoken)
James Feist3909dc82020-04-03 10:58:55 -0700281 {
John Edward Broadbent59b98b22021-07-13 15:36:32 -0700282 sessionOut = performXtokenAuth(reqHeader);
James Feist3909dc82020-04-03 10:58:55 -0700283 }
Alan Kuof16f6262020-12-08 19:29:59 +0800284#endif
285#ifdef BMCWEB_ENABLE_COOKIE_AUTHENTICATION
John Edward Broadbent59b98b22021-07-13 15:36:32 -0700286 if (sessionOut == nullptr && authMethodsConfig.cookie)
James Feist3909dc82020-04-03 10:58:55 -0700287 {
John Edward Broadbent59b98b22021-07-13 15:36:32 -0700288 sessionOut = performCookieAuth(method, reqHeader);
James Feist3909dc82020-04-03 10:58:55 -0700289 }
Alan Kuof16f6262020-12-08 19:29:59 +0800290#endif
John Edward Broadbent59b98b22021-07-13 15:36:32 -0700291 std::string_view authHeader = reqHeader["Authorization"];
Andrew Geissler1c801e12021-10-08 14:36:01 -0500292 BMCWEB_LOG_DEBUG << "authHeader=" << authHeader;
John Edward Broadbent59b98b22021-07-13 15:36:32 -0700293
John Edward Broadbent97a056a2021-09-10 14:56:19 -0700294 if (sessionOut == nullptr && authMethodsConfig.sessionToken)
James Feist3909dc82020-04-03 10:58:55 -0700295 {
Alan Kuof16f6262020-12-08 19:29:59 +0800296#ifdef BMCWEB_ENABLE_SESSION_AUTHENTICATION
John Edward Broadbent97a056a2021-09-10 14:56:19 -0700297 sessionOut = performTokenAuth(authHeader);
Alan Kuof16f6262020-12-08 19:29:59 +0800298#endif
James Feist3909dc82020-04-03 10:58:55 -0700299 }
John Edward Broadbent97a056a2021-09-10 14:56:19 -0700300 if (sessionOut == nullptr && authMethodsConfig.basic)
301 {
302#ifdef BMCWEB_ENABLE_BASIC_AUTHENTICATION
303 sessionOut = performBasicAuth(ipAddress, authHeader);
304#endif
305 }
306 if (sessionOut != nullptr)
307 {
308 return sessionOut;
309 }
310
John Edward Broadbent59b98b22021-07-13 15:36:32 -0700311 return nullptr;
James Feist3909dc82020-04-03 10:58:55 -0700312}
313
Nan Zhoud055a342022-05-25 01:15:34 +0000314} // namespace authentication
James Feist3909dc82020-04-03 10:58:55 -0700315} // namespace crow