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