Ed Tanous | f927347 | 2017-02-28 16:05:13 -0800 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Ed Tanous | c94ad49 | 2019-10-10 15:39:33 -0700 | [diff] [blame] | 3 | #include <app.h> |
| 4 | #include <common.h> |
| 5 | #include <http_request.h> |
| 6 | #include <http_response.h> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 7 | |
Ed Tanous | 4758d5b | 2017-06-06 15:28:13 -0700 | [diff] [blame] | 8 | #include <boost/container/flat_set.hpp> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 9 | #include <pam_authenticate.hpp> |
| 10 | #include <persistent_data_middleware.hpp> |
| 11 | #include <random> |
| 12 | #include <webassets.hpp> |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 13 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 14 | namespace crow |
| 15 | { |
Ed Tanous | b4d29f4 | 2017-03-24 16:39:25 -0700 | [diff] [blame] | 16 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 17 | namespace token_authorization |
| 18 | { |
Ed Tanous | b4d29f4 | 2017-03-24 16:39:25 -0700 | [diff] [blame] | 19 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 20 | class Middleware |
| 21 | { |
| 22 | public: |
| 23 | struct Context |
| 24 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 25 | }; |
Kowalski, Kamil | 2b7981f | 2018-01-31 13:24:59 +0100 | [diff] [blame] | 26 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 27 | void beforeHandle(crow::Request& req, Response& res, Context& ctx) |
| 28 | { |
| 29 | if (isOnWhitelist(req)) |
| 30 | { |
| 31 | return; |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 32 | } |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 33 | |
Zbigniew Kurzynski | 7815863 | 2019-11-05 12:57:37 +0100 | [diff] [blame] | 34 | const crow::persistent_data::AuthConfigMethods& authMethodsConfig = |
| 35 | crow::persistent_data::SessionStore::getInstance() |
| 36 | .getAuthMethodsConfig(); |
| 37 | |
| 38 | if (req.session == nullptr && authMethodsConfig.xtoken) |
| 39 | { |
| 40 | req.session = performXtokenAuth(req); |
| 41 | } |
| 42 | if (req.session == nullptr && authMethodsConfig.cookie) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 43 | { |
Ratan Gupta | 6f35956 | 2019-04-03 10:39:08 +0530 | [diff] [blame] | 44 | req.session = performCookieAuth(req); |
Ed Tanous | 9bd21fc | 2018-04-26 16:08:56 -0700 | [diff] [blame] | 45 | } |
Ratan Gupta | 6f35956 | 2019-04-03 10:39:08 +0530 | [diff] [blame] | 46 | if (req.session == nullptr) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 47 | { |
Ed Tanous | 39e7750 | 2019-03-04 17:35:53 -0800 | [diff] [blame] | 48 | std::string_view authHeader = req.getHeaderValue("Authorization"); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 49 | if (!authHeader.empty()) |
| 50 | { |
| 51 | // Reject any kind of auth other than basic or token |
Zbigniew Kurzynski | 7815863 | 2019-11-05 12:57:37 +0100 | [diff] [blame] | 52 | if (boost::starts_with(authHeader, "Token ") && |
| 53 | authMethodsConfig.sessionToken) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 54 | { |
Ratan Gupta | 6f35956 | 2019-04-03 10:39:08 +0530 | [diff] [blame] | 55 | req.session = performTokenAuth(authHeader); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 56 | } |
Zbigniew Kurzynski | 7815863 | 2019-11-05 12:57:37 +0100 | [diff] [blame] | 57 | else if (boost::starts_with(authHeader, "Basic ") && |
| 58 | authMethodsConfig.basic) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 59 | { |
Ratan Gupta | 6f35956 | 2019-04-03 10:39:08 +0530 | [diff] [blame] | 60 | req.session = performBasicAuth(authHeader); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 61 | } |
| 62 | } |
| 63 | } |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 64 | |
Ratan Gupta | 6f35956 | 2019-04-03 10:39:08 +0530 | [diff] [blame] | 65 | if (req.session == nullptr) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 66 | { |
| 67 | BMCWEB_LOG_WARNING << "[AuthMiddleware] authorization failed"; |
| 68 | |
| 69 | // If it's a browser connecting, don't send the HTTP authenticate |
| 70 | // header, to avoid possible CSRF attacks with basic auth |
| 71 | if (http_helpers::requestPrefersHtml(req)) |
| 72 | { |
| 73 | res.result(boost::beast::http::status::temporary_redirect); |
Ed Tanous | 6b5e77d | 2018-11-16 14:52:56 -0800 | [diff] [blame] | 74 | res.addHeader("Location", "/#/login?next=" + |
| 75 | http_helpers::urlEncode(req.url)); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 76 | } |
| 77 | else |
| 78 | { |
| 79 | res.result(boost::beast::http::status::unauthorized); |
| 80 | // only send the WWW-authenticate header if this isn't a xhr |
| 81 | // from the browser. most scripts, |
| 82 | if (req.getHeaderValue("User-Agent").empty()) |
| 83 | { |
| 84 | res.addHeader("WWW-Authenticate", "Basic"); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | res.end(); |
| 89 | return; |
| 90 | } |
| 91 | |
| 92 | // TODO get user privileges here and propagate it via MW Context |
| 93 | // else let the request continue unharmed |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 94 | } |
Ed Tanous | f927347 | 2017-02-28 16:05:13 -0800 | [diff] [blame] | 95 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 96 | template <typename AllContext> |
| 97 | void afterHandle(Request& req, Response& res, Context& ctx, |
| 98 | AllContext& allctx) |
| 99 | { |
| 100 | // TODO(ed) THis should really be handled by the persistent data |
| 101 | // middleware, but because it is upstream, it doesn't have access to the |
| 102 | // session information. Should the data middleware persist the current |
| 103 | // user session? |
Ratan Gupta | 6f35956 | 2019-04-03 10:39:08 +0530 | [diff] [blame] | 104 | if (req.session != nullptr && |
| 105 | req.session->persistence == |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 106 | crow::persistent_data::PersistenceType::SINGLE_REQUEST) |
| 107 | { |
| 108 | persistent_data::SessionStore::getInstance().removeSession( |
Ratan Gupta | 6f35956 | 2019-04-03 10:39:08 +0530 | [diff] [blame] | 109 | req.session); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 110 | } |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 111 | } |
| 112 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 113 | private: |
| 114 | const std::shared_ptr<crow::persistent_data::UserSession> |
Ed Tanous | 39e7750 | 2019-03-04 17:35:53 -0800 | [diff] [blame] | 115 | performBasicAuth(std::string_view auth_header) const |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 116 | { |
| 117 | BMCWEB_LOG_DEBUG << "[AuthMiddleware] Basic authentication"; |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 118 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 119 | std::string authData; |
Ed Tanous | 39e7750 | 2019-03-04 17:35:53 -0800 | [diff] [blame] | 120 | std::string_view param = auth_header.substr(strlen("Basic ")); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 121 | if (!crow::utility::base64Decode(param, authData)) |
| 122 | { |
| 123 | return nullptr; |
| 124 | } |
| 125 | std::size_t separator = authData.find(':'); |
| 126 | if (separator == std::string::npos) |
| 127 | { |
| 128 | return nullptr; |
| 129 | } |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 130 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 131 | std::string user = authData.substr(0, separator); |
| 132 | separator += 1; |
| 133 | if (separator > authData.size()) |
| 134 | { |
| 135 | return nullptr; |
| 136 | } |
| 137 | std::string pass = authData.substr(separator); |
| 138 | |
| 139 | BMCWEB_LOG_DEBUG << "[AuthMiddleware] Authenticating user: " << user; |
| 140 | |
Joseph Reynolds | d887fff | 2020-01-14 16:34:09 -0600 | [diff] [blame] | 141 | if (pamAuthenticateUser(user, pass) != PAM_SUCCESS) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 142 | { |
| 143 | return nullptr; |
| 144 | } |
| 145 | |
| 146 | // TODO(ed) generateUserSession is a little expensive for basic |
| 147 | // auth, as it generates some random identifiers that will never be |
| 148 | // used. This should have a "fast" path for when user tokens aren't |
| 149 | // needed. |
| 150 | // This whole flow needs to be revisited anyway, as we can't be |
| 151 | // calling directly into pam for every request |
| 152 | return persistent_data::SessionStore::getInstance().generateUserSession( |
| 153 | user, crow::persistent_data::PersistenceType::SINGLE_REQUEST); |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 154 | } |
| 155 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 156 | const std::shared_ptr<crow::persistent_data::UserSession> |
Ed Tanous | 39e7750 | 2019-03-04 17:35:53 -0800 | [diff] [blame] | 157 | performTokenAuth(std::string_view auth_header) const |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 158 | { |
| 159 | BMCWEB_LOG_DEBUG << "[AuthMiddleware] Token authentication"; |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 160 | |
Ed Tanous | 39e7750 | 2019-03-04 17:35:53 -0800 | [diff] [blame] | 161 | std::string_view token = auth_header.substr(strlen("Token ")); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 162 | auto session = |
| 163 | persistent_data::SessionStore::getInstance().loginSessionByToken( |
| 164 | token); |
| 165 | return session; |
Ed Tanous | 1ea9f06 | 2018-03-27 17:45:20 -0700 | [diff] [blame] | 166 | } |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 167 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 168 | const std::shared_ptr<crow::persistent_data::UserSession> |
| 169 | performXtokenAuth(const crow::Request& req) const |
| 170 | { |
| 171 | BMCWEB_LOG_DEBUG << "[AuthMiddleware] X-Auth-Token authentication"; |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 172 | |
Ed Tanous | 39e7750 | 2019-03-04 17:35:53 -0800 | [diff] [blame] | 173 | std::string_view token = req.getHeaderValue("X-Auth-Token"); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 174 | if (token.empty()) |
| 175 | { |
| 176 | return nullptr; |
| 177 | } |
| 178 | auto session = |
| 179 | persistent_data::SessionStore::getInstance().loginSessionByToken( |
| 180 | token); |
| 181 | return session; |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 182 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 183 | |
| 184 | const std::shared_ptr<crow::persistent_data::UserSession> |
| 185 | performCookieAuth(const crow::Request& req) const |
| 186 | { |
| 187 | BMCWEB_LOG_DEBUG << "[AuthMiddleware] Cookie authentication"; |
| 188 | |
Ed Tanous | 39e7750 | 2019-03-04 17:35:53 -0800 | [diff] [blame] | 189 | std::string_view cookieValue = req.getHeaderValue("Cookie"); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 190 | if (cookieValue.empty()) |
| 191 | { |
| 192 | return nullptr; |
| 193 | } |
| 194 | |
| 195 | auto startIndex = cookieValue.find("SESSION="); |
| 196 | if (startIndex == std::string::npos) |
| 197 | { |
| 198 | return nullptr; |
| 199 | } |
| 200 | startIndex += sizeof("SESSION=") - 1; |
| 201 | auto endIndex = cookieValue.find(";", startIndex); |
| 202 | if (endIndex == std::string::npos) |
| 203 | { |
| 204 | endIndex = cookieValue.size(); |
| 205 | } |
Ed Tanous | 39e7750 | 2019-03-04 17:35:53 -0800 | [diff] [blame] | 206 | std::string_view authKey = |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 207 | cookieValue.substr(startIndex, endIndex - startIndex); |
| 208 | |
| 209 | const std::shared_ptr<crow::persistent_data::UserSession> session = |
| 210 | persistent_data::SessionStore::getInstance().loginSessionByToken( |
| 211 | authKey); |
| 212 | if (session == nullptr) |
| 213 | { |
| 214 | return nullptr; |
| 215 | } |
Ed Tanous | 1e43987 | 2018-05-18 11:48:52 -0700 | [diff] [blame] | 216 | #ifndef BMCWEB_INSECURE_DISABLE_CSRF_PREVENTION |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 217 | // RFC7231 defines methods that need csrf protection |
| 218 | if (req.method() != "GET"_method) |
| 219 | { |
Ed Tanous | 39e7750 | 2019-03-04 17:35:53 -0800 | [diff] [blame] | 220 | std::string_view csrf = req.getHeaderValue("X-XSRF-TOKEN"); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 221 | // Make sure both tokens are filled |
| 222 | if (csrf.empty() || session->csrfToken.empty()) |
| 223 | { |
| 224 | return nullptr; |
| 225 | } |
Ed Tanous | 51dae67 | 2018-09-05 16:07:32 -0700 | [diff] [blame] | 226 | |
| 227 | if (csrf.size() != crow::persistent_data::sessionTokenSize) |
| 228 | { |
| 229 | return nullptr; |
| 230 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 231 | // Reject if csrf token not available |
Ed Tanous | 51dae67 | 2018-09-05 16:07:32 -0700 | [diff] [blame] | 232 | if (!crow::utility::constantTimeStringCompare(csrf, |
| 233 | session->csrfToken)) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 234 | { |
| 235 | return nullptr; |
| 236 | } |
| 237 | } |
Ed Tanous | 1e43987 | 2018-05-18 11:48:52 -0700 | [diff] [blame] | 238 | #endif |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 239 | return session; |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 240 | } |
| 241 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 242 | // checks if request can be forwarded without authentication |
| 243 | bool isOnWhitelist(const crow::Request& req) const |
| 244 | { |
| 245 | // it's allowed to GET root node without authentica tion |
| 246 | if ("GET"_method == req.method()) |
| 247 | { |
| 248 | if (req.url == "/redfish/v1" || req.url == "/redfish/v1/" || |
| 249 | req.url == "/redfish" || req.url == "/redfish/" || |
| 250 | req.url == "/redfish/v1/odata" || |
| 251 | req.url == "/redfish/v1/odata/") |
| 252 | { |
| 253 | return true; |
| 254 | } |
| 255 | else if (crow::webassets::routes.find(std::string(req.url)) != |
| 256 | crow::webassets::routes.end()) |
| 257 | { |
| 258 | return true; |
| 259 | } |
| 260 | } |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 261 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 262 | // it's allowed to POST on session collection & login without |
| 263 | // authentication |
| 264 | if ("POST"_method == req.method()) |
| 265 | { |
| 266 | if ((req.url == "/redfish/v1/SessionService/Sessions") || |
| 267 | (req.url == "/redfish/v1/SessionService/Sessions/") || |
| 268 | (req.url == "/login")) |
| 269 | { |
| 270 | return true; |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | return false; |
| 275 | } |
Ed Tanous | 9992332 | 2017-03-03 14:21:24 -0800 | [diff] [blame] | 276 | }; |
Ed Tanous | f3d847c | 2017-06-12 16:01:42 -0700 | [diff] [blame] | 277 | |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 278 | // TODO(ed) see if there is a better way to allow middlewares to request |
| 279 | // routes. |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 280 | // Possibly an init function on first construction? |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 281 | template <typename... Middlewares> void requestRoutes(Crow<Middlewares...>& app) |
| 282 | { |
| 283 | static_assert( |
| 284 | black_magic::Contains<persistent_data::Middleware, |
| 285 | Middlewares...>::value, |
| 286 | "token_authorization middleware must be enabled in app to use " |
| 287 | "auth routes"); |
Ed Tanous | c7a29d3 | 2019-10-23 13:30:04 -0700 | [diff] [blame] | 288 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 289 | BMCWEB_ROUTE(app, "/login") |
| 290 | .methods( |
Ed Tanous | fd4859a | 2019-10-23 13:31:38 -0700 | [diff] [blame] | 291 | "POST"_method)([](const crow::Request& req, crow::Response& res) { |
Ed Tanous | 39e7750 | 2019-03-04 17:35:53 -0800 | [diff] [blame] | 292 | std::string_view contentType = req.getHeaderValue("content-type"); |
| 293 | std::string_view username; |
| 294 | std::string_view password; |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 295 | |
Ed Tanous | c7a29d3 | 2019-10-23 13:30:04 -0700 | [diff] [blame] | 296 | bool looksLikePhosphorRest = false; |
Ed Tanous | db024a5 | 2018-03-06 12:50:34 -0800 | [diff] [blame] | 297 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 298 | // This object needs to be declared at this scope so the strings |
| 299 | // within it are not destroyed before we can use them |
| 300 | nlohmann::json loginCredentials; |
| 301 | // Check if auth was provided by a payload |
Ed Tanous | fdf43a3 | 2019-07-31 16:52:24 -0700 | [diff] [blame] | 302 | if (boost::starts_with(contentType, "application/json")) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 303 | { |
| 304 | loginCredentials = |
| 305 | nlohmann::json::parse(req.body, nullptr, false); |
| 306 | if (loginCredentials.is_discarded()) |
| 307 | { |
Ed Tanous | fdf43a3 | 2019-07-31 16:52:24 -0700 | [diff] [blame] | 308 | BMCWEB_LOG_DEBUG << "Bad json in request"; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 309 | res.result(boost::beast::http::status::bad_request); |
| 310 | res.end(); |
| 311 | return; |
| 312 | } |
| 313 | |
| 314 | // check for username/password in the root object |
| 315 | // THis method is how intel APIs authenticate |
| 316 | nlohmann::json::iterator userIt = |
| 317 | loginCredentials.find("username"); |
| 318 | nlohmann::json::iterator passIt = |
| 319 | loginCredentials.find("password"); |
| 320 | if (userIt != loginCredentials.end() && |
| 321 | passIt != loginCredentials.end()) |
| 322 | { |
| 323 | const std::string* userStr = |
| 324 | userIt->get_ptr<const std::string*>(); |
| 325 | const std::string* passStr = |
| 326 | passIt->get_ptr<const std::string*>(); |
| 327 | if (userStr != nullptr && passStr != nullptr) |
| 328 | { |
| 329 | username = *userStr; |
| 330 | password = *passStr; |
| 331 | } |
| 332 | } |
| 333 | else |
| 334 | { |
| 335 | // Openbmc appears to push a data object that contains the |
| 336 | // same keys (username and password), attempt to use that |
| 337 | auto dataIt = loginCredentials.find("data"); |
| 338 | if (dataIt != loginCredentials.end()) |
| 339 | { |
| 340 | // Some apis produce an array of value ["username", |
| 341 | // "password"] |
| 342 | if (dataIt->is_array()) |
| 343 | { |
| 344 | if (dataIt->size() == 2) |
| 345 | { |
| 346 | nlohmann::json::iterator userIt2 = |
| 347 | dataIt->begin(); |
| 348 | nlohmann::json::iterator passIt2 = |
| 349 | dataIt->begin() + 1; |
Ed Tanous | c7a29d3 | 2019-10-23 13:30:04 -0700 | [diff] [blame] | 350 | looksLikePhosphorRest = true; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 351 | if (userIt2 != dataIt->end() && |
| 352 | passIt2 != dataIt->end()) |
| 353 | { |
| 354 | const std::string* userStr = |
| 355 | userIt2->get_ptr<const std::string*>(); |
| 356 | const std::string* passStr = |
| 357 | passIt2->get_ptr<const std::string*>(); |
| 358 | if (userStr != nullptr && |
| 359 | passStr != nullptr) |
| 360 | { |
| 361 | username = *userStr; |
| 362 | password = *passStr; |
| 363 | } |
| 364 | } |
| 365 | } |
| 366 | } |
| 367 | else if (dataIt->is_object()) |
| 368 | { |
| 369 | nlohmann::json::iterator userIt2 = |
| 370 | dataIt->find("username"); |
| 371 | nlohmann::json::iterator passIt2 = |
| 372 | dataIt->find("password"); |
| 373 | if (userIt2 != dataIt->end() && |
| 374 | passIt2 != dataIt->end()) |
| 375 | { |
| 376 | const std::string* userStr = |
| 377 | userIt2->get_ptr<const std::string*>(); |
| 378 | const std::string* passStr = |
| 379 | passIt2->get_ptr<const std::string*>(); |
| 380 | if (userStr != nullptr && passStr != nullptr) |
| 381 | { |
| 382 | username = *userStr; |
| 383 | password = *passStr; |
| 384 | } |
| 385 | } |
| 386 | } |
| 387 | } |
| 388 | } |
| 389 | } |
| 390 | else |
| 391 | { |
| 392 | // check if auth was provided as a headers |
| 393 | username = req.getHeaderValue("username"); |
| 394 | password = req.getHeaderValue("password"); |
| 395 | } |
| 396 | |
| 397 | if (!username.empty() && !password.empty()) |
| 398 | { |
Joseph Reynolds | d887fff | 2020-01-14 16:34:09 -0600 | [diff] [blame] | 399 | if (pamAuthenticateUser(username, password) != PAM_SUCCESS) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 400 | { |
| 401 | res.result(boost::beast::http::status::unauthorized); |
| 402 | } |
| 403 | else |
| 404 | { |
| 405 | auto session = persistent_data::SessionStore::getInstance() |
| 406 | .generateUserSession(username); |
| 407 | |
Ed Tanous | c7a29d3 | 2019-10-23 13:30:04 -0700 | [diff] [blame] | 408 | if (looksLikePhosphorRest) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 409 | { |
Ed Tanous | c7a29d3 | 2019-10-23 13:30:04 -0700 | [diff] [blame] | 410 | // Phosphor-Rest requires a very specific login |
| 411 | // structure, and doesn't actually look at the status |
| 412 | // code. |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 413 | // TODO(ed).... Fix that upstream |
| 414 | res.jsonValue = { |
| 415 | {"data", |
| 416 | "User '" + std::string(username) + "' logged in"}, |
| 417 | {"message", "200 OK"}, |
| 418 | {"status", "ok"}}; |
| 419 | |
| 420 | // Hack alert. Boost beast by default doesn't let you |
| 421 | // declare multiple headers of the same name, and in |
| 422 | // most cases this is fine. Unfortunately here we need |
| 423 | // to set the Session cookie, which requires the |
| 424 | // httpOnly attribute, as well as the XSRF cookie, which |
| 425 | // requires it to not have an httpOnly attribute. To get |
| 426 | // the behavior we want, we simply inject the second |
| 427 | // "set-cookie" string into the value header, and get |
| 428 | // the result we want, even though we are technicaly |
| 429 | // declaring two headers here. |
| 430 | res.addHeader("Set-Cookie", |
| 431 | "XSRF-TOKEN=" + session->csrfToken + |
| 432 | "; Secure\r\nSet-Cookie: SESSION=" + |
| 433 | session->sessionToken + |
| 434 | "; Secure; HttpOnly"); |
| 435 | } |
| 436 | else |
| 437 | { |
| 438 | // if content type is json, assume json token |
| 439 | res.jsonValue = {{"token", session->sessionToken}}; |
| 440 | } |
| 441 | } |
| 442 | } |
| 443 | else |
| 444 | { |
Ed Tanous | fdf43a3 | 2019-07-31 16:52:24 -0700 | [diff] [blame] | 445 | BMCWEB_LOG_DEBUG << "Couldn't interpret password"; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 446 | res.result(boost::beast::http::status::bad_request); |
| 447 | } |
| 448 | res.end(); |
| 449 | }); |
| 450 | |
| 451 | BMCWEB_ROUTE(app, "/logout") |
Ratan Gupta | 6f35956 | 2019-04-03 10:39:08 +0530 | [diff] [blame] | 452 | .methods("POST"_method)( |
Ed Tanous | fd4859a | 2019-10-23 13:31:38 -0700 | [diff] [blame] | 453 | [](const crow::Request& req, crow::Response& res) { |
Ratan Gupta | 6f35956 | 2019-04-03 10:39:08 +0530 | [diff] [blame] | 454 | auto& session = req.session; |
| 455 | if (session != nullptr) |
| 456 | { |
| 457 | res.jsonValue = { |
| 458 | {"data", "User '" + session->username + "' logged out"}, |
| 459 | {"message", "200 OK"}, |
| 460 | {"status", "ok"}}; |
Anthony Wilson | af713a6 | 2019-03-15 15:40:58 -0500 | [diff] [blame] | 461 | |
Ratan Gupta | 6f35956 | 2019-04-03 10:39:08 +0530 | [diff] [blame] | 462 | persistent_data::SessionStore::getInstance().removeSession( |
| 463 | session); |
| 464 | } |
| 465 | res.end(); |
| 466 | return; |
| 467 | }); |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 468 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 469 | } // namespace token_authorization |
| 470 | } // namespace crow |