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