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