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> |
| 9 | #include <http_request.hpp> |
| 10 | #include <http_response.hpp> |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 11 | #include <http_utility.hpp> |
| 12 | #include <pam_authenticate.hpp> |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 13 | |
| 14 | #include <random> |
Ed Tanous | b5a7693 | 2020-09-29 16:16:58 -0700 | [diff] [blame] | 15 | #include <utility> |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 16 | |
| 17 | namespace crow |
| 18 | { |
| 19 | |
| 20 | namespace authorization |
| 21 | { |
| 22 | |
| 23 | static 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 Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 31 | persistent_data::PersistenceType::SINGLE_REQUEST) |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 32 | { |
| 33 | persistent_data::SessionStore::getInstance().removeSession(req.session); |
| 34 | } |
| 35 | } |
| 36 | |
Alan Kuo | f16f626 | 2020-12-08 19:29:59 +0800 | [diff] [blame] | 37 | #ifdef BMCWEB_ENABLE_BASIC_AUTHENTICATION |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 38 | static std::shared_ptr<persistent_data::UserSession> |
Sunitha Harish | c0ea7ae | 2020-10-30 02:37:30 -0500 | [diff] [blame] | 39 | performBasicAuth(const boost::asio::ip::address& clientIp, |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 40 | std::string_view authHeader) |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 41 | { |
| 42 | BMCWEB_LOG_DEBUG << "[AuthMiddleware] Basic authentication"; |
| 43 | |
| 44 | std::string authData; |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 45 | std::string_view param = authHeader.substr(strlen("Basic ")); |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 46 | 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 Harish | c0ea7ae | 2020-10-30 02:37:30 -0500 | [diff] [blame] | 65 | BMCWEB_LOG_DEBUG << "[AuthMiddleware] User IPAddress: " |
| 66 | << clientIp.to_string(); |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 67 | |
| 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 Harish | d323922 | 2021-02-24 15:33:29 +0530 | [diff] [blame] | 81 | std::string unsupportedClientId = ""; |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 82 | return persistent_data::SessionStore::getInstance().generateUserSession( |
Sunitha Harish | d323922 | 2021-02-24 15:33:29 +0530 | [diff] [blame] | 83 | user, clientIp.to_string(), unsupportedClientId, |
| 84 | persistent_data::PersistenceType::SINGLE_REQUEST, isConfigureSelfOnly); |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 85 | } |
Alan Kuo | f16f626 | 2020-12-08 19:29:59 +0800 | [diff] [blame] | 86 | #endif |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 87 | |
Alan Kuo | f16f626 | 2020-12-08 19:29:59 +0800 | [diff] [blame] | 88 | #ifdef BMCWEB_ENABLE_SESSION_AUTHENTICATION |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 89 | static std::shared_ptr<persistent_data::UserSession> |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 90 | performTokenAuth(std::string_view authHeader) |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 91 | { |
| 92 | BMCWEB_LOG_DEBUG << "[AuthMiddleware] Token authentication"; |
| 93 | |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 94 | std::string_view token = authHeader.substr(strlen("Token ")); |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 95 | auto session = |
| 96 | persistent_data::SessionStore::getInstance().loginSessionByToken(token); |
| 97 | return session; |
| 98 | } |
Alan Kuo | f16f626 | 2020-12-08 19:29:59 +0800 | [diff] [blame] | 99 | #endif |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 100 | |
Alan Kuo | f16f626 | 2020-12-08 19:29:59 +0800 | [diff] [blame] | 101 | #ifdef BMCWEB_ENABLE_XTOKEN_AUTHENTICATION |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 102 | static std::shared_ptr<persistent_data::UserSession> |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 103 | 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 Kuo | f16f626 | 2020-12-08 19:29:59 +0800 | [diff] [blame] | 116 | #endif |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 117 | |
Alan Kuo | f16f626 | 2020-12-08 19:29:59 +0800 | [diff] [blame] | 118 | #ifdef BMCWEB_ENABLE_COOKIE_AUTHENTICATION |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 119 | static std::shared_ptr<persistent_data::UserSession> |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 120 | 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 Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 136 | auto endIndex = cookieValue.find(';', startIndex); |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 137 | if (endIndex == std::string::npos) |
| 138 | { |
| 139 | endIndex = cookieValue.size(); |
| 140 | } |
| 141 | std::string_view authKey = |
| 142 | cookieValue.substr(startIndex, endIndex - startIndex); |
| 143 | |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 144 | std::shared_ptr<persistent_data::UserSession> session = |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 145 | 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 Tanous | b41187f | 2019-10-24 16:30:02 -0700 | [diff] [blame] | 153 | if (req.method() != boost::beast::http::verb::get) |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 154 | { |
| 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 Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 162 | if (csrf.size() != persistent_data::sessionTokenSize) |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 163 | { |
| 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 Kuo | f16f626 | 2020-12-08 19:29:59 +0800 | [diff] [blame] | 175 | #endif |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 176 | |
Alexander Filippov | 9645760 | 2020-09-29 14:19:38 +0300 | [diff] [blame] | 177 | #ifdef BMCWEB_ENABLE_MUTUAL_TLS_AUTHENTICATION |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 178 | static std::shared_ptr<persistent_data::UserSession> |
James Feist | 6964c98 | 2020-07-28 16:10:23 -0700 | [diff] [blame] | 179 | performTLSAuth(const crow::Request& req, Response& res, |
Ed Tanous | b5a7693 | 2020-09-29 16:16:58 -0700 | [diff] [blame] | 180 | const std::weak_ptr<persistent_data::UserSession>& session) |
James Feist | 6964c98 | 2020-07-28 16:10:23 -0700 | [diff] [blame] | 181 | { |
James Feist | 6964c98 | 2020-07-28 16:10:23 -0700 | [diff] [blame] | 182 | 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 Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 191 | std::string_view cookieValue = req.getHeaderValue("Cookie"); |
| 192 | if (cookieValue.empty() || |
| 193 | cookieValue.find("SESSION=") == std::string::npos) |
James Feist | 6964c98 | 2020-07-28 16:10:23 -0700 | [diff] [blame] | 194 | { |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 195 | // TODO: change this to not switch to cookie auth |
Gunnar Mills | 636be39 | 2021-03-15 12:47:07 -0500 | [diff] [blame] | 196 | res.addHeader( |
| 197 | "Set-Cookie", |
| 198 | "XSRF-TOKEN=" + sp->csrfToken + |
| 199 | "; SameSite=Strict; Secure\r\nSet-Cookie: SESSION=" + |
| 200 | sp->sessionToken + |
| 201 | "; SameSite=Strict; Secure; HttpOnly\r\nSet-Cookie: " |
| 202 | "IsAuthenticated=true; Secure"); |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 203 | BMCWEB_LOG_DEBUG << " TLS session: " << sp->uniqueId |
| 204 | << " with cookie will be used for this request."; |
| 205 | return sp; |
James Feist | 6964c98 | 2020-07-28 16:10:23 -0700 | [diff] [blame] | 206 | } |
| 207 | } |
James Feist | 6964c98 | 2020-07-28 16:10:23 -0700 | [diff] [blame] | 208 | return nullptr; |
| 209 | } |
Alexander Filippov | 9645760 | 2020-09-29 14:19:38 +0300 | [diff] [blame] | 210 | #endif |
James Feist | 6964c98 | 2020-07-28 16:10:23 -0700 | [diff] [blame] | 211 | |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 212 | // checks if request can be forwarded without authentication |
| 213 | static bool isOnWhitelist(const crow::Request& req) |
| 214 | { |
| 215 | // it's allowed to GET root node without authentication |
Ed Tanous | b41187f | 2019-10-24 16:30:02 -0700 | [diff] [blame] | 216 | if (boost::beast::http::verb::get == req.method()) |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 217 | { |
| 218 | if (req.url == "/redfish/v1" || req.url == "/redfish/v1/" || |
| 219 | req.url == "/redfish" || req.url == "/redfish/" || |
| 220 | req.url == "/redfish/v1/odata" || req.url == "/redfish/v1/odata/") |
| 221 | { |
| 222 | return true; |
| 223 | } |
Ed Tanous | d4d2579 | 2020-09-29 15:15:03 -0700 | [diff] [blame] | 224 | if (crow::webroutes::routes.find(std::string(req.url)) != |
| 225 | crow::webroutes::routes.end()) |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 226 | { |
| 227 | return true; |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | // it's allowed to POST on session collection & login without |
| 232 | // authentication |
Ed Tanous | b41187f | 2019-10-24 16:30:02 -0700 | [diff] [blame] | 233 | if (boost::beast::http::verb::post == req.method()) |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 234 | { |
| 235 | if ((req.url == "/redfish/v1/SessionService/Sessions") || |
| 236 | (req.url == "/redfish/v1/SessionService/Sessions/") || |
| 237 | (req.url == "/login")) |
| 238 | { |
| 239 | return true; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | return false; |
| 244 | } |
| 245 | |
Alexander Filippov | 9645760 | 2020-09-29 14:19:38 +0300 | [diff] [blame] | 246 | static void authenticate( |
| 247 | crow::Request& req, Response& res, |
Ed Tanous | f23b729 | 2020-10-15 09:41:17 -0700 | [diff] [blame] | 248 | [[maybe_unused]] const std::weak_ptr<persistent_data::UserSession>& session) |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 249 | { |
| 250 | if (isOnWhitelist(req)) |
| 251 | { |
| 252 | return; |
| 253 | } |
| 254 | |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 255 | const persistent_data::AuthConfigMethods& authMethodsConfig = |
| 256 | persistent_data::SessionStore::getInstance().getAuthMethodsConfig(); |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 257 | |
Alexander Filippov | 9645760 | 2020-09-29 14:19:38 +0300 | [diff] [blame] | 258 | #ifdef BMCWEB_ENABLE_MUTUAL_TLS_AUTHENTICATION |
James Feist | 6964c98 | 2020-07-28 16:10:23 -0700 | [diff] [blame] | 259 | if (req.session == nullptr && authMethodsConfig.tls) |
| 260 | { |
Ed Tanous | f23b729 | 2020-10-15 09:41:17 -0700 | [diff] [blame] | 261 | req.session = performTLSAuth(req, res, session); |
James Feist | 6964c98 | 2020-07-28 16:10:23 -0700 | [diff] [blame] | 262 | } |
Alexander Filippov | 9645760 | 2020-09-29 14:19:38 +0300 | [diff] [blame] | 263 | #endif |
Alan Kuo | f16f626 | 2020-12-08 19:29:59 +0800 | [diff] [blame] | 264 | #ifdef BMCWEB_ENABLE_XTOKEN_AUTHENTICATION |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 265 | if (req.session == nullptr && authMethodsConfig.xtoken) |
| 266 | { |
| 267 | req.session = performXtokenAuth(req); |
| 268 | } |
Alan Kuo | f16f626 | 2020-12-08 19:29:59 +0800 | [diff] [blame] | 269 | #endif |
| 270 | #ifdef BMCWEB_ENABLE_COOKIE_AUTHENTICATION |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 271 | if (req.session == nullptr && authMethodsConfig.cookie) |
| 272 | { |
| 273 | req.session = performCookieAuth(req); |
| 274 | } |
Alan Kuo | f16f626 | 2020-12-08 19:29:59 +0800 | [diff] [blame] | 275 | #endif |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 276 | if (req.session == nullptr) |
| 277 | { |
| 278 | std::string_view authHeader = req.getHeaderValue("Authorization"); |
| 279 | if (!authHeader.empty()) |
| 280 | { |
| 281 | // Reject any kind of auth other than basic or token |
| 282 | if (boost::starts_with(authHeader, "Token ") && |
| 283 | authMethodsConfig.sessionToken) |
| 284 | { |
Alan Kuo | f16f626 | 2020-12-08 19:29:59 +0800 | [diff] [blame] | 285 | #ifdef BMCWEB_ENABLE_SESSION_AUTHENTICATION |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 286 | req.session = performTokenAuth(authHeader); |
Alan Kuo | f16f626 | 2020-12-08 19:29:59 +0800 | [diff] [blame] | 287 | #endif |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 288 | } |
| 289 | else if (boost::starts_with(authHeader, "Basic ") && |
| 290 | authMethodsConfig.basic) |
| 291 | { |
Alan Kuo | f16f626 | 2020-12-08 19:29:59 +0800 | [diff] [blame] | 292 | #ifdef BMCWEB_ENABLE_BASIC_AUTHENTICATION |
Sunitha Harish | c0ea7ae | 2020-10-30 02:37:30 -0500 | [diff] [blame] | 293 | req.session = performBasicAuth(req.ipAddress, 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 | } |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | if (req.session == nullptr) |
| 300 | { |
| 301 | BMCWEB_LOG_WARNING << "[AuthMiddleware] authorization failed"; |
| 302 | |
| 303 | // If it's a browser connecting, don't send the HTTP authenticate |
| 304 | // header, to avoid possible CSRF attacks with basic auth |
| 305 | if (http_helpers::requestPrefersHtml(req)) |
| 306 | { |
| 307 | res.result(boost::beast::http::status::temporary_redirect); |
| 308 | res.addHeader("Location", |
| 309 | "/#/login?next=" + http_helpers::urlEncode(req.url)); |
| 310 | } |
| 311 | else |
| 312 | { |
| 313 | res.result(boost::beast::http::status::unauthorized); |
| 314 | // only send the WWW-authenticate header if this isn't a xhr |
| 315 | // from the browser. most scripts, |
| 316 | if (req.getHeaderValue("User-Agent").empty()) |
| 317 | { |
| 318 | res.addHeader("WWW-Authenticate", "Basic"); |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | res.end(); |
| 323 | return; |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | } // namespace authorization |
| 328 | } // namespace crow |