James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 3 | #include "common.hpp" |
| 4 | #include "forward_unauthorized.hpp" |
| 5 | #include "http_request.hpp" |
| 6 | #include "http_response.hpp" |
| 7 | #include "http_utility.hpp" |
| 8 | #include "pam_authenticate.hpp" |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 9 | #include "webroutes.hpp" |
| 10 | |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 11 | #include <boost/container/flat_set.hpp> |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 12 | |
| 13 | #include <random> |
Ed Tanous | b5a7693 | 2020-09-29 16:16:58 -0700 | [diff] [blame] | 14 | #include <utility> |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 15 | |
| 16 | namespace crow |
| 17 | { |
| 18 | |
Nan Zhou | d055a34 | 2022-05-25 01:15:34 +0000 | [diff] [blame] | 19 | namespace authentication |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 20 | { |
| 21 | |
Ed Tanous | b1d736f | 2023-02-10 12:24:00 -0800 | [diff] [blame] | 22 | inline void cleanupTempSession(const Request& req) |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 23 | { |
| 24 | // TODO(ed) THis should really be handled by the persistent data |
| 25 | // middleware, but because it is upstream, it doesn't have access to the |
| 26 | // session information. Should the data middleware persist the current |
| 27 | // user session? |
| 28 | if (req.session != nullptr && |
| 29 | req.session->persistence == |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 30 | persistent_data::PersistenceType::SINGLE_REQUEST) |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 31 | { |
| 32 | persistent_data::SessionStore::getInstance().removeSession(req.session); |
| 33 | } |
| 34 | } |
| 35 | |
Alan Kuo | f16f626 | 2020-12-08 19:29:59 +0800 | [diff] [blame] | 36 | #ifdef BMCWEB_ENABLE_BASIC_AUTHENTICATION |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 37 | static std::shared_ptr<persistent_data::UserSession> |
Sunitha Harish | c0ea7ae | 2020-10-30 02:37:30 -0500 | [diff] [blame] | 38 | performBasicAuth(const boost::asio::ip::address& clientIp, |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 39 | std::string_view authHeader) |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 40 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 41 | BMCWEB_LOG_DEBUG("[AuthMiddleware] Basic authentication"); |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 42 | |
Ed Tanous | 11ba397 | 2022-07-11 09:50:41 -0700 | [diff] [blame] | 43 | if (!authHeader.starts_with("Basic ")) |
John Edward Broadbent | 97a056a | 2021-09-10 14:56:19 -0700 | [diff] [blame] | 44 | { |
| 45 | return nullptr; |
| 46 | } |
| 47 | |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 48 | std::string_view param = authHeader.substr(strlen("Basic ")); |
John Edward Broadbent | 97a056a | 2021-09-10 14:56:19 -0700 | [diff] [blame] | 49 | std::string authData; |
| 50 | |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 51 | if (!crow::utility::base64Decode(param, authData)) |
| 52 | { |
| 53 | return nullptr; |
| 54 | } |
| 55 | std::size_t separator = authData.find(':'); |
| 56 | if (separator == std::string::npos) |
| 57 | { |
| 58 | return nullptr; |
| 59 | } |
| 60 | |
| 61 | std::string user = authData.substr(0, separator); |
| 62 | separator += 1; |
| 63 | if (separator > authData.size()) |
| 64 | { |
| 65 | return nullptr; |
| 66 | } |
| 67 | std::string pass = authData.substr(separator); |
| 68 | |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 69 | BMCWEB_LOG_DEBUG("[AuthMiddleware] Authenticating user: {}", user); |
| 70 | BMCWEB_LOG_DEBUG("[AuthMiddleware] User IPAddress: {}", |
| 71 | clientIp.to_string()); |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 72 | |
| 73 | int pamrc = pamAuthenticateUser(user, pass); |
| 74 | bool isConfigureSelfOnly = pamrc == PAM_NEW_AUTHTOK_REQD; |
| 75 | if ((pamrc != PAM_SUCCESS) && !isConfigureSelfOnly) |
| 76 | { |
| 77 | return nullptr; |
| 78 | } |
| 79 | |
| 80 | // TODO(ed) generateUserSession is a little expensive for basic |
| 81 | // auth, as it generates some random identifiers that will never be |
| 82 | // used. This should have a "fast" path for when user tokens aren't |
| 83 | // needed. |
| 84 | // This whole flow needs to be revisited anyway, as we can't be |
| 85 | // calling directly into pam for every request |
| 86 | return persistent_data::SessionStore::getInstance().generateUserSession( |
Ed Tanous | bb759e3 | 2022-08-02 17:07:54 -0700 | [diff] [blame] | 87 | user, clientIp, std::nullopt, |
Sunitha Harish | d323922 | 2021-02-24 15:33:29 +0530 | [diff] [blame] | 88 | persistent_data::PersistenceType::SINGLE_REQUEST, isConfigureSelfOnly); |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 89 | } |
Alan Kuo | f16f626 | 2020-12-08 19:29:59 +0800 | [diff] [blame] | 90 | #endif |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 91 | |
Alan Kuo | f16f626 | 2020-12-08 19:29:59 +0800 | [diff] [blame] | 92 | #ifdef BMCWEB_ENABLE_SESSION_AUTHENTICATION |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 93 | static std::shared_ptr<persistent_data::UserSession> |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 94 | performTokenAuth(std::string_view authHeader) |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 95 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 96 | BMCWEB_LOG_DEBUG("[AuthMiddleware] Token authentication"); |
Ed Tanous | 11ba397 | 2022-07-11 09:50:41 -0700 | [diff] [blame] | 97 | if (!authHeader.starts_with("Token ")) |
John Edward Broadbent | 97a056a | 2021-09-10 14:56:19 -0700 | [diff] [blame] | 98 | { |
| 99 | return nullptr; |
| 100 | } |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 101 | std::string_view token = authHeader.substr(strlen("Token ")); |
John Edward Broadbent | 59b98b2 | 2021-07-13 15:36:32 -0700 | [diff] [blame] | 102 | auto sessionOut = |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 103 | persistent_data::SessionStore::getInstance().loginSessionByToken(token); |
John Edward Broadbent | 59b98b2 | 2021-07-13 15:36:32 -0700 | [diff] [blame] | 104 | return sessionOut; |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 105 | } |
Alan Kuo | f16f626 | 2020-12-08 19:29:59 +0800 | [diff] [blame] | 106 | #endif |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 107 | |
Alan Kuo | f16f626 | 2020-12-08 19:29:59 +0800 | [diff] [blame] | 108 | #ifdef BMCWEB_ENABLE_XTOKEN_AUTHENTICATION |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 109 | static std::shared_ptr<persistent_data::UserSession> |
John Edward Broadbent | 59b98b2 | 2021-07-13 15:36:32 -0700 | [diff] [blame] | 110 | performXtokenAuth(const boost::beast::http::header<true>& reqHeader) |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 111 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 112 | BMCWEB_LOG_DEBUG("[AuthMiddleware] X-Auth-Token authentication"); |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 113 | |
John Edward Broadbent | 59b98b2 | 2021-07-13 15:36:32 -0700 | [diff] [blame] | 114 | std::string_view token = reqHeader["X-Auth-Token"]; |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 115 | if (token.empty()) |
| 116 | { |
| 117 | return nullptr; |
| 118 | } |
John Edward Broadbent | 59b98b2 | 2021-07-13 15:36:32 -0700 | [diff] [blame] | 119 | auto sessionOut = |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 120 | persistent_data::SessionStore::getInstance().loginSessionByToken(token); |
John Edward Broadbent | 59b98b2 | 2021-07-13 15:36:32 -0700 | [diff] [blame] | 121 | return sessionOut; |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 122 | } |
Alan Kuo | f16f626 | 2020-12-08 19:29:59 +0800 | [diff] [blame] | 123 | #endif |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 124 | |
Alan Kuo | f16f626 | 2020-12-08 19:29:59 +0800 | [diff] [blame] | 125 | #ifdef BMCWEB_ENABLE_COOKIE_AUTHENTICATION |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 126 | static std::shared_ptr<persistent_data::UserSession> |
Ed Tanous | 1d86960 | 2022-12-19 13:48:12 -0800 | [diff] [blame] | 127 | performCookieAuth(boost::beast::http::verb method [[maybe_unused]], |
John Edward Broadbent | 59b98b2 | 2021-07-13 15:36:32 -0700 | [diff] [blame] | 128 | const boost::beast::http::header<true>& reqHeader) |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 129 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 130 | BMCWEB_LOG_DEBUG("[AuthMiddleware] Cookie authentication"); |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 131 | |
John Edward Broadbent | 59b98b2 | 2021-07-13 15:36:32 -0700 | [diff] [blame] | 132 | std::string_view cookieValue = reqHeader["Cookie"]; |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 133 | if (cookieValue.empty()) |
| 134 | { |
| 135 | return nullptr; |
| 136 | } |
| 137 | |
| 138 | auto startIndex = cookieValue.find("SESSION="); |
| 139 | if (startIndex == std::string::npos) |
| 140 | { |
| 141 | return nullptr; |
| 142 | } |
| 143 | startIndex += sizeof("SESSION=") - 1; |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 144 | auto endIndex = cookieValue.find(';', startIndex); |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 145 | if (endIndex == std::string::npos) |
| 146 | { |
| 147 | endIndex = cookieValue.size(); |
| 148 | } |
Patrick Williams | 89492a1 | 2023-05-10 07:51:34 -0500 | [diff] [blame] | 149 | std::string_view authKey = cookieValue.substr(startIndex, |
| 150 | endIndex - startIndex); |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 151 | |
John Edward Broadbent | 59b98b2 | 2021-07-13 15:36:32 -0700 | [diff] [blame] | 152 | std::shared_ptr<persistent_data::UserSession> sessionOut = |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 153 | persistent_data::SessionStore::getInstance().loginSessionByToken( |
| 154 | authKey); |
John Edward Broadbent | 59b98b2 | 2021-07-13 15:36:32 -0700 | [diff] [blame] | 155 | if (sessionOut == nullptr) |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 156 | { |
| 157 | return nullptr; |
| 158 | } |
Ed Tanous | c3b3ad0 | 2023-06-16 11:32:36 -0700 | [diff] [blame] | 159 | sessionOut->cookieAuth = true; |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 160 | #ifndef BMCWEB_INSECURE_DISABLE_CSRF_PREVENTION |
| 161 | // RFC7231 defines methods that need csrf protection |
John Edward Broadbent | 59b98b2 | 2021-07-13 15:36:32 -0700 | [diff] [blame] | 162 | if (method != boost::beast::http::verb::get) |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 163 | { |
John Edward Broadbent | 59b98b2 | 2021-07-13 15:36:32 -0700 | [diff] [blame] | 164 | std::string_view csrf = reqHeader["X-XSRF-TOKEN"]; |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 165 | // Make sure both tokens are filled |
John Edward Broadbent | 59b98b2 | 2021-07-13 15:36:32 -0700 | [diff] [blame] | 166 | if (csrf.empty() || sessionOut->csrfToken.empty()) |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 167 | { |
| 168 | return nullptr; |
| 169 | } |
| 170 | |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 171 | if (csrf.size() != persistent_data::sessionTokenSize) |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 172 | { |
| 173 | return nullptr; |
| 174 | } |
| 175 | // Reject if csrf token not available |
John Edward Broadbent | 59b98b2 | 2021-07-13 15:36:32 -0700 | [diff] [blame] | 176 | if (!crow::utility::constantTimeStringCompare(csrf, |
| 177 | sessionOut->csrfToken)) |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 178 | { |
| 179 | return nullptr; |
| 180 | } |
| 181 | } |
| 182 | #endif |
John Edward Broadbent | 59b98b2 | 2021-07-13 15:36:32 -0700 | [diff] [blame] | 183 | return sessionOut; |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 184 | } |
Alan Kuo | f16f626 | 2020-12-08 19:29:59 +0800 | [diff] [blame] | 185 | #endif |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 186 | |
Alexander Filippov | 9645760 | 2020-09-29 14:19:38 +0300 | [diff] [blame] | 187 | #ifdef BMCWEB_ENABLE_MUTUAL_TLS_AUTHENTICATION |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 188 | static std::shared_ptr<persistent_data::UserSession> |
John Edward Broadbent | 59b98b2 | 2021-07-13 15:36:32 -0700 | [diff] [blame] | 189 | performTLSAuth(Response& res, |
| 190 | const boost::beast::http::header<true>& reqHeader, |
Ed Tanous | b5a7693 | 2020-09-29 16:16:58 -0700 | [diff] [blame] | 191 | const std::weak_ptr<persistent_data::UserSession>& session) |
James Feist | 6964c98 | 2020-07-28 16:10:23 -0700 | [diff] [blame] | 192 | { |
James Feist | 6964c98 | 2020-07-28 16:10:23 -0700 | [diff] [blame] | 193 | if (auto sp = session.lock()) |
| 194 | { |
| 195 | // set cookie only if this is req from the browser. |
John Edward Broadbent | 59b98b2 | 2021-07-13 15:36:32 -0700 | [diff] [blame] | 196 | if (reqHeader["User-Agent"].empty()) |
James Feist | 6964c98 | 2020-07-28 16:10:23 -0700 | [diff] [blame] | 197 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 198 | BMCWEB_LOG_DEBUG(" TLS session: {} will be used for this request.", |
| 199 | sp->uniqueId); |
James Feist | 6964c98 | 2020-07-28 16:10:23 -0700 | [diff] [blame] | 200 | return sp; |
| 201 | } |
Karol Niczyj | ade2fe7 | 2022-04-27 18:04:58 +0200 | [diff] [blame] | 202 | // TODO: change this to not switch to cookie auth |
Ed Tanous | 994fd86 | 2023-06-06 13:37:03 -0700 | [diff] [blame] | 203 | res.addHeader(boost::beast::http::field::set_cookie, |
Karol Niczyj | ade2fe7 | 2022-04-27 18:04:58 +0200 | [diff] [blame] | 204 | "XSRF-TOKEN=" + sp->csrfToken + |
Ed Tanous | 994fd86 | 2023-06-06 13:37:03 -0700 | [diff] [blame] | 205 | "; SameSite=Strict; Secure"); |
| 206 | res.addHeader(boost::beast::http::field::set_cookie, |
| 207 | "SESSION=" + sp->sessionToken + |
| 208 | "; SameSite=Strict; Secure; HttpOnly"); |
| 209 | res.addHeader(boost::beast::http::field::set_cookie, |
| 210 | "IsAuthenticated=true; Secure"); |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 211 | BMCWEB_LOG_DEBUG( |
| 212 | " TLS session: {} with cookie will be used for this request.", |
| 213 | sp->uniqueId); |
Karol Niczyj | ade2fe7 | 2022-04-27 18:04:58 +0200 | [diff] [blame] | 214 | return sp; |
James Feist | 6964c98 | 2020-07-28 16:10:23 -0700 | [diff] [blame] | 215 | } |
James Feist | 6964c98 | 2020-07-28 16:10:23 -0700 | [diff] [blame] | 216 | return nullptr; |
| 217 | } |
Alexander Filippov | 9645760 | 2020-09-29 14:19:38 +0300 | [diff] [blame] | 218 | #endif |
James Feist | 6964c98 | 2020-07-28 16:10:23 -0700 | [diff] [blame] | 219 | |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 220 | // checks if request can be forwarded without authentication |
Nan Zhou | 8682c5a | 2021-11-13 11:00:07 -0800 | [diff] [blame] | 221 | [[maybe_unused]] static bool isOnAllowlist(std::string_view url, |
| 222 | boost::beast::http::verb method) |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 223 | { |
John Edward Broadbent | 59b98b2 | 2021-07-13 15:36:32 -0700 | [diff] [blame] | 224 | if (boost::beast::http::verb::get == method) |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 225 | { |
John Edward Broadbent | 59b98b2 | 2021-07-13 15:36:32 -0700 | [diff] [blame] | 226 | if (url == "/redfish/v1" || url == "/redfish/v1/" || |
| 227 | url == "/redfish" || url == "/redfish/" || |
| 228 | url == "/redfish/v1/odata" || url == "/redfish/v1/odata/") |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 229 | { |
| 230 | return true; |
| 231 | } |
John Edward Broadbent | 59b98b2 | 2021-07-13 15:36:32 -0700 | [diff] [blame] | 232 | if (crow::webroutes::routes.find(std::string(url)) != |
Ed Tanous | d4d2579 | 2020-09-29 15:15:03 -0700 | [diff] [blame] | 233 | crow::webroutes::routes.end()) |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 234 | { |
| 235 | return true; |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | // it's allowed to POST on session collection & login without |
| 240 | // authentication |
John Edward Broadbent | 59b98b2 | 2021-07-13 15:36:32 -0700 | [diff] [blame] | 241 | if (boost::beast::http::verb::post == method) |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 242 | { |
John Edward Broadbent | 59b98b2 | 2021-07-13 15:36:32 -0700 | [diff] [blame] | 243 | if ((url == "/redfish/v1/SessionService/Sessions") || |
| 244 | (url == "/redfish/v1/SessionService/Sessions/") || |
Ed Tanous | e76cd86 | 2022-03-14 09:12:00 -0700 | [diff] [blame] | 245 | (url == "/redfish/v1/SessionService/Sessions/Members") || |
| 246 | (url == "/redfish/v1/SessionService/Sessions/Members/") || |
John Edward Broadbent | 59b98b2 | 2021-07-13 15:36:32 -0700 | [diff] [blame] | 247 | (url == "/login")) |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 248 | { |
| 249 | return true; |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | return false; |
| 254 | } |
| 255 | |
Nan Zhou | 8682c5a | 2021-11-13 11:00:07 -0800 | [diff] [blame] | 256 | [[maybe_unused]] static std::shared_ptr<persistent_data::UserSession> |
| 257 | authenticate( |
Ed Tanous | 02cad96 | 2022-06-30 16:50:15 -0700 | [diff] [blame] | 258 | const boost::asio::ip::address& ipAddress [[maybe_unused]], |
Nan Zhou | 3acced2 | 2022-07-12 23:21:02 +0000 | [diff] [blame] | 259 | Response& res [[maybe_unused]], |
| 260 | boost::beast::http::verb method [[maybe_unused]], |
Nan Zhou | 8682c5a | 2021-11-13 11:00:07 -0800 | [diff] [blame] | 261 | const boost::beast::http::header<true>& reqHeader, |
| 262 | [[maybe_unused]] const std::shared_ptr<persistent_data::UserSession>& |
| 263 | session) |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 264 | { |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 265 | const persistent_data::AuthConfigMethods& authMethodsConfig = |
| 266 | persistent_data::SessionStore::getInstance().getAuthMethodsConfig(); |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 267 | |
John Edward Broadbent | 59b98b2 | 2021-07-13 15:36:32 -0700 | [diff] [blame] | 268 | std::shared_ptr<persistent_data::UserSession> sessionOut = nullptr; |
Alexander Filippov | 9645760 | 2020-09-29 14:19:38 +0300 | [diff] [blame] | 269 | #ifdef BMCWEB_ENABLE_MUTUAL_TLS_AUTHENTICATION |
John Edward Broadbent | 59b98b2 | 2021-07-13 15:36:32 -0700 | [diff] [blame] | 270 | if (authMethodsConfig.tls) |
James Feist | 6964c98 | 2020-07-28 16:10:23 -0700 | [diff] [blame] | 271 | { |
John Edward Broadbent | 59b98b2 | 2021-07-13 15:36:32 -0700 | [diff] [blame] | 272 | sessionOut = performTLSAuth(res, reqHeader, session); |
James Feist | 6964c98 | 2020-07-28 16:10:23 -0700 | [diff] [blame] | 273 | } |
Alexander Filippov | 9645760 | 2020-09-29 14:19:38 +0300 | [diff] [blame] | 274 | #endif |
Alan Kuo | f16f626 | 2020-12-08 19:29:59 +0800 | [diff] [blame] | 275 | #ifdef BMCWEB_ENABLE_XTOKEN_AUTHENTICATION |
John Edward Broadbent | 59b98b2 | 2021-07-13 15:36:32 -0700 | [diff] [blame] | 276 | if (sessionOut == nullptr && authMethodsConfig.xtoken) |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 277 | { |
John Edward Broadbent | 59b98b2 | 2021-07-13 15:36:32 -0700 | [diff] [blame] | 278 | sessionOut = performXtokenAuth(reqHeader); |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 279 | } |
Alan Kuo | f16f626 | 2020-12-08 19:29:59 +0800 | [diff] [blame] | 280 | #endif |
| 281 | #ifdef BMCWEB_ENABLE_COOKIE_AUTHENTICATION |
John Edward Broadbent | 59b98b2 | 2021-07-13 15:36:32 -0700 | [diff] [blame] | 282 | if (sessionOut == nullptr && authMethodsConfig.cookie) |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 283 | { |
John Edward Broadbent | 59b98b2 | 2021-07-13 15:36:32 -0700 | [diff] [blame] | 284 | sessionOut = performCookieAuth(method, reqHeader); |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 285 | } |
Alan Kuo | f16f626 | 2020-12-08 19:29:59 +0800 | [diff] [blame] | 286 | #endif |
John Edward Broadbent | 59b98b2 | 2021-07-13 15:36:32 -0700 | [diff] [blame] | 287 | std::string_view authHeader = reqHeader["Authorization"]; |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 288 | BMCWEB_LOG_DEBUG("authHeader={}", authHeader); |
John Edward Broadbent | 59b98b2 | 2021-07-13 15:36:32 -0700 | [diff] [blame] | 289 | |
John Edward Broadbent | 97a056a | 2021-09-10 14:56:19 -0700 | [diff] [blame] | 290 | if (sessionOut == nullptr && authMethodsConfig.sessionToken) |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 291 | { |
Alan Kuo | f16f626 | 2020-12-08 19:29:59 +0800 | [diff] [blame] | 292 | #ifdef BMCWEB_ENABLE_SESSION_AUTHENTICATION |
John Edward Broadbent | 97a056a | 2021-09-10 14:56:19 -0700 | [diff] [blame] | 293 | sessionOut = performTokenAuth(authHeader); |
Alan Kuo | f16f626 | 2020-12-08 19:29:59 +0800 | [diff] [blame] | 294 | #endif |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 295 | } |
John Edward Broadbent | 97a056a | 2021-09-10 14:56:19 -0700 | [diff] [blame] | 296 | if (sessionOut == nullptr && authMethodsConfig.basic) |
| 297 | { |
| 298 | #ifdef BMCWEB_ENABLE_BASIC_AUTHENTICATION |
| 299 | sessionOut = performBasicAuth(ipAddress, authHeader); |
| 300 | #endif |
| 301 | } |
| 302 | if (sessionOut != nullptr) |
| 303 | { |
| 304 | return sessionOut; |
| 305 | } |
| 306 | |
John Edward Broadbent | 59b98b2 | 2021-07-13 15:36:32 -0700 | [diff] [blame] | 307 | return nullptr; |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 308 | } |
| 309 | |
Nan Zhou | d055a34 | 2022-05-25 01:15:34 +0000 | [diff] [blame] | 310 | } // namespace authentication |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 311 | } // namespace crow |