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, |
| 40 | std::string_view auth_header) |
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; |
| 45 | std::string_view param = auth_header.substr(strlen("Basic ")); |
| 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 |
| 81 | return persistent_data::SessionStore::getInstance().generateUserSession( |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 82 | user, persistent_data::PersistenceType::SINGLE_REQUEST, |
Sunitha Harish | c0ea7ae | 2020-10-30 02:37:30 -0500 | [diff] [blame] | 83 | isConfigureSelfOnly, clientIp.to_string()); |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 84 | } |
Alan Kuo | f16f626 | 2020-12-08 19:29:59 +0800 | [diff] [blame^] | 85 | #endif |
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 | #ifdef BMCWEB_ENABLE_SESSION_AUTHENTICATION |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 88 | static std::shared_ptr<persistent_data::UserSession> |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 89 | performTokenAuth(std::string_view auth_header) |
| 90 | { |
| 91 | BMCWEB_LOG_DEBUG << "[AuthMiddleware] Token authentication"; |
| 92 | |
| 93 | std::string_view token = auth_header.substr(strlen("Token ")); |
| 94 | auto session = |
| 95 | persistent_data::SessionStore::getInstance().loginSessionByToken(token); |
| 96 | return session; |
| 97 | } |
Alan Kuo | f16f626 | 2020-12-08 19:29:59 +0800 | [diff] [blame^] | 98 | #endif |
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 | #ifdef BMCWEB_ENABLE_XTOKEN_AUTHENTICATION |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 101 | static std::shared_ptr<persistent_data::UserSession> |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 102 | performXtokenAuth(const crow::Request& req) |
| 103 | { |
| 104 | BMCWEB_LOG_DEBUG << "[AuthMiddleware] X-Auth-Token authentication"; |
| 105 | |
| 106 | std::string_view token = req.getHeaderValue("X-Auth-Token"); |
| 107 | if (token.empty()) |
| 108 | { |
| 109 | return nullptr; |
| 110 | } |
| 111 | auto session = |
| 112 | persistent_data::SessionStore::getInstance().loginSessionByToken(token); |
| 113 | return session; |
| 114 | } |
Alan Kuo | f16f626 | 2020-12-08 19:29:59 +0800 | [diff] [blame^] | 115 | #endif |
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 | #ifdef BMCWEB_ENABLE_COOKIE_AUTHENTICATION |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 118 | static std::shared_ptr<persistent_data::UserSession> |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 119 | performCookieAuth(const crow::Request& req) |
| 120 | { |
| 121 | BMCWEB_LOG_DEBUG << "[AuthMiddleware] Cookie authentication"; |
| 122 | |
| 123 | std::string_view cookieValue = req.getHeaderValue("Cookie"); |
| 124 | if (cookieValue.empty()) |
| 125 | { |
| 126 | return nullptr; |
| 127 | } |
| 128 | |
| 129 | auto startIndex = cookieValue.find("SESSION="); |
| 130 | if (startIndex == std::string::npos) |
| 131 | { |
| 132 | return nullptr; |
| 133 | } |
| 134 | startIndex += sizeof("SESSION=") - 1; |
| 135 | auto endIndex = cookieValue.find(";", startIndex); |
| 136 | if (endIndex == std::string::npos) |
| 137 | { |
| 138 | endIndex = cookieValue.size(); |
| 139 | } |
| 140 | std::string_view authKey = |
| 141 | cookieValue.substr(startIndex, endIndex - startIndex); |
| 142 | |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 143 | std::shared_ptr<persistent_data::UserSession> session = |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 144 | persistent_data::SessionStore::getInstance().loginSessionByToken( |
| 145 | authKey); |
| 146 | if (session == nullptr) |
| 147 | { |
| 148 | return nullptr; |
| 149 | } |
| 150 | #ifndef BMCWEB_INSECURE_DISABLE_CSRF_PREVENTION |
| 151 | // RFC7231 defines methods that need csrf protection |
Ed Tanous | b41187f | 2019-10-24 16:30:02 -0700 | [diff] [blame] | 152 | if (req.method() != boost::beast::http::verb::get) |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 153 | { |
| 154 | std::string_view csrf = req.getHeaderValue("X-XSRF-TOKEN"); |
| 155 | // Make sure both tokens are filled |
| 156 | if (csrf.empty() || session->csrfToken.empty()) |
| 157 | { |
| 158 | return nullptr; |
| 159 | } |
| 160 | |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 161 | if (csrf.size() != persistent_data::sessionTokenSize) |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 162 | { |
| 163 | return nullptr; |
| 164 | } |
| 165 | // Reject if csrf token not available |
| 166 | if (!crow::utility::constantTimeStringCompare(csrf, session->csrfToken)) |
| 167 | { |
| 168 | return nullptr; |
| 169 | } |
| 170 | } |
| 171 | #endif |
| 172 | return session; |
| 173 | } |
Alan Kuo | f16f626 | 2020-12-08 19:29:59 +0800 | [diff] [blame^] | 174 | #endif |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 175 | |
Alexander Filippov | 9645760 | 2020-09-29 14:19:38 +0300 | [diff] [blame] | 176 | #ifdef BMCWEB_ENABLE_MUTUAL_TLS_AUTHENTICATION |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 177 | static std::shared_ptr<persistent_data::UserSession> |
James Feist | 6964c98 | 2020-07-28 16:10:23 -0700 | [diff] [blame] | 178 | performTLSAuth(const crow::Request& req, Response& res, |
Ed Tanous | b5a7693 | 2020-09-29 16:16:58 -0700 | [diff] [blame] | 179 | const std::weak_ptr<persistent_data::UserSession>& session) |
James Feist | 6964c98 | 2020-07-28 16:10:23 -0700 | [diff] [blame] | 180 | { |
James Feist | 6964c98 | 2020-07-28 16:10:23 -0700 | [diff] [blame] | 181 | if (auto sp = session.lock()) |
| 182 | { |
| 183 | // set cookie only if this is req from the browser. |
| 184 | if (req.getHeaderValue("User-Agent").empty()) |
| 185 | { |
| 186 | BMCWEB_LOG_DEBUG << " TLS session: " << sp->uniqueId |
| 187 | << " will be used for this request."; |
| 188 | return sp; |
| 189 | } |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 190 | std::string_view cookieValue = req.getHeaderValue("Cookie"); |
| 191 | if (cookieValue.empty() || |
| 192 | cookieValue.find("SESSION=") == std::string::npos) |
James Feist | 6964c98 | 2020-07-28 16:10:23 -0700 | [diff] [blame] | 193 | { |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 194 | // TODO: change this to not switch to cookie auth |
| 195 | res.addHeader("Set-Cookie", "XSRF-TOKEN=" + sp->csrfToken + |
| 196 | "; Secure\r\nSet-Cookie: SESSION=" + |
| 197 | sp->sessionToken + |
| 198 | "; Secure; HttpOnly\r\nSet-Cookie: " |
| 199 | "IsAuthenticated=true; Secure"); |
| 200 | BMCWEB_LOG_DEBUG << " TLS session: " << sp->uniqueId |
| 201 | << " with cookie will be used for this request."; |
| 202 | return sp; |
James Feist | 6964c98 | 2020-07-28 16:10:23 -0700 | [diff] [blame] | 203 | } |
| 204 | } |
James Feist | 6964c98 | 2020-07-28 16:10:23 -0700 | [diff] [blame] | 205 | return nullptr; |
| 206 | } |
Alexander Filippov | 9645760 | 2020-09-29 14:19:38 +0300 | [diff] [blame] | 207 | #endif |
James Feist | 6964c98 | 2020-07-28 16:10:23 -0700 | [diff] [blame] | 208 | |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 209 | // checks if request can be forwarded without authentication |
| 210 | static bool isOnWhitelist(const crow::Request& req) |
| 211 | { |
| 212 | // it's allowed to GET root node without authentication |
Ed Tanous | b41187f | 2019-10-24 16:30:02 -0700 | [diff] [blame] | 213 | if (boost::beast::http::verb::get == req.method()) |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 214 | { |
| 215 | if (req.url == "/redfish/v1" || req.url == "/redfish/v1/" || |
| 216 | req.url == "/redfish" || req.url == "/redfish/" || |
| 217 | req.url == "/redfish/v1/odata" || req.url == "/redfish/v1/odata/") |
| 218 | { |
| 219 | return true; |
| 220 | } |
Ed Tanous | d4d2579 | 2020-09-29 15:15:03 -0700 | [diff] [blame] | 221 | if (crow::webroutes::routes.find(std::string(req.url)) != |
| 222 | crow::webroutes::routes.end()) |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 223 | { |
| 224 | return true; |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | // it's allowed to POST on session collection & login without |
| 229 | // authentication |
Ed Tanous | b41187f | 2019-10-24 16:30:02 -0700 | [diff] [blame] | 230 | if (boost::beast::http::verb::post == req.method()) |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 231 | { |
| 232 | if ((req.url == "/redfish/v1/SessionService/Sessions") || |
| 233 | (req.url == "/redfish/v1/SessionService/Sessions/") || |
| 234 | (req.url == "/login")) |
| 235 | { |
| 236 | return true; |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | return false; |
| 241 | } |
| 242 | |
Alexander Filippov | 9645760 | 2020-09-29 14:19:38 +0300 | [diff] [blame] | 243 | static void authenticate( |
| 244 | crow::Request& req, Response& res, |
Ed Tanous | f23b729 | 2020-10-15 09:41:17 -0700 | [diff] [blame] | 245 | [[maybe_unused]] const std::weak_ptr<persistent_data::UserSession>& session) |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 246 | { |
| 247 | if (isOnWhitelist(req)) |
| 248 | { |
| 249 | return; |
| 250 | } |
| 251 | |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 252 | const persistent_data::AuthConfigMethods& authMethodsConfig = |
| 253 | persistent_data::SessionStore::getInstance().getAuthMethodsConfig(); |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 254 | |
Alexander Filippov | 9645760 | 2020-09-29 14:19:38 +0300 | [diff] [blame] | 255 | #ifdef BMCWEB_ENABLE_MUTUAL_TLS_AUTHENTICATION |
James Feist | 6964c98 | 2020-07-28 16:10:23 -0700 | [diff] [blame] | 256 | if (req.session == nullptr && authMethodsConfig.tls) |
| 257 | { |
Ed Tanous | f23b729 | 2020-10-15 09:41:17 -0700 | [diff] [blame] | 258 | req.session = performTLSAuth(req, res, session); |
James Feist | 6964c98 | 2020-07-28 16:10:23 -0700 | [diff] [blame] | 259 | } |
Alexander Filippov | 9645760 | 2020-09-29 14:19:38 +0300 | [diff] [blame] | 260 | #endif |
Alan Kuo | f16f626 | 2020-12-08 19:29:59 +0800 | [diff] [blame^] | 261 | #ifdef BMCWEB_ENABLE_XTOKEN_AUTHENTICATION |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 262 | if (req.session == nullptr && authMethodsConfig.xtoken) |
| 263 | { |
| 264 | req.session = performXtokenAuth(req); |
| 265 | } |
Alan Kuo | f16f626 | 2020-12-08 19:29:59 +0800 | [diff] [blame^] | 266 | #endif |
| 267 | #ifdef BMCWEB_ENABLE_COOKIE_AUTHENTICATION |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 268 | if (req.session == nullptr && authMethodsConfig.cookie) |
| 269 | { |
| 270 | req.session = performCookieAuth(req); |
| 271 | } |
Alan Kuo | f16f626 | 2020-12-08 19:29:59 +0800 | [diff] [blame^] | 272 | #endif |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 273 | if (req.session == nullptr) |
| 274 | { |
| 275 | std::string_view authHeader = req.getHeaderValue("Authorization"); |
| 276 | if (!authHeader.empty()) |
| 277 | { |
| 278 | // Reject any kind of auth other than basic or token |
| 279 | if (boost::starts_with(authHeader, "Token ") && |
| 280 | authMethodsConfig.sessionToken) |
| 281 | { |
Alan Kuo | f16f626 | 2020-12-08 19:29:59 +0800 | [diff] [blame^] | 282 | #ifdef BMCWEB_ENABLE_SESSION_AUTHENTICATION |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 283 | req.session = performTokenAuth(authHeader); |
Alan Kuo | f16f626 | 2020-12-08 19:29:59 +0800 | [diff] [blame^] | 284 | #endif |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 285 | } |
| 286 | else if (boost::starts_with(authHeader, "Basic ") && |
| 287 | authMethodsConfig.basic) |
| 288 | { |
Alan Kuo | f16f626 | 2020-12-08 19:29:59 +0800 | [diff] [blame^] | 289 | #ifdef BMCWEB_ENABLE_BASIC_AUTHENTICATION |
Sunitha Harish | c0ea7ae | 2020-10-30 02:37:30 -0500 | [diff] [blame] | 290 | req.session = performBasicAuth(req.ipAddress, authHeader); |
Alan Kuo | f16f626 | 2020-12-08 19:29:59 +0800 | [diff] [blame^] | 291 | #endif |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 292 | } |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | if (req.session == nullptr) |
| 297 | { |
| 298 | BMCWEB_LOG_WARNING << "[AuthMiddleware] authorization failed"; |
| 299 | |
| 300 | // If it's a browser connecting, don't send the HTTP authenticate |
| 301 | // header, to avoid possible CSRF attacks with basic auth |
| 302 | if (http_helpers::requestPrefersHtml(req)) |
| 303 | { |
| 304 | res.result(boost::beast::http::status::temporary_redirect); |
| 305 | res.addHeader("Location", |
| 306 | "/#/login?next=" + http_helpers::urlEncode(req.url)); |
| 307 | } |
| 308 | else |
| 309 | { |
| 310 | res.result(boost::beast::http::status::unauthorized); |
| 311 | // only send the WWW-authenticate header if this isn't a xhr |
| 312 | // from the browser. most scripts, |
| 313 | if (req.getHeaderValue("User-Agent").empty()) |
| 314 | { |
| 315 | res.addHeader("WWW-Authenticate", "Basic"); |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | res.end(); |
| 320 | return; |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | } // namespace authorization |
| 325 | } // namespace crow |